使用结构和指针进行多项式的乘法运算

时间:2015-03-05 05:08:28

标签: c++ pointers struct

我已经尝试了一段时间来实现多项式的长乘法,我仍然没有想出如何做到这一点。我一直在收垃圾号码。

这是我的代码:

#include <iostream>
using namespace std;

struct Poly
{
    int degree;
    int *coeffs; // array of coefficients from lowest degree to highest degree
};

Poly* readPoly();
void outputPoly(const Poly* p, char x);
Poly* multPoly(const Poly* a, const Poly* b);

int main()
{

    cout << "First polynomial" << endl;
    Poly* a = readPoly();
    cout << "Second polynomial" << endl;
    Poly* b = readPoly();


    cout << "a = ";
    outputPoly(a, 'x');
    cout << "b = ";
    outputPoly(b, 'x');

    Poly* prodOfPoly = multPoly(a,b);
    cout << "d = ";
    outputPoly(prodOfPoly, 'x');

    return 0;
}

Poly* readPoly()
{
    int deg;
    int* A;
    cout << "Input the degree: ";
    cin >> deg;
    A = new int[deg+1]; //add 1 for constants

    cout << "Input coefficients: ";

    //assign coefficients
    for (int i = 0; i <= deg; i++){
        cin >> A[i];
    }

    cout << endl;

    //create the polynomial structure and assign values to the fields
    Poly* p;
    p = new Poly;
    p->degree = deg;
    p->coeffs = A;

    return p;
}
void outputPoly(const Poly* p, char x)
{
    for (int i = 0; i <= p->degree; i++){
        //coefficient is non-zero
        if ( p->coeffs[i] != 0){
            //exponent is 0
            if (i == 0) 
                cout << *(p->coeffs);
            //exponent is 1 and coefficient is 1
            else if ( i == 1 && p->coeffs[i] == 1) 
                cout << " + " <<  x;
            //exponent is 1 and coefficient is NOT 1
            else if ( i == 1 && p->coeffs[i] != 1) 
                cout << " + " << p->coeffs[i] << "*" << x;
            //exponent is > 1 and coefficient is 1
            else if ( p->coeffs[i] == 1) 
                cout << " + " << x << "^" << i;
            //coefficient is positive
            else if ( p->coeffs[i] > 0) 
                cout  << " + " << p->coeffs[i] << "*" << x << "^" << i;
            //coefficient is negative
            else if ( p->coeffs[i] < 0)
                cout  << " - " << -p->coeffs[i] << "*" <<  x << "^" << i;
        }
        //polynomial is a constant
        else if( p->coeffs[i] == 0 && p->degree == 0)
            cout << p->coeffs[i];
    }
    cout << endl;
}
Poly* multPoly(const Poly* a, const Poly* b)
{
    Poly* finalProduct;
    finalProduct = new Poly;
    finalProduct->degree = a->degree + b->degree; 

    int* productCoeffs = new int[finalProduct->degree - 1];

    //solve for product of polynomials
    for (int i = 0; i != finalProduct->degree; i++){
        for (int j = 0; j != finalProduct->degree; j++)
            productCoeffs[i + j] += a->coeffs[i] * b->coeffs[j];
    }

    finalProduct->coeffs = productCoeffs;

    return finalProduct;
}

我知道我得到了垃圾号码,因为在这一行:

for (int i = 0; i != finalProduct->degree; i++){
            for (int j = 0; j != finalProduct->degree; j++)
                productCoeffs[i + j] += a->coeffs[i] * b->coeffs[j];

如果我输入的是

a: degree 3, coeffs, 0 1 2 3
b: degree 2, coeffs, 1 2 -3
然后产品的程度为7-1(a:3 + 1&amp; b:2 + 1)= 6 然后a->coeffs[i]会有垃圾编号,因为a->coeffs只会有3 + 1个最大单项。

输出应为:d = 4x^2 + 4x^3 - 9x^5, 但我明白了:d = 9398672 + x + 9371988*x^2 + 4*x^3 - 1112350759*x^4 - 2063433372*x^5 如果有人可以帮助并引导我修复那些非常感激的代码。谢谢!

2 个答案:

答案 0 :(得分:1)

我真的不明白你的代码在做什么是诚实的,但很明显你在multPoly函数中溢出了缓冲区:

int* productCoeffs = new int[finalProduct->degree - 1];

//solve for product of polynomials
for (int i = 0; i != finalProduct->degree; i++){
    for (int j = 0; j != finalProduct->degree; j++)
        productCoeffs[i + j] += a->coeffs[i] * b->coeffs[j];
}

i + j可以轻松地为&gt; = finalProduct->degree - 1,这意味着您正在写productCoeffs数组的末尾。

如果将阵列分配更改为

int* productCoeffs = new int[finalProduct->degree * 2];

它似乎修复了输出中的垃圾数量。无论是否给你正确答案,我都不知道:)

答案 1 :(得分:1)

通过@JonathanPotter跟进答案......

新对象的内存分配应为:

int* productCoeffs = new int[finalProduct->degree + 1];
                                               //^^^ +, not -.

循环应该是:

for (int i = 0; i != a->degree; i++){
    for (int j = 0; j != b->degree; j++)
        productCoeffs[i + j] += a->coeffs[i] * b->coeffs[j];
}

在上面的循环之前,我会添加代码来将产品的系数初始化为0。

for (int i = 0; i != finalProduct->degree; i++){
    productCoeffs[i] = 0.0;
}

<强>更新

multPoly的正确实施:

Poly* multPoly(const Poly* a, const Poly* b)
{
   Poly* finalProduct;
   finalProduct = new Poly;
   finalProduct->degree = a->degree + b->degree; 

   int* productCoeffs = new int[finalProduct->degree + 1];

   for (int i = 0; i <= finalProduct->degree; i++){
      productCoeffs[i] = 0.0;
   }

   //solve for product of polynomials
   for (int i = 0; i <= a->degree; i++){
      for (int j = 0; j <= b->degree; j++)
         productCoeffs[i + j] += a->coeffs[i] * b->coeffs[j];
   }

   finalProduct->coeffs = productCoeffs;

   return finalProduct;
}

http://ideone.com/S7Tc2R看到它。