Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;多项式输出精度为50%,内存地址为50%_C++_Struct_Linked List_Operator Overloading_Polynomials - Fatal编程技术网

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

C++ C++;多项式输出精度为50%,内存地址为50%,c++,struct,linked-list,operator-overloading,polynomials,C++,Struct,Linked List,Operator Overloading,Polynomials,我正在编写一个实现稀疏多项式的程序,其中两个全局结构定义如下: //Global structs to represent a polynomial struct Term { int coeff; int degree; }; struct Node { Term *term; Node *next; }; 该类有一个私有的节点*poly 我正在重载*运算符以获得两个多项式的乘积 此文件的头文件包括以下定义: //Returns a polynomial that

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

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

struct Node {
   Term *term;
   Node *next;
};
该类有一个私有的
节点*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);
}
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;
   }
}
多项式酯:

   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();
}
重载赋值运算符,但这在我的代码中被注释掉了,因为它似乎只会使事情变得更糟

    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;
}

有人能帮助我理解为什么我有时会得到地址作为结果吗?

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

poly->next=toCopy->next
Huh?这有什么用?仍然没有
操作符=
。你有吗?@Dmitry会不会因为在第一个while循环之后有两个while循环而被处理?@n.m.没有重载赋值运算符while(toCopy!=nullptr){insert(toCopy->term);poly->next=toCopy->next;toCopy=toCopy->next;}这里是poly.insert(toCopy->term)?你在复制构造函数和任务中做了一些奇怪的事情。在这两种方法中都尝试
{insert(toCopy->term);toCopy=toCopy->next;}
。您的
操作符=
将颠倒术语顺序,并且您的复制选择器有一个无法解释的
poly->next=toCopy->next。解释这是如何导致内存泄漏的?答案中没有任何东西会导致内存泄漏,事实上,答案完全消除了对临时空闲存储内存的需要。Delete poly仍在主函数中对多项式调用,但在重载乘法函数中没有调用,因为没有要删除的内容。正如我在回答中所说,如果在insert方法中创建了一个不需要的术语,那么它将被删除。除此之外,析构函数、复制构造函数和重载赋值运算符都已就位。很抱歉,我误读了您的解释。出于某种原因,我想象您已经从析构函数中删除了deletePoly()。没有内存泄漏。Downvote已删除。不,等等,析构函数中没有deletePoly(),对吗?这是错误的,析构函数是正确的名称,这就是析构函数的设计目的。无论如何,如果您想听到建设性的批评,请将代码发布到codereview。stackexchange.com。
    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;
}