打印出在类内部被双重包装的多项式

时间:2018-11-06 03:38:53

标签: c++

我的任务是创建一个称为Polynomial的C ++类,该类将不同的数学公式应用于多项式。我得到了4个单独的代码位,这些代码必须与程序的其余部分一起使用,而且我不太了解我应该如何在整个函数中使用这些包装器。例如在我的打印功能和访问多项式中隐藏的多层。

预制1:

#ifndef __GUARDED_ARRAY_H__
#define __GUARDED_ARRAY_H__

using namespace std;

typedef int ItemType;

const unsigned MAX_LENGTH = 500;

//
// GuardedArray
// - A wrapper class for C++ arrays to make array access safe.
//   Specifically, initialization is guaranteed, and assertions are
    //   in place to detect array index out of bound errors in array member
//   accesses.
//

class GuardedArray {
public:

    // Purpose: Initializes array elements to zeros
    GuardedArray();

    // Purpose: Initializes all array elements to a given value
    GuardedArray(ItemType x);

    // Purpose: Read array element at index i
    ItemType read(unsigned i) const;

    // Purpose: Write x into array element at index i.
    void write(unsigned i, ItemType x);

private:

    ItemType array[MAX_LENGTH];

};

#endif

预制2:

#include <cassert>
#include "polynomial.h"
#include "guarded_array.h"
#include "managed_array.h"

GuardedArray::GuardedArray() {
    for (unsigned i = 0; i < MAX_LENGTH; i++)
        array[i] = 0;
}

GuardedArray::GuardedArray(ItemType x) {
    for (unsigned i = 0; i < MAX_LENGTH; i++)
        array[i] = x;
}

ItemType GuardedArray::read(unsigned i) const {
    assert(i < MAX_LENGTH);
    return array[i];
}

void GuardedArray::write(unsigned i, ItemType x) {
    assert(i < MAX_LENGTH);
    array[i] = x;
}

预制3:

#ifndef __MANAGED_ARRAY_H__
#define __MANAGED_ARRAY_H__

#include "guarded_array.h"

using namespace std;

//
// ManagedArray
// - A wrapper class for C++ arrays to facilitate insertion and removal of
//   array elements.
// - Every instance of ManagedArray has a size that can be increased
//   until the maximum capacity MAX_LENGTH is reached.
//

class ManagedArray {
public:

    // Purpose: Initializes array to have zero size.
    ManagedArray();

    // Purpose: Initializes array to a given size.  All array elements
    ManagedArray(unsigned N);

    // Purpose: Initializes array to a given size.  All array elements
    //          are initialized to x.
    ManagedArray(unsigned N, ItemType x);

    // Purpose: Return the current size of the array.
    unsigned size() const;

    // Purpose: Read array element at index i
    ItemType read(unsigned i) const;

    // Purpose: Write x into array element at index i.
    void write(unsigned i, ItemType x);

    // Purpose: Insert an element into the array.
    void insert(unsigned i, ItemType x);

    // Purpose: Remove an element from the array.
    void remove(unsigned i);

private:

    unsigned count;
    GuardedArray array;

};

#endif

预制4:

#include <cassert>
#include "polynomial.h"
#include "guarded_array.h"
#include "managed_array.h"

ManagedArray::ManagedArray() : array() {
    count = 0;
}

ManagedArray::ManagedArray(unsigned N) : array() {
    assert(N <= MAX_LENGTH);
    count = N;
}

ManagedArray::ManagedArray(unsigned N, ItemType x) : array(x) {
    assert(N <= MAX_LENGTH);
    count = N;
}

unsigned ManagedArray::size() const {
    return count;
}

ItemType ManagedArray::read(unsigned i) const {
    assert(i < count);
    return array.read(i);
}

void ManagedArray::write(unsigned i, ItemType x) {
    assert(i < count);
    array.write(i, x);
}

void ManagedArray::insert(unsigned i, ItemType x) {
    assert(i <= count && count < MAX_LENGTH);
    for (unsigned j = count; j > i; j--)
        array.write(j, array.read(j - 1));
    array.write(i, x);
    count++;
}

void ManagedArray::remove(unsigned i) {
    assert(i < count && count > 0);
    for (unsigned j = i; j < count - 1; j++)
        array.write(j, array.read(j + 1));
    count--;
}

这是我自己的代码,必须更改以适应上述代码:

#include "polynomial.h"

using namespace std;


int main()
{
    Polynomial poly1;

    poly1.print();

    /* Calculate all of these:
    The zero polynomial.
    The degree of the zero polynomial.
    The value of the zero polynomial when x = 1.
    The polynomial P(x) = -1 + 3x^2 - 2x^5.
    The degree of P(x).
    The value of P(1) and P(-2).
    The polynomial Q(x) = 1 + x^3 + 2x^5.
    The polynomial P(x) +Q(x).
    The polynomial P(x) - Q(x).
    The polynomial 4P(x).
    The polynomial x^2Q(x).
    The polynomial P(x)Q(x).
*/
    system("pause");
    return 0;
}

标题:

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include <iostream>
#include <cassert>
#include <iomanip>
#include "managed_array.h"

using namespace std;

//
// Polynomial class
//
//Purpose- To hold and calculate specific polynomial equations
//
//Parameter(s)- 
//              -None OR
//              -An array of coefficients and the size of the array OR
//              -An instance of Polynomial to copy into a new instance
//Precondition(s)- In the array/array size constructor the last element of the array must be non-zero
//              
//Returns- N/A
//
//Side Effect- Holds all functions required to intialize, hold, and calculate specific polynomial equations
//
//
class Polynomial
{
private:
    ManagedArray polynomial;

public:

    Polynomial(); //Default constructor
    /*
        second constructor takes an integer array and an integer array size as arguments, and initializes the target Polynomial
        instance with coefficients identical to the elements of the array
        A precondition for this operation is that the last element of the array must be non-zero, or else the array size is zero.
    */
    Polynomial(int array[], const unsigned int size);
    /*
        A third constructor expects a Polynomial instance as its sole argument, and initializes the target Polynomial 
        instance with coefficients identical to those in the argument Polynomial.
    */
    //Polynomial();

    //Purpose- Apply the Polynomial function to an integer argument.That is, compute the value of the Polynomial for a given value of x.
    void evaluate();

    //Purpose- An arithmetic addition operation that adds one instance of Polynomial to another.
    void add(const Polynomial& poly2);

    //Purpose- An arithmetic subtraction operation that subtracts one instance of Polynomial by another.
    void subtract();

    //Purpose- An arithmetic multiplication operation that multiplies one instance of Polynomial by another.
    void multiply();

    //Purpose- An arithmetic multiplication operation that multiplies an instance of Polynomial by an integer.
    //void multiply();

    //Purpose- An arithmetic multiplication operation that multiplies an instance of Polynomial by a polynomial of the form x^k for some non-negative integer k
    void raiseDegree();

    //Purpose- A Boolean operation that compares two instances of Polynomial to determine if they are identical.
    bool equal();

    //Purpose- Get the degree of a Polynomial instance.
    int getDegree();

    //Purpose- Retrieve the coefficient of the term x^k in a Polynomial instance, given a non-negative integer k.
    int getCoefficient();

    //Purpose- Print a Polynomial instance in a user-friendly format.
    void print() const;

    //The polynomial (-3 + 4x - 7x^3) should be printed in the following format.- 3 + 4 x - 7 x^3

};

#endif

cpp:

#include "polynomial.h"
#include "managed_array.h"
#include "guarded_array.h"

unsigned int i;

Polynomial::Polynomial()
{
    ManagedArray();
    GuardedArray();
}

Polynomial::Polynomial(int array[], const unsigned int size)
{
    ManagedArray(size);
    for (i = 0; i < size; i++)
    {
        int temp = array[i];
        GuardedArray(temp);
    }
}

//Polynomial::Polynomial()
//{
//
//}

void Polynomial::evaluate()
{

}

void Polynomial::add(const Polynomial& poly2)
{

}

void Polynomial::subtract()
{

}

void Polynomial::multiply()
{

}

//void Polynomial::multiply()
//{
//
//}

void Polynomial::raiseDegree()
{

}

bool Polynomial::equal()
{
    return false;
}

int Polynomial::getDegree()
{
    return -1;
}

int Polynomial::getCoefficient()
{
    return -1;
}

void Polynomial::print() const
{

}

1 个答案:

答案 0 :(得分:0)

您只需要处理polynomial类中的数据成员Polynomial,该数据成员表示多项式的系数。

例如,polynomial.read(2)应该返回项x ^ 2的系数

要开始使用,请访问 polynomial.cpp

Polynomial::Polynomial()
    :polynomial() {}

Polynomial::Polynomial(int array[], const int size)
    :polynomial(size)
{
    for (int i = 0; i < size; ++i)
        polynomial.write(i, array[i]);
}

double Polynomial::evaluate(double x) const
{
    double sum = 0;
    for (int i = 0; i < polynomial.size(); ++i)
        sum += polynomial.read(i) * pow(x, i);
    return sum;
}