Home > Back-end >  One yuan polynomial addition, help to modify it
One yuan polynomial addition, help to modify it

Time:10-03

# 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));

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));

}



  • Related