# include & lt; stdlib.h>
Typedef struct Polynode
{
Int coef, exp;
Struct Polynode * next;
} Polynode, * Polylist;
Void polycreate (Polylist & amp; The head)
{
Polylist rear, s; Int c, e,
Head=(Polynode *) malloc (sizeof (Polynode));
Rear=head;
The scanf (" % d % d ", & amp; C., & amp; E);
While (c!=0) {
S=(Polynode *) malloc (sizeof (Polynode));
S - & gt; Coef=c; S - & gt; Exp=e;
Rear - & gt; Next=s; Rear=s;
The scanf (" % d % d ", & amp; C., & amp; E);
}
Rear - & gt; Next=NULL;
}
Polyadd (Polylist polya, Polylist polyb)
{
Pb Polynode * pa, * and * PC, * r;
Int sum;
Pa=polya - & gt; Next;
Pb=polya - & gt; Next;
PC=polya;//* point pre and polynomial tail node
While (pa!=NULL& & Pb!=NULL)
{if (pa - & gt; Exp
PC - & gt; Next=pa; PC=pa; Pa=pa - & gt; Next; }
Else if (pa - & gt; Exp==pb - & gt; Exp)
{
Sum=pa - & gt; Coef + pb - & gt; Coef.
If (sum!=0)
{
Pa - & gt; Coef=sum; PC - & gt; Next=pa;
PC=pa; Pa=pa - & gt; Next;
R=pb; Pb=pb - & gt; Next; Free (r);
}
The else
{
R=pa; Pa=pa - & gt; Next; Free (r);
R=pb; Pb=pb - & gt; Next; Free (r);
}
} the else
{PC - & gt; Next=pb; PC=pb; Pb=pb - & gt; Next; }
}
If (pa!=NULL) PC - & gt; Next=pa;
The else PC - & gt; Next=pb;
}
Void PrintPoly (Polylist p)
{
Polylist s;
S=p - & gt; Next;
While (s)
{
Printf (" % 0.2 f ^ x % d ", s - & gt; Coef, s - & gt; Exp);
S=s - & gt; Next;
If (s!=NULL)
If (s - & gt; Coef>=0) printf (" + ");
}
printf("\n");
}
Int main ()
{
Polylist polya, polyb;
Polycreate (polya);
Polycreate (polyb);
PrintPoly (polyadd (polya, polyb));
}
CodePudding user response:
# include & lt; Stdio. H>
# include & lt; stdlib.h>
Typedef struct Polynode
{
Int coef, exp;
Struct Polynode * next;
} Polynode, * Polylist;
Void polycreate (Polylist & amp; The head)
{
Polylist rear, s; Int c, e,
Head=(Polynode *) malloc (sizeof (Polynode));
if (! The head)
The exit (0);
Rear=head;
The scanf (" % d % d ", & amp; C., & amp; E);
While (c!=0) {
S=(Polynode *) malloc (sizeof (Polynode));
if (! S)
The exit (0);
S - & gt; Coef=c;
S - & gt; Exp=e;
Rear - & gt; Next=s;
Rear=s;
The scanf (" % d % d ", & amp; C., & amp; E);
}
Rear - & gt; Next=NULL;
}
//polyadd (Polylist polya, Polylist polyb)
Polylist polyadd (Polylist polya, Polylist polyb)
{
PC Polynode * pa, pb, * and * r * polyc;
Int sum;
Polyc=(Polylist) malloc (sizeof (Polynode));
if (! Polyc)
The exit (0);
Pa=polya - & gt; Next;
//pb=polya - & gt; Next;
Pb=polyb - & gt; Next;
PC=polyc;//* point pre and polynomial tail node
While (pa!=NULL& & Pb!=NULL)
{
If (pa - & gt; Exp{
PC - & gt; Next=pa; PC=pa; Pa=pa - & gt; Next; }
Else if (pa - & gt; Exp==pb - & gt; Exp)
{
Sum=pa - & gt; Coef + pb - & gt; Coef.
If (sum!=0)
{
Pa - & gt; Coef=sum; PC - & gt; Next=pa;
PC=pa; Pa=pa - & gt; Next;
R=pb; Pb=pb - & gt; Next; Free (r);
}
The else
{
R=pa; Pa=pa - & gt; Next; Free (r);
R=pb; Pb=pb - & gt; Next; Free (r);
}
} the else
{PC - & gt; Next=pb; PC=pb; Pb=pb - & gt; Next; }
}
If (pa!=NULL) PC - & gt; Next=pa;
The else PC - & gt; Next=pb;
Return polyc;
}
Void PrintPoly (Polylist p)
{
Polylist s;
S=p - & gt; Next;
While (s)
{
//printf (" % 0.2 f ^ x % d ", s - & gt; Coef, s - & gt; Exp);
X ^ printf (" % d % d ", s - & gt; Coef, s - & gt; Exp);
S=s - & gt; Next;
If (s!=NULL)
If (s - & gt; Coef>=0) printf (" + ");
}
printf("\n");
}
Int main ()
{
Polylist polya, polyb;
Polycreate (polya);
Polycreate (polyb);
PrintPoly (polyadd (polya, polyb));
}
For your reference ~
CodePudding user response:
The