#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct POLYNODE
{
int Coef; coefficients of the polynomial
int exp; Index
struct Polynode *next;
}node;
Node *create ()//using the tail interpolation method to establish a linked list of unary polynomial
{
Node *h,*r,*s;
int c,e;
H= (node*) malloc (sizeof (node));
R=h;
printf ("Coef:");
scanf ("%d", &c);
printf ("Exp:");
scanf ("%d", &e);
while (c!=0)//input factor is 0 o'clock, the input of the polynomial ends
{
S= (node*) malloc (sizeof (node));
s->coef=c;
s->exp=e;
r->next=s;
R=s;
printf ("Coef:");
scanf ("%d", &c);
printf ("Exp:");
scanf ("%d", &e);
}
r->next=null;
return (h);
}
void Print (node *p)//Output function, printing out a unary polynomial
{
while (P->next!=null)
{
p=p->next;
printf ("%d*x^%d", p->coef,p->exp);
}
}
Void Polyadd (node *ha, node *HB)//Unary polynomial addition function for adding two polynomial, then storing and polynomial in the polynomial ha, and deleting the polynomial HB
{
Node *p,*q,*pre,*temp;
int sum;
p=ha->next;
q=hb->next;
Pre=ha;
while (p!=null&&q!=null)
{
if (p->exp<q->exp)
{
pre->next=p;
pre=pre->next;
p=p->next;
}
Else if (p->exp==q->exp)
{
sum=p->coef+q->coef;
if (sum!=0)
{
p->coef=sum;
pre->next=p;pre=pre->next;p=p->next;
Temp=q;q=q->next;free (temp);
}
Else/If the coefficients and zeros are removed, the node p and Q are deleted, and the pointer points to the next node
{
Temp=p->next;free (p);p =temp;
Temp=q->next;free (q); q=temp;
}
}
Else
{
pre->next=q;
pre=pre->next;
q=q->next;
}
}
if (p!=null)//Add the remaining nodes in the polynomial A to the and polynomial
pre->next=p;
Else
pre->next=q;
}
void Multipoly (node *ha,node *HB)
{node *p,*q,*n,*m;
p=ha->next;
N= (node*) malloc (sizeof (node));
n->next=null;
while (P!=null)
{m= (node*) malloc (sizeof (node));
for (Q=hb->next;q;q=q->next)
{m->coef=p->coef*q->coef;
m->exp=p->exp+q->exp;
m->next=null;
}
p=p->next;
Polyadd (N,M);
}
printf (the product of the polynomial is:/n);
printf ("/n");
}
void Main ()
{
Node *HA,*HB;
printf ("Please enter the coefficients of the polynomial ha with the exponent:/n");
Ha=create ();
Print (HA);
printf ("/n");
printf ("Please enter the coefficient of the polynomial HB with the exponent:/n");
Hb=create ();
Print (HB);
printf ("/n");
printf ("and is:/n" for polynomial);
Polyadd (HA,HB);
Print (HA);
printf ("/n");
Multipoly (HA,HB);
}