Home > Back-end >  C questions, ask the help of new debug it
C questions, ask the help of new debug it

Time:09-30

Polynomial class Polynomial, Polynomial of each expressed in an array or structure, each contains a factor and an index, such as the index of 4 2 x4, coefficient of 2, please develop a complete Polynomial class, including the constructor, destructor and the get functions and set, the class also provides the following overloaded operator:
1) overloaded operator "+" and "-", the two polynomial addition or subtraction,
2) overloading multiplication operator "*", the two polynomial multiplication,
3) the overloaded assignment operator "=", to assign a polynomial to another polynomials,
#include
#include
using namespace std;
Struct Node
{
Int coefficient;
Int exponent;
Node * next;

};
The class Polynomial
{
Public:
Polynomial ();
Polynomial (int len);
Polynomial (const Polynomial& Old);
~ Polynomial ();
Void the get ();
Void the set (int len);
Polynomial operator + (Polynomial & amp; A);
Polynomial operator - (Polynomial & amp; A);
Polynomial operator * (Polynomial & amp; A);
Polynomial operator=(Polynomial & amp; A);
Private:
Int length;
Node * first;//the head pointer
Void the add (int a, int b);
Void multiply (int a, int b);
};
Polynomial: : Polynomial ()
{
The first=0;
Length=0;
}
Polynomial: : Polynomial (int len)
{
Node * Node=new Node;
cin> Node - & gt; Coefficient> Node - & gt; Exponent;
Node - & gt; Next=0;
The first=node;
Node * p=first;
for(int i=0; I{
Node * Node=new Node;
cin> Node - & gt; Coefficient> Node - & gt; Exponent;
Node - & gt; Next=0;
p-> Next=node;
P=p - & gt; next;
}
Length=len;
}
Polynomial: : Polynomial (const Polynomial & amp; Old)
{
If (old. The first==0)
{
The first=0;
Length=0;
}
The else
{
The Node * q=old. First;
Node * Node=new Node;
Node - & gt; Coefficient=q - & gt; Coefficient;
Node - & gt; Exponent=q - & gt; Exponent;
Node - & gt; Next=0;
Q=q - & gt; next;
The first=node;
Node * p=first;
For (int I=0; I{
Node * Node=new Node;
Node - & gt; Coefficient=q - & gt; Coefficient;
Node - & gt; Exponent=q - & gt; Exponent;
Node - & gt; Next=0;
p-> Next=node;
P=p - & gt; next;
Q=q - & gt; next;
}
Length=old. Length;
}
}
Polynomial: : ~ Polynomial ()
{
Node * p=first;
While (first)
{
P=first - & gt; next;
Free (first);
The first=p;
}
Length=0;
}
Polynomial Polynomial: : operator + (Polynomial & amp; A)
{
Polynomial temp=(* this);
The Node * p=A.f irst.
For (int I=0; I{
Temp. Add (p - & gt; Coefficient, p - & gt; Exponent);
P=p - & gt; next;
}
Return temp.
}
Polynomial Polynomial: : operator - (Polynomial & amp; A)
{
Polynomial temp=(* this);
The Node * p=A.f irst.
for(int i=0; I{
Temp. Add ((1) * p - & gt; Coefficient, p - & gt; Exponent);
P=p - & gt; next;
}
Return temp.
}
Polynomial Polynomial: : operator * (Polynomial & amp; A)
{
Polynomial ret.
The Node * p=A.f irst.
For (int I=0; I{
Polynomial temp=(* this);
Temp. Multiply (p - & gt; Coefficient, p - & gt; Exponent);
P=p - & gt; next;
Ret=ret + temp;
}
Return ret.
}
Void Polynomial: : get ()
{
Node * p=first;
Cout Coefficient<" X ^ "& lt; Exponent;
P=p - & gt; next;
Char oper.
for(int i=0; I{
Oper=p - & gt; Coefficient> 0? '+', '-';
Cout Coefficient<" X ^ "& lt; Exponent;
P=p - & gt; next;
}

}
Polynomial Polynomial: : operator=(Polynomial & amp; A)
{
If (A.f irst==0)
{
The first=0;
Length=0;
}
The else
{
The Node * q=A.f irst.
Node * Node=new Node;
Node - & gt; Coefficient=q - & gt; Coefficient;
Node - & gt; Exponent=q - & gt; Exponent;
Node - & gt; Next=0;
Q=q - & gt; next;
The first=node;
Node * p=first;
For (int I=0; I{
Node * Node=new Node;
Node - & gt; Coefficient=q - & gt; Coefficient;
Node - & gt; Exponent=q - & gt; Exponent;
Node - & gt; Next=0;
p-> Next=node;
P=p - & gt; next;
Q=q - & gt; next;
}
Length=a. ength;
}
Return * this;
}
Void Polynomial: : add (int a, int b)
{
If (first==0)
{
Node * Node=new Node;
Node - & gt; Coefficient=a;
Node - & gt; Exponent=b;
Node - & gt; Next=0;
The first=node;
Length++;
}
Else if (b> First - & gt; Exponent)
{
Node * Node=new Node;
Node - & gt; Coefficient=a;
Node - & gt; Exponent=b;
Node - & gt; Next=first;
The first=node;
Length++;
}
The else
{
Node * p=first;
for(int i=0; I{
If (p - & gt; Exponent==b)
{
p-> Coefficient +=a;
break;
}
Else if (b Exponent& & p-> Next& & B> p-> next-> Exponent)
{
Node * Node=new Node;
Node - & gt; Coefficient=a;
Node - & gt; Exponent=b;
Node - & gt; Next=p - & gt; next;
p-> nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related