#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int p;
int e;
struct node *next;
}Polynomial,*P_Polynomial;
P_Polynomial Input()
{//輸入多項式
P_Polynomial p1,p2,head;
head=p2=(P_Polynomial)malloc(sizeof(Polynomial));
p1=(P_Polynomial)malloc(sizeof(Polynomial));
printf("\n請輸入多項式的系數和冪值\n(注:以冪值為0結束)\n");
scanf("%d %d",&p1->p,&p1->e);
while(p1->e!=0)
{
p2->next=p1;
p2=p1;
p1=(P_Polynomial)malloc(sizeof(Polynomial));
scanf("%d %d",&p1->p,&p1->e);
}
p2->next=NULL;
free(p1);
return head;
}
void Add_polynomial(P_Polynomial p1,P_Polynomial p2)
{//多項式相加
P_Polynomial pre_p1,cur_p1,cur_p2;
pre_p1=p1;//p1的前指針
cur_p1=p1->next;
cur_p1=p2->next;// p1和p2的當前指針
while(cur_p1&&cur_p2)
{
if(cur_p1->e==cur_p2->e)// 如果p1和p2的冪相等,則相加並入p1
{
cur_p1->p=cur_p1->p+cur_p2->p;
if(cur_p1->p==0)//if p==0
{
pre_p1->next=cur_p1->next;
free(cur_p1);
cur_p1=pre_p1->next;
cur_p2=cur_p2->next;
}
else// if p!=0
{
pre_p1=cur_p1;
cur_p1=cur_p1->next;
cur_p2=cur_p2->next;
}
}
continue;//continue find...
if(cur_p1->e<cur_p2->e)
{
pre_p1=cur_p1;
cur_p1=cur_p1->next;
}
else
{
P_Polynomial temp;
if(!(temp=(P_Polynomial)malloc(sizeof(Polynomial))))
{
printf("內存不足。。。");
exit(0);
}
temp->p=cur_p2->p;
temp->e=cur_p2->e;
temp->next=cur_p1;
pre_p1->next=temp;
pre_p1=pre_p1->next;
cur_p2=cur_p2->next;//在p1中插入p2
}
}
while(cur_p2)
{
P_Polynomial temp;
if(!(temp=(P_Polynomial)malloc(sizeof(Polynomial))))
{
printf("內存不足。。。");
exit(0);
}
temp->p=cur_p2->p;
temp->e=cur_p2->e;
temp->next=NULL;
pre_p1->next=temp;
pre_p1=pre_p1->next;
cur_p2=cur_p2->next;
}
}
P_Polynomial Mul_polynomial(P_Polynomial *exp1,P_Polynomial *exp2)
{//多項式相乘
P_Polynomial head,p1,p2,last,pre,px,q;
int flag=1;
head=p1=*exp1;
p1=p1->next;
for(;p1->next!=NULL;p1=p1->next);
px=last=p1;
p1=(*exp1)->next;
while(p1!=px->next)
{
pre=p1;
flag=1;
p2=(*exp2)->next;
while(p2)
{
if(flag)
{
p1->p=p1->p*p2->p;
p1->e=p1->e+p2->e;
p2=p2->next;
flag=0;
}
else
{
q=(P_Polynomial)malloc(sizeof(Polynomial));
q->p=pre->p*p2->p;
q->e=pre->e+p2->e;
last->next=q;
last=q;
p2=p2->next;
}
}
p1=p1->next;
last->next=NULL;
}
return head;
}
P_Polynomial Sort_polynomial(P_Polynomial pol)
{//對多項式排序,
P_Polynomial head=pol,p,q,r,t;
if(head->next==NULL)
printf("input error!!! ");
else
{
p=head->next;
q=p->next;
p->next=NULL;
p=q;
}
while(p!=NULL)
{
r = head;
q = r->next;
while(q != NULL&& q->e < p->e)
{
r = q;
q = q->next;
}
t=p->next;
p->next=r->next;
r->next=p;
p=t;
}
return(head);
}
P_Polynomial Combine_polynomial(P_Polynomial head)
{//對指數相同的多項式進行合並。。
P_Polynomial p1,p2;
p1=head->next;
while(p1->next!=NULL)
{
if(p1->next->e==p1->e)
{
p2=p1->next;
p1->e=p1->e+p2->e;
p1->next=p1->next->next;
free(p2);
}
else
{
p1=p1->next;
}
}
return head;
}
void output(P_Polynomial head)
{
P_Polynomial p1;
p1=head->next;
while(p1!=NULL)
{
if(p1->e<0&&p1!=NULL)
printf("\b\b");
printf("%dX(%d)+",p1->p,p1->e);
p1=p1->next;
}
printf("\b");
printf("\n");
}
int main()
{
P_Polynomial p1,p2,add,mul;
printf("please input p1:\n");
p1=Input();
printf("please input p2:\n");
p2=Input();
// printf("p1+p2=");
// add=Add_polynomial(p1,p2);
// output(add);n
printf("p1*p2=");
mul=Mul_polynomial(&p1,&p2);
Sort_polynomial(mul);
Combine_polynomial(mul);
output(mul);
return 0;
}