C++ 多项式输出准确度为 50%,内存地址为 50%

C++ Polynomial Output is 50% accurate and 50% memory address

我正在编写一个实现稀疏多项式的程序,其中两个全局结构定义如下:

//Global structs to represent a polynomial
struct Term {
   int coeff;
   int degree;
};

struct Node {
   Term *term;
   Node *next;
};

class 有一个私有 Node *poly;

我正在重载 * 运算符以获得两个多项式的乘积。

这个头文件包括定义:

//Returns a polynomial that is the product of polynomial a and b
friend const Polynomial operator *(const Polynomial &a, const Polynomial &b);

对于函数,我遍历每个表示多项式 (a & b) 的链表,直到达到 nullptr,添加度数,乘以系数,创建一个新项,将其插入到临时项多项式,并使用我的重载加法运算符将临时多项式添加到要返回的多项式。我已经测试了重载的加法运算符,但没有收到这个问题,所以我认为这不是导致这个问题的原因。问题是有时我得到正确答案,在示例输出中,第一个 运行 是正确的,另一个 运行 产生一些术语和一些地址:

>PolynomialTester
Enter numbebr of terms of the polynomial: 3
    coefficient degree
Enter term 1:   3 0
Enter term 2:   2 1
Enter term 3:   1 2

3 + 2*x^1 + 1*x^2

Enter numbebr of terms of the polynomial: 3
    coefficient degree
Enter term 1:   3 0
Enter term 2:   2 1
Enter term 3:   1 2

3 + 2*x^1 + 1*x^2
9 + 12*x^1 + 7*x^2 + 6*x^3 + 1*x^4 //Correct
>Exit code: 0    Time: 33.22
>PolynomialTester
Enter numbebr of terms of the polynomial: 3
    coefficient degree
Enter term 1:   3 0
Enter term 2:   2 1
Enter term 3:   1 2

3 + 2*x^1 + 1*x^2

Enter numbebr of terms of the polynomial: 3
    coefficient degree
Enter term 1:   3 0
Enter term 2:   2 1
Enter term 3:   1 2

3 + 2*x^1 + 1*x^2
4109104 + 9 + 12*x^1 + 3*x^2 + 2*x^3 + 1*x^4 + 4108992*x^4108944 + 4109392*x^4109136 //Nope
>Exit code: 0    Time: 19.54

我用于重载 * 的函数是:

const Polynomial operator *(const Polynomial &a, const Polynomial &b) {
//Computes the sum of two polynomials a + b
//Overloaded + operator

   //The new polynomial that will be returned
   Polynomial polyProduct;

   Polynomial tempPoly;
   //Temporary nodes to process
   Node *tempA = a.poly;
   Node *tempB;

   while(tempA != nullptr) {
      tempB = b.poly;
      while(tempB != nullptr) {

         int degree = tempA->term->degree + tempB->term->degree;
         int coeff = tempA->term->coeff * tempB->term->coeff;
         tempPoly.insert(Polynomial::newTerm(coeff, degree));
         tempB = tempB->next;
      }
      polyProduct = polyProduct + tempPoly;
      tempPoly.deletePoly();
      tempA = tempA->next;
   }

   return polyProduct;
}

PS。 deletePoly() 遍历多项式并首先删除项,然后删除节点,直到它是 nullptr。

默认构造函数:

Term* Polynomial::newTerm(int c, int d) {
//Creates a new term
   Term *newTerm = new Term;
   newTerm->coeff = c;
   newTerm->degree = d;
   return newTerm;
}

Node* Polynomial::cons(Term *t, Node *p) {
//Creates a new node
   Node *newNode = new Node;
   newNode->term = t;
   newNode->next = p;
   return newNode;
}

Polynomial::Polynomial() {
//Creates the zero polynomial
   poly = cons(newTerm(0, 0), nullptr);
}

多项式测试器:

   int main() {
   Polynomial newPoly;
   Polynomial newPoly2;
   Polynomial newPoly3;

   newPoly.readPoly();
   cout << "\n";
   newPoly.printPoly();

   cout << "\n";

   newPoly2.readPoly();
   cout << "\n";
   newPoly2.printPoly();

   newPoly3 = newPoly * newPoly2;
   newPoly3.printPoly();
   newPoly3.deletePoly();
   newPoly2.deletePoly();
   newPoly.deletePoly();
}

运算符+

const Polynomial operator +(const Polynomial &a, const Polynomial &b) {
//Computes the sum of two polynomials a + b
//Overloaded + operator

   //The new polynomial that will be returned
   Polynomial polySum;
   //Temporary nodes to process
   Node *tempA = a.poly;
   Node *tempB = b.poly;

   //While neither node A or B is equal to the nullptr
   while(tempA != nullptr && tempB != nullptr) {

      int degreeA = tempA->term->degree;
      int degreeB = tempB->term->degree;

      if(degreeA < degreeB) {
         polySum.insert(tempA->term);
         tempA = tempA->next;
      }

      else if(degreeA > degreeB) {
         polySum.insert(tempB->term);
         tempB = tempB->next;
      }

      else {
         int coeff = tempA->term->coeff + tempB->term->coeff;
         int degree = degreeA;
         polySum.insert(Polynomial::newTerm(coeff, degree));
         tempA = tempA->next;
         tempB = tempB->next;
      }
   }
   //Incase one of the polynomials still has remaining terms
   while(tempA != nullptr) {
      polySum.insert(tempA->term);
      tempA = tempA->next;
   }
   //Incase one of the polynomials still has remaining terms
   while(tempB != nullptr) {
      polySum.insert(tempB->term);
      tempB = tempB->next;
   }
   //Removes any zero coeffiecient terms
   //Possibly due to a positive and negative coefficient being added
   polySum.remZeroCoeff();
   return polySum;
}

复制构造函数:

Polynomial::Polynomial(const Polynomial& oP) {
//Constructs a deep copy of the Polynomial being passed

   poly = nullptr;
   //The list to copy
   Node *toCopy = oP.poly;

   while(toCopy != nullptr) {
      insert(toCopy->term);
      poly->next = toCopy->next;
      toCopy = toCopy->next;
   }
}

重载赋值运算符,但目前在我的代码中已将其注释掉,因为它似乎只会让事情变得更糟

    Polynomial& Polynomial::operator =(const Polynomial &a) {

   //Check to see if it's passing itself
   if(this == &a) {
      return *this;
   }

   //Delete current polynomial  
   deletePoly();

   //The poly to copy
   Node *toCopy = a.poly;

   while(toCopy != nullptr) {
      poly = cons(toCopy->term, poly);
      toCopy = toCopy->next;
   }

   //Return a reference to itself
   return *this;
}

谁能帮助我理解为什么我有时会收到地址作为结果?

行tempPoly.deletePoly();是导致问题的原因,因此为了提高效率并解决问题,我在插入函数中添加了一个条件,用于检查传递的项的度是否等于当前多项式中的项度,如果是,则只需添加该项的系数并在不需要后删除该项。这消除了在重载*函数中进行任何删除或重载添加的需要,并提高了效率。