欢迎来到天天文库
浏览记录
ID:61426298
大小:53.50 KB
页数:6页
时间:2021-01-29
《数据结构编程——一元稀疏多项式计算器.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、#include#include//节点结构体typedefstructLNode{intxishu;intzhishu;structLNode*next;}LNode,*LinkList;voidxxx(){//第一个多项式intn,x,z,m,xx,zz;intfangfa;scanf("%d%d%d",&n,&m,&fangfa);LinkListhead1;LNode*p,*r;head1=(LinkList)malloc(sizeof(LNode));head1->next=NULL;r=h
2、ead1;for(inti=0;ixishu=x;p->zhishu=z;r->next=p;r=p;}if(r!=NULL)r->next=NULL;//第二个多项式LinkListhead2;LNode*p2,*r2;head2=(LinkList)malloc(sizeof(LNode));head2->next=NULL;r2=head2;for(inti=0;i3、LNode*)malloc(sizeof(LNode));scanf("%d",&xx);scanf("%d",&zz);p2->xishu=xx;p2->zhishu=zz;r2->next=p2;r2=p2;}if(r2!=NULL)r2->next=NULL;//加法或者是减法LNode*pa,*pb,*pc;LinkListhead3;pa=(LNode*)malloc(sizeof(LNode));pb=(LNode*)malloc(sizeof(LNode));pc=(LNode*)malloc(sizeof(LNode));4、pa=head1->next;pb=head2->next;head3=pc=head1;if(fangfa==0){//加法while(pa&&pb){if(pa->zhishuzhishu){pc->next=pa;pc=pa;pa=pa->next;}elseif(pa->zhishu>pb->zhishu){pc->next=pb;pc=pb;pb=pb->next;}else{pa->xishu=(pa->xishu)+(pb->xishu);pc->next=pa;pc=pa;pb=pb->next;pa=pa->n5、ext;}}while(1){if(pa){pc->next=pa;pc=pa;pa=pa->next;}if(pb){pc->next=pb;pc=pb;pb=pb->next;}if(pc->next==NULL)break;}free(head2);}if(fangfa==1){//减法while(pa&&pb){if(pa->zhishuzhishu){pc->next=pa;pc=pa;pa=pa->next;}elseif(pa->zhishu>pb->zhishu){pc->next=pb;pc=pb;pb=pb-6、>next;}else{pa->xishu=(pa->xishu)-(pb->xishu);pc->next=pa;pc=pa;pb=pb->next;pa=pa->next;}}while(1){if(pa){pc->next=pa;pc=pa;pa=pa->next;}if(pb){pc->next=pb;pc=pc->next;pc->xishu=-(pb->xishu);pc->zhishu=pb->zhishu;pc->next=pb->next;//pb=pb->next;}if(pc->next==NULL)break;}f7、ree(head2);}pc=head3;for(pc=pc->next;pc->next!=NULL;pc=pc->next){if(pc->xishu==0);elsebreak;}if((pc->xishu==0)&&(pc->next==NULL))printf("0");pc=head3;if(pc->next!=NULL){pc=pc->next;if(pc->xishu==0);elseif(pc->xishu==1){if(pc->zhishu==0)printf("1");elseif(pc->zhishu==1)pri8、ntf("x");elseprintf("x^%d",pc->zhishu);}elseif(pc->xishu==-1){if(pc->zhishu==0)printf("-1");else
3、LNode*)malloc(sizeof(LNode));scanf("%d",&xx);scanf("%d",&zz);p2->xishu=xx;p2->zhishu=zz;r2->next=p2;r2=p2;}if(r2!=NULL)r2->next=NULL;//加法或者是减法LNode*pa,*pb,*pc;LinkListhead3;pa=(LNode*)malloc(sizeof(LNode));pb=(LNode*)malloc(sizeof(LNode));pc=(LNode*)malloc(sizeof(LNode));
4、pa=head1->next;pb=head2->next;head3=pc=head1;if(fangfa==0){//加法while(pa&&pb){if(pa->zhishuzhishu){pc->next=pa;pc=pa;pa=pa->next;}elseif(pa->zhishu>pb->zhishu){pc->next=pb;pc=pb;pb=pb->next;}else{pa->xishu=(pa->xishu)+(pb->xishu);pc->next=pa;pc=pa;pb=pb->next;pa=pa->n
5、ext;}}while(1){if(pa){pc->next=pa;pc=pa;pa=pa->next;}if(pb){pc->next=pb;pc=pb;pb=pb->next;}if(pc->next==NULL)break;}free(head2);}if(fangfa==1){//减法while(pa&&pb){if(pa->zhishuzhishu){pc->next=pa;pc=pa;pa=pa->next;}elseif(pa->zhishu>pb->zhishu){pc->next=pb;pc=pb;pb=pb-
6、>next;}else{pa->xishu=(pa->xishu)-(pb->xishu);pc->next=pa;pc=pa;pb=pb->next;pa=pa->next;}}while(1){if(pa){pc->next=pa;pc=pa;pa=pa->next;}if(pb){pc->next=pb;pc=pc->next;pc->xishu=-(pb->xishu);pc->zhishu=pb->zhishu;pc->next=pb->next;//pb=pb->next;}if(pc->next==NULL)break;}f
7、ree(head2);}pc=head3;for(pc=pc->next;pc->next!=NULL;pc=pc->next){if(pc->xishu==0);elsebreak;}if((pc->xishu==0)&&(pc->next==NULL))printf("0");pc=head3;if(pc->next!=NULL){pc=pc->next;if(pc->xishu==0);elseif(pc->xishu==1){if(pc->zhishu==0)printf("1");elseif(pc->zhishu==1)pri
8、ntf("x");elseprintf("x^%d",pc->zhishu);}elseif(pc->xishu==-1){if(pc->zhishu==0)printf("-1");else
此文档下载收益归作者所有