c语言链表实现一元多项式的加减乘运算

#include <stdio.h>
#include <stdlib.h>
#define SHI sizeof(struct duoxiangshi)
#define NULL 0
struct duoxiangshi
{int xishu,zhishu;
struct duoxiangshi*next;
};


struct duoxiangshi*create()
{struct duoxiangshi*head,*p1,*p2;
int n;
n=0;
head=NULL;
p2=NULL;
p1=NULL;
p1=(struct duoxiangshi*)malloc(SHI);
scanf("%d %d",&p1->xishu,&p1->zhishu);
while(p1->xishu!=NULL)
{++n;
if(n==1){head=p1;p2=p1;}
else{p2->next=p1;p2=p1,p2->next=NULL;}
p1=(struct duoxiangshi*)malloc(SHI);
scanf("%d %d",&p1->xishu,&p1->zhishu);
p1->next=NULL;
}
free(p1);
return (head);
}

void paixu(struct duoxiangshi*head)
{struct duoxiangshi*p1,*p2;
int t=0;
p1=head;
p2=head;
for( ; ;p1=p1->next)
{ if(p1->next==NULL)break;
for(p2=p1->next; ;p2=p2->next)
{ if(p1->zhishu>p2->zhishu)
{ t=p1->zhishu;
p1->zhishu=p2->zhishu;
p2->zhishu=t;

t=p1->xishu;
p1->xishu=p2->xishu;
p2->xishu=t;
}

if(p2->next==NULL)break;
}
}
}



struct duoxiangshi*add(struct duoxiangshi*head1,struct duoxiangshi*head2)
{struct duoxiangshi*p1,*p2,*s,*p3,*head3;
int n=1;
p1=head1;
p2=head2;
head3=NULL;
p3=NULL;
while(p1!=NULL&&p2!=NULL)
{s=(struct duoxiangshi*)malloc(SHI);
++n;
if(n==1) head3=s;
if(p1->zhishu>p2->zhishu)
{s->zhishu=p2->zhishu;
s->xishu=p2->xishu;
p2=p2->next;
}
else if(p1->zhishu<p2->zhishu)
{s->zhishu=p1->zhishu;
s->xishu=p1->xishu;
p1=p1->next;
}
else
{s->zhishu=p1->zhishu;
s->xishu=p1->xishu+p2->xishu;
p1=p1->next;
p2=p2->next;
}
p3->next=s;
p3=s;

}
p3->next=NULL;
if(p1!=NULL)
p3->next=p1;
if(p2!=NULL)
p3->next=p2;
return (head3);
}


struct duoxiangshi* minus(struct duoxiangshi*head1,struct duoxiangshi*head2)
{struct duoxiangshi*p2;
p2=head2;
while(p2!=NULL)
{p2->xishu=(-1)*p2->xishu;
p2=p2->next;
}
return (add(head1,head2));
}


struct duoxiangshi* multiply(struct duoxiangshi*px,struct duoxiangshi*py)
{ int n=0,i=0,j=0;
struct duoxiangshi*heada,*headb,*pa,*pb,*lasta,*lastb,*p2,*p1;
p1=heada=headb=pa=pb=lasta=lastb=NULL;
p2=py;

while (p2!=NULL)
{n++;
if(n==1)
{ p1=px;
while(p1!=NULL)
{i++;
pa=(struct duoxiangshi*)malloc(SHI);
if(i==1) heada=pa;
else lasta->next=pa;
lasta=pa;
pa->xishu=p1->xishu*p2->xishu;
pa->zhishu=p1->zhishu+p2->zhishu;
pa->next=NULL;
p1=p1->next;
}
}
else
{ p1=px;
while(p1!=NULL)
{j++;
pb=(struct duoxiangshi*)malloc(SHI);
if(j==1) headb=pb;
else lastb->next=pb;
lastb=pb;
pb->xishu=p1->xishu*p2->xishu;
pb->zhishu=p1->zhishu+p2->zhishu;
pb->next=NULL;
p1=p1->next;
}

}
p2=p2->next;
if(n!=1)heada=add(heada,headb);
}
return(heada);


}


void print (struct duoxiangshi*head3)
{struct duoxiangshi*p3;
p3=head3;

do
{ if(p3->zhishu==0)
{printf("%d",p3->xishu);
p3=p3->next;
continue;
}
else if(p3==head3)
{printf("%dx%d",p3->xishu,p3->zhishu);
p3=p3->next;
continue;
}
else if(p3->xishu<0)
printf("%dx%d",p3->xishu,p3->zhishu);
else if(p3->xishu>0)
printf("+%dx%d",p3->xishu,p3->zhishu);
p3=p3->next;
}while(p3!=NULL);
printf("\n\n");
}



void main()
{struct duoxiangshi*p1,*p2,*p3;
int c=0;char d;
do
{


printf("please input A:\n");
p1=create();
paixu(p1);
printf("\nthe A is:");
print(p1);


printf("please input B:\n");
p2=create();
paixu(p2);
printf("\nthe B is:");
print(p2);

printf("\n please choice :\n");
printf("*********************************\n");
printf("1.add 2.minu 3.multiply\n");
printf("*********************************\n");
scanf("%d",&c);
switch(c)
{case 1:p3=add(p1,p2);break;
case 2:p3=minus(p1,p2);break;
case 3:p3=multiply(p1,p2);break;
default:printf("\nerror");

}
printf("\nthe result is :");
print(p3);
printf("do you still have anything to do?(y/n):\n");
scanf("%c",&d);
getchar();
clrscr();

}while(d!='n'&&d!='N');

}

 

posted @ 2011-11-07 22:11  ☆A希亿  阅读(3187)  评论(0编辑  收藏  举报