如圖所示:
#include
#include
typedef int datatype;
typedef struct linklist
{
datatype basenum;
datatype exponentnum;
struct linklist *next;
} node;
/*初始化帶頭結點的單鏈表*/
node *init()
{
node *head;
head=(node *)malloc(sizeof(node));
return head;
}
void inputAB(node *headA,node *headB)
{
int x,i;
node *p,*tailA,*tailB;
/*inputA*/
tailA=headA;
printf("請輸入多項式A(以係數(空格)次數形式輸入,—1111結束輸入):");
i=0;
scanf("%d",&x);
while(x!=-1111)
{
if(i%2==0)
{
p=(node *)malloc(sizeof(node));
p->basenum=x;
}
if(i%2!=0)p->exponentnum=x;
tailA->next=p;
tailA=p;
i++;
scanf("%d",&x);
}
tailA->next=NULL;
/*inputB*/
tailB=headB;
printf("請輸入多項式B(以係數(空格)次數形式輸入,—1111結束輸入):");
i=0;
scanf("%d",&x);
while(x!=-1111)
{
if(i%2==0)
{
p=(node *)malloc(sizeof(node));
p->basenum=x;
}
if(i%2!=0)p->exponentnum=x;
tailB->next=p;
tailB=p;
i++;
scanf("%d",&x);
}
tailB->next=NULL;
}
int linkAB(node *headA,node *headB,node *headC)
{
node *p,*q,*pC,*tailC;
tailC=headC;
int a,tempb,tempe;
/*對A的指數重小到大排序,僅用於輸出顯示測試用*/
for(p=headA->next; p!=NULL; p=p->next)
{
for(q=p->next; q!=NULL; q=q->next)
{
if(p->exponentnum>q->exponentnum)
{
tempb=q->basenum;
tempe=q->exponentnum;
q->basenum=p->basenum;
q->exponentnum=p->exponentnum;
p->basenum=tempb;
p->exponentnum=tempe;
}
}
}
/*對B的指數重小到大排序*/
for(p=headB->next; p!=NULL; p=p->next)
{
for(q=p->next; q!=NULL; q=q->next)
{
if(p->exponentnum>q->exponentnum)
{
tempb=q->basenum;
tempe=q->exponentnum;
q->basenum=p->basenum;
q->exponentnum=p->exponentnum;
p->basenum=tempb;
p->exponentnum=tempe;
}
}
}
printf("按次數從小到達排序後的AB分別爲:");
for(p=headA->next,q=headB->next; p!=NULL&&q!=NULL; )
{
if(p->exponentnum==q->exponentnum)
{
a=p->basenum+q->basenum;
if(a!=0)
{
pC=(node *)malloc(sizeof(node));
pC->basenum=a;
pC->exponentnum=q->exponentnum;
p=p->next;
q=q->next;
}
else{
p=p->next;
q=q->next;
}
}
else if(p->exponentnumexponentnum)
{
pC=(node *)malloc(sizeof(node));
pC->basenum=p->basenum;
pC->exponentnum=p->exponentnum;
p=p->next;
}
else
{
pC=(node *)malloc(sizeof(node));
pC->basenum=q->basenum;
pC->exponentnum=q->exponentnum;
q=q->next;
}
tailC->next=pC;
tailC=pC;
}
for(p;p!=NULL;p=p->next)
{
pC=(node *)malloc(sizeof(node));
pC->basenum=p->basenum;
pC->exponentnum=p->exponentnum;
tailC->next=pC;
tailC=pC;
}
for(q;q!=NULL;q=q->next)
{
pC=(node *)malloc(sizeof(node));
pC->basenum=q->basenum;
pC->exponentnum=q->exponentnum;
tailC->next=pC;
tailC=pC;
}
tailC->next=NULL;
return 1;
}
void display(node *headA,node *headB,node *headC,int sign)
{
node *pA,*pB,*pC;
/*打印A*/
pA=headA->next;
if(pA==NULL)printf("\nA是空的!");
else
{
printf("\nA(x)=");
while(pA!=NULL)
{
printf("%dx^(%d)",pA->basenum,pA->exponentnum);
if(pA->next!=NULL&&pA->next->basenum>0)printf("+");
pA=pA->next;
}
// printf("\n");
}
/*打印B*/
pB=headB->next;
if(pB==NULL)printf("\nB是空的!");
else
{
printf("\nB(x)=");
while(pB!=NULL)
{
printf("%dx^(%d)",pB->basenum,pB->exponentnum);
if(pB->next!=NULL&&pB->next->basenum>0)printf("+");
pB=pB->next;
}
printf("\n");
}
switch(sign)
{
case 0:
break;
default:
/*打印C*/
pC=headC->next;
if(pC==NULL)printf("\nC是空的!");
else
{
printf("多項式相加後爲:");
printf("\nC(x)=");
while(pC!=NULL)
{
printf("%dx^(%d)",pC->basenum,pC->exponentnum);
if(pC->next!=NULL&&pC->next->basenum>0)printf("+");
pC=pC->next;
}
printf("\n");
}
break;
}
}
int main()
{
int sign=0;
node *headA,*headB,*headC;
headA=init();
headB=init();
headC=init();
inputAB(headA,headB);
display(headA,headB,headC,sign);
sign=linkAB(headA,headB,headC);
display(headA,headB,headC,sign);
return 0;
}