多项式程序中的C++动态数组和指针导致退出错误255



我目前正在做一个程序,读取一个次数和系数,并创建一个多项式结构。该程序可以将多项式相加和相乘,然后输出和或乘积。程序运行,输出正确答案,然后得到一个窗口错误:

poly.exe已停止工作一个问题导致程序停止正常工作。如果有可用的解决方案,Windows将关闭该程序并通知您。

然后scite显示退出代码:255

我认为它可能是我在乘法多边形中使用的double-for循环中的东西,或者初始化指向系数数组的指针。但我不知道是什么。我正在使用的罐头如下:

#include <iostream>
#include <stdio.h>
using namespace std;
struct Poly {
   int degree;
   int *coeff; //array of coefficients from lowest degree to highest degree
};
//Reads the coefficients of a polynomial from standard input
//Creates a poly struct and returns a pointer to the poly
Poly* readPoly();
//Outputs a polynomial to standard output with the variable x
void outputPoly(const Poly* p, char x);
//Computes the sum of two polynomials and returns
//a pointer to the new poly struct which is their sum
Poly* addPoly(const Poly* a, const Poly* b);
//Computes the product of two polynomials and returns
//a pointer to the new poly struct which is their product
Poly* multPoly(const Poly* a, const Poly* b);
//Returns to the heap the memory allocated for the polynomial
//and sets p to the nullptr
void deletePoly(Poly* &p);
Poly* readPoly() {
   int deg;
   //Read the highest degree
   cout << "Input the degree: ";
   cin >> deg;
   //Handles when the degree is == 0
   if(deg == 0) {
      int *C = new int[deg+1];
      Poly *p;
      p = new Poly;
      p->degree = deg;
      p->coeff = C;
      return p;
   }
   int *C = new int[deg+1];
   //Read the coefficients
   cout << "Input the coefficients: ";
   for(int i = 0; i <= deg; i++) {
      cin >> C[i];
   }
   //Create a new poly structure, assign its fields 
   //and retun a pointer to the new structure
   Poly *p;
   p = new Poly;
   p->degree = deg;
   p->coeff = C;
   return p;
}
void outputPoly(const Poly* p, char x) {
   //Set the degree and cooefficients to be used in the loop
   int d = p->degree;
   int *C = p->coeff;
   //Output the polynomial, depending on the degree/coeff
   for(int i = 0; i <= d; i++) {
      //if the coeff is zero, and the degree is > 1
      if(C[i] == 0 && i > 0)
         continue; //Skip the +
      //if the degree is 0, and the coeff is 0
      else if(i == 0 && C[i] == 0) {
         cout << C[i];
         continue; //Skip the +
      }
      //if the degree is 0, and the coeff is not 0
      else if(i == 0 && C[i] != 0)
         cout << C[i];
      //if the degree is 1, and the coeff is 0
      else if(C[i] == 0 && i == 1)
         cout << x;
      //if the degree is 1, and the coeff is 1
      else if(C[i] == 1 && i == 1)
         cout << x;
      //if the degree is 0, and the coeff is > 0
      else if(C[i] > 0 && i == 0)
         cout << C[i] << "*" << x;
      //if the coefficient is 1
      else if(C[i] == 1)
         cout << x << "^" << i;
      //if the degree is 1
      else if(i == 1)
         cout << C[i] << "*" << x;
      //any other circumstance
      else
         cout << C[i] << "*" << x << "^" << i;
      //Print a +, as long as it's not the last term
      if(i != d)
         cout << " + ";
   }
}
void deletePoly(Poly* &p) {
   delete[] p->coeff; //Delete the array first
   delete p; 
   p = nullptr;
}
const Poly* getLargest(const Poly* a, const Poly* b) {
//Helper function to get the larger polynomial, given two
   if(a->degree > b->degree)
      return a;
   else
      return b;
}
const Poly* getSmallest(const Poly* a, const Poly* b) {
//Helper function to get the smaller polynomial, given two
   if(a->degree < b->degree)
      return a;
   else
      return b;
}
Poly* addPoly(const Poly* a, const Poly* b){
   int i, j;
   int *polyOneC = a->coeff;
   int *polyTwoC = b->coeff;
   //The new polynomials degree is the size of the polynomial that is the largest
   int polyThreeD = getLargest(a, b)->degree;
   int *polyThreeC = new int[polyThreeD];
   for(i = 0, j = 0; j <= polyThreeD; i++, j++) {
      //If the polynomials are of different size, 
      //then any coefficent term over the size 
      //of the smaller polynomial degree stays the same
      if(i > getSmallest(a, b)->degree)
         polyThreeC[i] = getLargest(a, b)->coeff[i];
      else
         //Otherwise, just add them
         polyThreeC[i] = polyOneC[j] + polyTwoC[j];
         //"Shifts" if it's equal to zero
         if(polyThreeC[i] == 0)
            i--;
   }
   //Ensures the remaining terms have a coefficient value(0)
   while(i <= polyThreeD) {
      polyThreeC[i] = 0;
      i++;
   }
   Poly *sumPoly;
   sumPoly = new Poly;
   sumPoly->degree = polyThreeD;
   sumPoly->coeff = polyThreeC;
   return sumPoly;
}
Poly* multPoly(const Poly* a, const Poly* b) {
   //Get the degrees and arrays of coefficients
   int polyOneD = a->degree;
   int polyTwoD = b->degree; 
   int *polyOneC = a-> coeff;
   int *polyTwoC = b-> coeff;
   int polyThreeD = polyOneD + polyTwoD; 
   int *polyThreeC = new int[polyThreeD + 1];
   //Initialize the array of coefficients
   for(int i = 0; i <= polyThreeD; i++)
     polyThreeC[i] = 0;
   //Multiple the coeffients and add to the position of the degree
   for(int i = 0; i <= polyOneD; i++) {
      for(int j = 0; j <= polyTwoD; j++) {
            polyThreeC[i + j] += (polyOneC[i] * polyTwoC[j]);
      }
   }
   //Create a new polynomial pointer and return it
   Poly *productPoly;
   productPoly = new Poly;
   productPoly->degree = polyThreeD;
   productPoly->coeff = polyThreeC;
   return productPoly;   
}
int main() {
   Poly *x = readPoly();
   Poly *y = readPoly();
   //Test the add poly function
   cout << "(";
   outputPoly(x, 'x');
   cout << ")";

   cout << " + ";
   cout << "(";
   outputPoly(y, 'x');
   cout << ")";
   cout << endl;
   //Call addPoly
   Poly *z = addPoly(x, y);
   cout << "= ";
   cout << "(";
   outputPoly(z, 'x');
   cout << ")";
   cout << endl;
   cout << endl;
   //Test the multiply poly function
   cout << "(";
   outputPoly(x, 'x');
   cout << ")";

   cout << " * ";
   cout << "(";
   outputPoly(y, 'x');
   cout << ")";
   cout << endl;
   //Call multPoly
   z = multPoly(x, y);
   cout << "= ";
   cout << "(";
   outputPoly(z, 'x');
   cout << ")";
   cout << endl;

   //Delete the polynomials now that we are done with them
   deletePoly(x);
   deletePoly(y);
   deletePoly(z);
}

另一件事是,每次运行时都不会出现错误,到目前为止,当我输入一个次数为4的多项式时,我已经看到了这种情况。与次数为2的多项式相反,它工作得很好。

有人能帮我吗!

您在addPoly(polyThreeC)中分配的系数数组不够大。在分配数组时,您忘记在度数上加一。