0

オーバーロード*演算子を使用して2倍の多項式を書き込もうとしています

これはオーバーロード*関数です

Polynomial Polynomial::operator * (const Polynomial &right)
{
    Polynomial temp;
    temp.setPolynomial(right.maxExp + maxExp + 1);
    for (int i = 0; i < maxExp; i++)
    {
        for (int j = 0; j < right.maxExp; j++)
            temp.poly[1][i+j] += poly[0][i] * right.poly[0][j];
    }
    return temp;
}

配列の結果。最初の行は係数で、2番目の行は指数を格納します。

The first (original) polynomial is: (degree = 4, F = x^2)
0 0 1 0 0
0 0 2 0 0
The second polynomial is: (degree = 4, F = x^2)
0 0 1 0 0
0 0 2 0 0
The result polynomial is: // the location of the result is right (x^4)
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0

多項式クラス

class Polynomial
{
private:
    int **poly;
    int maxExp;
    void createPolynomialArray(int);
public:
    Polynomial();
    Polynomial(int); // constructor
    Polynomial(const Polynomial &); // copy constructor
    ~Polynomial(); // destructor

    // setter
    void setCoefficient(int,int);
    void setPolynomial(int);

    // getters
    int getTerm() const; // get the maxExp (highest exponent)
    int getCoefficient(int,int) const; // get a specific exponential value

    // overloading operators
    void operator=(const Polynomial &); // assignment
    Polynomial operator+(const Polynomial &); // addition
    Polynomial operator-(const Polynomial &); // substraction
    Polynomial operator*(const Polynomial &);
}

質問:その値を生成するという私のコードの何が問題になっていますか?ありがとう!

4

1 に答える 1

1

これにより、複数の係数で結果の指数を更新します。

 temp.poly[1][i+j] += poly[0][i] * right.poly[0][j];

コードは

for (int j = 0; j < right.maxExp; j++)
{
    temp.poly[0][i+j] += poly[0][i] * right.poly[0][j];
    if (temp.poly[0][i+j] != 0)
        temp.poly[1][i+j] = i+j;
}

また、配列内の位置自体が指数を反映しているため、コードに指数を格納することは冗長な情報だと思います

于 2013-03-03T08:02:13.427 に答える