欢迎来到天天文库
浏览记录
ID:38675501
大小:35.50 KB
页数:5页
时间:2019-06-17
《多项式相加链表实现(可添加多个多项式)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include"stdafx.h"#include"iostream"#include"windows.h"usingnamespacestd;structPolynomial{floatcoef;intexpn;Polynomial*next;};voidbuild(Polynomial*head){Polynomial*p=head,*q;while(p->next)p=p->next;charc1;charc2;cout<<"输入非数字结束!";while(1){q=newPolynomial();cout<<"请输入指数:";cin>>c1;if(c1<'0'
2、
3、
4、c1>'9')break;intre=1;//判断指数是否相同Polynomial*w=head->next;while(1){w=head->next;re=0;while(w){if(c1==(w->expn+'0'))re=1;w=w->next;}if(re==1){cout<<"指数已存在!请重新输入:";cin>>c1;}elsebreak;}cout<<"请输入系数:";cin>>c2;if(c2<'0'
5、
6、c2>'9')break;q->expn=c1-'0';q->coef=c2-'0';q->next=NULL;p->next=q;p=p->next;c
7、out<next;while(p){if(p->next)cout<coef<<"X("<expn<<")+";elsecout<coef<<"X("<expn<<")";p=p->next;}cout<next,*q1;Polynomial*p2=head2->next,*q2;Polynomial
8、*addhead,*add;addhead=newPolynomial();addhead->next=NULL;Polynomial*addp=addhead;while(p1){intre=1;Polynomial*add=newPolynomial();p2=head2->next;while(p2){if(p1->expn==p2->expn){add->coef=p1->coef+p2->coef;add->expn=p1->expn;add->next=NULL;addp->next=add;addp=addp->next;re=0;}p2=p2->next;}i
9、f(re==1){add->coef=p1->coef;add->expn=p1->expn;add->next=NULL;addp->next=add;addp=addp->next;}p1=p1->next;}p2=head2->next;Polynomial*ap=addp->next;while(p2){intre=1;Polynomial*add=newPolynomial();ap=addhead->next;while(ap){if(p2->expn==ap->expn){re=0;}ap=ap->next;}if(re==1){add->coef=p2->co
10、ef;add->expn=p2->expn;add->next=NULL;addp->next=add;addp=addp->next;}p2=p2->next;}show(addhead);}intmain(intargc,char*argv[]){Polynomial*head[10];intk;intn=0;while(1){cout<<"多项式相加"<>k;if(k==1){system("cls");//cout<<"建立多项式"<11、ad[n]=newPolynomial();head[n]->next=NULL;build(head[n]);n++;system("cls");//}if(k==2){system("cls");cout<<"显示多项式";for(inti=0;i
11、ad[n]=newPolynomial();head[n]->next=NULL;build(head[n]);n++;system("cls");//}if(k==2){system("cls");cout<<"显示多项式";for(inti=0;i
此文档下载收益归作者所有