#include <stdio.h>
#include <stdlib.h>
/读入的话才用尾插法,头部保留空结点,插入读入完成后释放头部结点/
/主要结构是:读入,相加(判别相加是否系数为零),相乘(第一项先乘,后续在此基础上按指数大小顺序插入,如果指数大小一样系数相加为零,删除该点/
typedef struct PSTNODE * polynomial;
struct PSTNODE
{
int coef;
int expon;
polynomial pnext;
};
typedef struct PSTNODE node;
polynomial read_poly(void);
polynomial poly_mult(polynomial T1, polynomial T2);
polynomial poly_add(polynomial T1, polynomial T2);
void print_poly(polynomial p);
void attach(int c, int e, polynomial * prear);
int compare(int e1, int e2);
int main(void)
{
polynomial p1, p2, pp, ps;
p1 = read_poly();
p2 = read_poly();
pp = poly_mult(p1, p2);
print_poly(pp);
ps = poly_add(p1, p2);
print_poly(ps);
}
polynomial read_poly(void)
{
polynomial front, rear;
front = (polynomial)malloc(sizeof(node));
rear = front;
rear->pnext = NULL;
int n, i,e, c;
scanf("%d", &n);
while(n–)
{
scanf("%d %d", &c, &e);
if (c != 0)
attach(c, e, &rear);
}
polynomial tmp = front;
front=front->pnext;
free(tmp);
tmp = NULL;
return front;
}
void attach(int c, int e, polynomial * prear)
{
polynomial p = (polynomial) malloc(sizeof(node));
p->coef = c;
p->expon = e;
p->pnext = NULL;
(*prear)->pnext = p;
(*prear) = p;
}
void print_poly(polynomial p)
{
int flag = 0;
polynomial trav = p;
if(!trav)
{
printf(“0 0\n”);
return;
}
while(trav)
{
if (!flag)
{
flag = 1;
}
else
{
printf(" ");
}
printf("%d %d", trav->coef, trav->expon);
trav=trav->pnext;
}
printf("\n");
return ;
}
polynomial poly_mult(polynomial T1, polynomial T2)
{
polynomial p1, p2, tmp;
p1 = T1;
p2 = T2;
int c, e;
if(!p1 || !p2)
{
return NULL;
}
polynomial front = (polynomial) malloc(sizeof(node));
polynomial rear = front;
rear->pnext = NULL;
while (p2)
{
//p1的第一项与p2的每一项相乘;
attach( p1->coef*p2->coef, p1->expon + p2->expon ,&rear);
p2 = p2->pnext;
}
p1 = p1->pnext;
while (p1)
{
p2 = T2;
rear = front;
while (p2)
{
c = p1->coef * p2->coef;
e = p1->expon + p2->expon;
while (rear->pnext && rear->pnext ->expon > e)
{
rear = rear->pnext;
}
if (rear->pnext && rear->pnext->expon == e)
{
if (rear->pnext->coef + c == 0)
{
tmp = rear->pnext;
rear->pnext = tmp ->pnext;
free(tmp);
}
else
{
rear->pnext->coef = rear->pnext->coef + c;
}
}
else
{
//注意这一步是插入, 不是add,注意不要搞混;
tmp = (polynomial) malloc(sizeof(node));
tmp->coef = c;
tmp->expon = e;
tmp->pnext = rear->pnext;
rear->pnext = tmp;
rear = rear->pnext;
}
p2 = p2->pnext;
}
p1= p1->pnext;
}
tmp = front;
front = front->pnext;
free(tmp);
return front;
}
int compare(int e1, int e2)
{
if(e1>e2)
{
return 0;
}
else if (e1<e2)
{
return -1;
}
else
{
return 1;
}
}
polynomial poly_add(polynomial T1, polynomial T2)
{
int c1, c2;
polynomial front, rear, tmp;
polynomial p1, p2;
p1 = T1;
p2 = T2;
front = (polynomial)malloc(sizeof(node));
rear = front;
int sum;
rear->pnext = NULL;
while (p1 && p2)
{
switch (compare(p1->expon, p2->expon))
{
case 1:
sum=p1->coef + p2->coef;
if(sum)
{
attach(sum, p1->expon, &rear);
}
p1=p1->pnext;
p2=p2->pnext;
break;
case 0:
attach(p1->coef, p1->expon, &rear);
p1=p1->pnext;
break;
case -1:
attach(p2->coef, p2->expon, &rear);
p2=p2->pnext;
break;
}
}
for (; p1; p1= p1->pnext)
{
attach(p1->coef, p1->expon, &rear);
}
for (; p2; p2= p2->pnext)
{
attach(p2->coef, p2->expon, &rear);
}
rear->pnext = NULL; //这部其实没必要;
tmp = front;
front = front->pnext;
free(tmp);
tmp = NULL;
return front;
}