For one yuan polynomial p (x)=p0 + p1x + p2x2 +... + PNXN, each item has a coefficient and index of two parts, such as the coefficient of p2 p2x2, index of 2,
Programming to realize two polynomial addition,
Such as 5 + x + 2 x2 + 3 x3, - 5-6 x2 + x + 4 x4, combined results: 8 x2 + 3 x3, 4 x4
Coefficient and 5-5 of them are the 0 x to the power factor, combined to 0, so don't show, x 1 power similarly does not display,
Asked to use singly linked lists,
Case input:
4
5 0
1 1
2 2
3 3
4
- 5 0
1 1
6 2
4 4
Output:
5 + 1 x ^ 1 ^ 2 + 2 x + 3 x ^ 3
(5) + (1) x 1 + 6 x ^ 2 + 4 ^ ^ 4 x
8 x ^ 2 + 3 x ^ 3 + 4 x ^ 4
# include & lt; Iostream>
Using namespace STD.
The class CPloy {
Public:
Int, E factor;
CPloy * next;
CPloy () {next=NULL; }
};
The class CLinkPloy {
Public:
CPloy * head;
int len;
CLinkPloy ();
~ CLinkPloy ();
Void init (int n);
Void the print ();
Void LMerge (CLinkPloy & amp; Cl);
};
CLinkPloy: : CLinkPloy () {
The head=new CPloy;
Len=0;
}
CLinkPloy: : ~ CLinkPloy () {
CPloy * p=head, * q;
While (p) {
Q=p;
P=p - & gt; Next;
The delete q;
}
The head=NULL;
Len=0;
}
Void CLinkPloy: : init (int n) {
Int f, e;
Len=n;
CPloy * p=head;
for(int i=0; ICPloy * q=new CPloy;
Cin> F> e;
Q - & gt; E=E.
Q - & gt; Factor=f;
P - & gt; Next=q;
P=q;
}
}
Void CLinkPloy: : print () {
CPloy * p=head - & gt; Next;
If (p) {
If (p - & gt; Factor & gt; 0 & amp; & P - & gt; E==0)
CoutElse if (p - & gt; Factor & lt; 0 & amp; & P - & gt; E==0)
Cout<& lt;" (" & lt; Else if (p - & gt; Factor & gt; 0 & amp; & P - & gt; E> 0)
CoutElse if (p - & gt; Factor & lt; 0 & amp; & P - & gt; E> 0)
CoutElse if (p - & gt; Factor & gt; 0 & amp; & P - & gt; E<0)
CoutElse if (p - & gt; Factor & lt; 0 & amp; & P - & gt; E<0)
Cout<& lt;" (" & lt; P=p - & gt; Next;
}
While (p) {
If (p - & gt; Factor & gt; 0 & amp; & P - & gt; E> 0)
Cout<& lt;" + "& lt; Else if (p - & gt; Factor & lt; 0 & amp; & P - & gt; E> 0)
Cout<& lt;" + (" & lt; Else if (p - & gt; Factor & gt; 0 & amp; & P - & gt; E==0)
Cout<& lt;" + "& lt; Else if (p - & gt; Factor & lt; 0 & amp; & P - & gt; E==0)
Cout<& lt;" + (" & lt; Else if (p - & gt; Factor & gt; 0 & amp; & P - & gt; E<0)
Cout<& lt;" + "& lt; Else if (p - & gt; Factor & lt; 0 & amp; & P - & gt; E<0)
Cout<& lt;" + (" & lt; P=p - & gt; Next;
}
Cout}
Void CLinkPloy: : LMerge (CLinkPloy & amp; Cl) {
CPloy * p=head - & gt; Next, * q=cl. Head - & gt; Next;
CPloy * r=head;
//CPloy * s=new CPloy;
While (p& & Q) {
CPloy * s=new CPloy;
If (p - & gt; E==q - & gt; E) {
S - & gt; E=p - & gt; E;
S - & gt; Factor=p - & gt; Factor + q - & gt; Factor;
If (p - & gt; Factor + q - & gt; Factor!=0) {
R - & gt; Next=s;
R=s;
}
P=p - & gt; Next;
Q=q - & gt; Next;
} else if (p - & gt; E & lt; Q - & gt; E) {
S - & gt; E=p - & gt; E;
S - & gt; Factor=p - & gt; Factor;
R - & gt; Next=s;
R=s;
P=p - & gt; Next;
} else if (p - & gt; E & gt; Q - & gt; E) {
S - & gt; E=q - & gt; E;
S - & gt; Factor=q - & gt; Factor;
R - & gt; Next=s;
R=s;
Q=q - & gt; Next;
}
}
R - & gt; Next=p? P: q;
}
Int main () {
Int t, n, m;
Cin> t;
While (t -) {
CLinkPloy cl1, cl2;
Cin> n;
Cl1. Init (n);
Cl1. Print ();
Cin> m;
Cl2. Init (m);
Cl2. Print ();
Cl1. LMerge (cl2);
Cl1. Print ();
}
return 0;
}