#include
using namespace std;
Struct term//define a structure
{
Int coef.//coefficient
Int exp.//index
Friend ostream & amp; The operator & lt; <(ostream & amp; Out, term & amp; M)
{out
{in> M.c oef> M.e xp; Return in; }
Term & amp; Operator=(term & amp; T)
{coef=tc oef. Exp=t.e xp; Return * this; }
};
Struct Node//define a Node
{
The term poly real;
Node * next;
};
The class Polyomical
{
Private:
Int length;
Node * head;
Public:
Polyomical ()
{the head=new Node; The head - & gt; Next=NULL; Length=0; }
Bool Insert (term & amp; t);
Void MakeEmpty ();
Polyomical & amp; Copy (Polyomical & amp; P);
Polyomical & amp; Operator=(Polyomical & amp; P);
Polyomical Delete ();
Friend Polyomical operator + (Polyomical & amp; P1, Polyomical & amp; P2);
Friend Polyomical operator - (Polyomical & amp; P1, Polyomical & amp; P2);
Friend ostream & amp; The operator & lt; <(ostream & amp; , Polyomical & amp;);
};
Void Polyomical: : MakeEmpty ()
{
Node * p;
P=the head;
While (the head)
{
The head=p - & gt; Next;
The delete p;
P=the head;
}
Length=0;
}
Ostream & amp; Operator<(ostream & amp; Out, Polyomical & amp; P)
{
Node * m=p.h ead;
M=m - & gt; Next;
While (m)
{
If (m!=p.h ead - & gt; Next& & M - & gt; Poly real. Coef> 0)
Out<'+' & lt;
Out
Out
}
Polyomical Polyomical: : Delete ()//Delete the coefficient is zero item
{
The Node * p, * q;
P=the head;
While (p - & gt; Next!=NULL)
{
Q=p;
P=p - & gt; Next;
If (p - & gt; Poly real. Coef==0)
{
Q - & gt; Next=p - & gt; Next;
The delete p;
P=q;
}
}
Return * this;
}
Bool Polyomical: : Insert (term & amp; T)//the number of according to the index from small to large insert the polynomial list
{
if(! The head - & gt; Next)//when the list is empty operation
{
Node * p=new Node;
Assert (p);
If (p==NULL)
return false;
p-> Next=head - & gt; Next;
P=the head - & gt; Next;
p-> Poly real=t;
}
The else//when the list is not empty
{
The Node * p=head - & gt; Next;
For (; p-> Poly real. Exp
If (p - & gt; Poly real. J exp==t.e xp)
p-> Poly real. Coef=p - & gt; Poly real. Coef + tc oef.
The else
{
Node * q=new Node;
Assert (q);
If (q==NULL)
return false;
Q - & gt; Next=p - & gt; Next;
Q - & gt; Poly real=t;
}
}
Length++;
return true;
}
Polyomical & amp; Polyomical: : Copy (Polyomical & amp; M)
{
The Node * p * q * l;
Length=m.l ength;
The head=NULL;
if(! M.h ead)
Return * this;
The head=new Node;
if(! The head)
Return * this;
The head - & gt; Next=NULL;
P=the head;
Q=m.h ead - & gt; Next;
While (q)
{
Node * l=new Node;
if(! L)
Return * this;
L - & gt; Poly real=q - & gt; Poly real;
L - & gt; Next=NULL;
p-> Next=l;
P=p - & gt; Next;
Q=q - & gt; Next;
}
Return * this;
}
Polyomical & amp; Polyomical: : operator=(Polyomical & amp; P)//assignment operations
{
if(! The head)
MakeEmpty ();
Copy (p);
Return * this;
}
Polyomical operator + (Polyomical & amp; P1, Polyomical & amp; P2)//addition operation
{
Polyomical p3;
The Node * p, * q;
P=p1. Head;
Q=p2. The head;
While (q - & gt; Next!=NULL& & p-> Next!=NULL)
{
If (q - & gt; Poly real. Exp
{
P3. Insert (q - & gt; Poly real);
Q=q - & gt; Next;
}
Else if (q - & gt; Poly real. Exp> p-> Poly real. J exp)
{
P3. Insert (p - & gt; Poly real);
P=p - & gt; Next;
}
The else
{
The term m;
M.c oef=p - & gt; Poly real. Coef + q - & gt; Poly real. Coef;
M.e xp=p - & gt; Poly real. Exp;
P3. Insert (m);
P=p - & gt; Next;
Q=q - & gt; Next;
}
}
If (p - & gt; Next==NULL)
{
While (q - & gt; Next!=NULL)
{
P3. Insert (q - & gt; Poly real);
Q=q - & gt; Next;
}
}
The else
{
While (p - & gt; Next!=NULL)
{
P3. Insert (p - & gt; Poly real);
P=p - & gt; Next;
}
}
P3. Delete ();
Return p3;
}
Polyomical operator - (Polyomical & amp; P1, Polyomical & amp; P2)//subtraction operation
{
Polyomical p3;
The Node * p, * q;
P=p1. Head;
Q=p2. The head;
While (q - & gt; Next!=NULL& & p-> Next!=NULL)
{
If (q - & gt; Poly real. Exp
{
The term l;
L.c oef=- q - & gt; Poly real. Coef;//there is no corresponding number of items in the minuend plus negative number
L.e xp=q - & gt; Poly real. Exp;
P3. Insert (l);
Q=q - & gt; Next;
}
Else if (q - & gt; Poly real. Exp> p-> Poly real. J exp)
{
P3. Insert (p - & gt; Poly real);
P=p - & gt; Next;
}
The else
{
The term m;
M.c oef=p - & gt; Poly real. Coef - q - & gt; Poly real. Coef;
M.e xp=p - & gt; Poly real. Exp;
P3. Insert (m);
P=p - & gt; Next;
Q=q - & gt; Next;
}
}
If (q - & gt; Next==NULL)
{
While (p - & gt; Next!=NULL)
{
P3. Insert (p - & gt; Poly real);
p-> Next;
}
}
The else
{
While (q - & gt; Next!=NULL)
{
The term l;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull