Home > Back-end >  Zhejiang university PAT problem sets, found a night brothers come in and see no error in
Zhejiang university PAT problem sets, found a night brothers come in and see no error in

Time:09-23

02 - linear structure 2 one yuan polynomial multiplication and addition operations (20 point (s))
Design function, respectively, and the product of two unary multinomial and,

Input format:
Input in 2 lines, each line respectively polynomial number of nonzero entries are first, then to enter a polynomial index degradation way non-zero coefficient and index (integer) of absolute value is less than 1000, separated by Spaces between Numbers,

The output format:
Output in 2 lines, with index degradation product of output polynomial and polynomial non-zero coefficient and index, separated by Spaces between Numbers, but the end can not have extra Spaces, zero polynomial should be 0 0,

Input the sample:
4-5 4 3 2 1 6-2 0
3 5 20-7 4 3 1



The output sample:
15 to 24 and 25 30 21-22 10 20-21 August 6 to 35 33 14 4-15 18 3 2-6 1
5 20-4 of 4-5 2 9 1-2 0















 # include & lt; Stdio. H> 
# include & lt; stdlib.h>
Typedef struct PolyNode * Polynomial;
Struct PolyNode {
Int coef.
Int expon;
Polynomial link;
};
Polynomial readpoly ();
Void the Attach (int int a, c, e, e, Polynomial * prear);//in the end of the newly added a
Polynomial to the Add (Polynomial p1, Polynomial p2);//add
Polynomial Mult (Polynomial p1, Polynomial p2);//the multiplication
Void Printpoly (Polynomial p);//output function
Int main ()
{
Polynomial P1, P2, PP, PS;
P1=readpoly ();
//Printpoly (P1);
The P2=readpoly ();
//Printpoly (P2);
PP=Mult (P1, P2);
Printpoly (PP);
printf("\n");
PS=Add (P1, P2);
Printpoly (PS);
return 0;
}

Polynomial readpoly () {//read input Polynomial
Int c=0, e=0, N.
Polynomial p=(Polynomial) malloc (sizeof (struct PolyNode));
P - & gt; The link=NULL;
Polynomial rear=p;
The scanf (" % d ", & amp; N);
While (N -) {
The scanf (" % d % d ", & amp; C., & amp; E);
If (c==0)//determine whether the input coefficient is 0, 0 if skip
continue;
The Attach (c, e, & amp; Rear);
}
Polynomial t=p;
P=p - & gt; The link;
Free (t);
return p;
}
Void the Attach (int int a, c, e, e, Polynomial * prear) {

Polynomial p=(Polynomial) malloc (sizeof (struct PolyNode));
P - & gt; Coef=c;
P - & gt; Expon=e;
P - & gt; The link=NULL;
(* prear) - & gt; The link=p;
* prear=p;
}
Polynomial to the Add (Polynomial Polynomial p1, p2) {
Polynomial t, p, t1=p1, t2=p2, rear;
P=(Polynomial) malloc (sizeof (struct PolyNode));
Rear=p;
T=p;
While (t1 & amp; & T2) {
If (t1 - & gt; Expon==t2 - & gt; Expon) {
If (t1 - & gt; Coef + t2 - & gt; Coef) {//whether the combined coefficient of 0

The Attach (t1 - & gt; Coef + t2 - & gt; Coef, t1 - & gt; Expon, & amp; Rear);
T1=t1 - & gt; The link;
T2=t2 - & gt; The link; }
The else {
T1=t1 - & gt; The link;
T2=t2 - & gt; The link;
}
}
Else if (t1 - & gt; Expon> T2 - & gt; Expon) {
The Attach (t1 - & gt; Coef, t1 - & gt; Expon, & amp; Rear);
T1=t1 - & gt; The link;
}
The else {
The Attach (t2 - & gt; Coef, t2 - & gt; Expon, & amp; Rear);
T2=t2 - & gt; The link;
}
}
If (t1) {
Rear - & gt; The link=t1;
}
If (t2) {
Rear - & gt; The link=t2;
}
P=p - & gt; The link;
Free (t);
return p;
}
Polynomial Mult (Polynomial Polynomial p1, p2) {
Polynomial t, p, t1=p1, t2=p2, rear;
Int c, e,
P=(Polynomial) malloc (sizeof (struct PolyNode));
Rear=p;
T=p;
While (t1==NULL | | t2==NULL)
return NULL;
While (t2) {
The Attach (t1 - & gt; Coef * t2 - & gt; Coef, t1 - & gt; Expon + t2 - & gt; Expon, & amp; Rear);
T2=t2 - & gt; The link;
}
T1=t1 - & gt; The link;
While (t1) {
Rear=p;
T2=p2;
While (t2) {
C=t1 - & gt; Coef * t2 - & gt; Coef.
E=t1 - & gt; Expon + t2 - & gt; Expon;
While (rear - & gt; Link& & Rear - & gt; The link - & gt; Expon> E) {
Rear=rear - & gt; The link;
}
If (rear - & gt; Link& & Rear - & gt; The link - & gt; Expon==e) {
If (rear - & gt; The link - & gt; Coef + c) {//whether the number is 0, 0, delete this item
Rear - & gt; The link - & gt; Coef +=c;
}
The else {
T=rear - & gt; The link;
Rear - & gt; The link=t - & gt; The link;
Free (t);
}
}
The else {
T=(Polynomial) malloc (sizeof (struct PolyNode));
T - & gt; Coef=c;
T - & gt; Expon=e;
T - & gt; The link=rear - & gt; The link;
Rear - & gt; The link=t;
}
T2=t2 - & gt; The link;
}
T1=t1 - & gt; The link;
}
T=p; P=p - & gt; The link; Free (t);
return p;
}
Void Printpoly (Polynomial p) {
Int flag=0;
If (p==NULL) {
Printf (" % d % d ", 0, 0).
return;
}
While (p) {
If (flag==0) {
Flag=1;
}
The else {
Printf (" ");
}
Printf (" % d % d ", p - & gt; Coef, p - & gt; Expon);
P=p - & gt; The link;
}
return;
}

CodePudding user response:

The old man to help look at it
  • Related