北邮数据结构实验一题目3一元多项式(代码).txt

上传人:scccc 文档编号:11308743 上传时间:2021-07-22 格式:TXT 页数:6 大小:5.85KB
返回 下载 相关 举报
北邮数据结构实验一题目3一元多项式(代码).txt_第1页
第1页 / 共6页
北邮数据结构实验一题目3一元多项式(代码).txt_第2页
第2页 / 共6页
北邮数据结构实验一题目3一元多项式(代码).txt_第3页
第3页 / 共6页
北邮数据结构实验一题目3一元多项式(代码).txt_第4页
第4页 / 共6页
北邮数据结构实验一题目3一元多项式(代码).txt_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《北邮数据结构实验一题目3一元多项式(代码).txt》由会员分享,可在线阅读,更多相关《北邮数据结构实验一题目3一元多项式(代码).txt(6页珍藏版)》请在三一文库上搜索。

1、#include iostreamusing namespace std;struct element double coef;int exp;element * next;element()next=NULL;/nextNULLelement(double co,int e)coef=co;exp=e;next=NULL;class Polylistpublic:Polylist()front =new element;front-next=NULL;m=0;/Polylist(element e,int n);/Polylist();/void PrintList(char *c);/vo

2、id Add(Polylist *P,char c);/void Calculate(double x);/xvoid Pi();/Polylist* Multiply(Polylist &P);/private:element * front;int m;/;/Polylist:Polylist(element e,int n)if (e=NULL)front=new element;front -next=NULL;m=0;return;front = new element;front -next=NULL;for(int i=n-1;i=0;i-)element * s=new ele

3、ment(ei.coef,ei.exp);s-next=front-next;front-next=s;m=en-1.exp;/Polylist:Polylist()element*p=front;while (p)front=p;p=p-next;delete front;front=NULL;/void Polylist:PrintList(char *c)element * p=front-next;coutFc(X) = ;if (p=NULL)coutexp=0)coutcoef;elsecoutcoefXexp;p=p-next;if (p!=0)&(p-coef0)cout+;c

4、out max:mendlnext;element*q=P-front-next;if(c=-)/while (q)q-coef=0-q-coef;q=q-next;q=P-front-next;/qif (p=NULL)/;front-next=q;m=P-m;while (p&q)if (p-expexp)p_prior=p;p=p-next;m=q-exp;else if (p-expq-exp)p_prior-next=q;p_prior=q;q=q-next;p_prior-next=p;m=p-exp;elsep-coef+=q-coef;if (fabs(p-coef)next=

5、p-next;delete p;p=p_prior-next;m-;elsep_prior=p;m=p-exp;p=p-next;element * temp=q;q=q-next;delete temp;if (q)p_prior-next=q;while (q-next)q=q-next;m=q-exp;P-front=NULL;/xvoid Polylist:Calculate(double x)element *p =front-next;double sum=0;while (p)sum+=p-coef*pow(x,p-exp);p=p-next;coutsumnext;if (p=

6、NULL)/return;if (p-exp=0)/front-next=p-next;element *temp=p;p=p-next;delete temp;while (p)p-coef*=p-exp;p-exp-=1;if(p-next=NULL)m=p-exp;p=p-next;/Polylist* Polylist:Multiply(Polylist &P)element * p=front-next;element * q=P.front-next;/3洢F1(X),F2(X);double * coe1=new doublem+P.m+1;/0m+P.mdouble * coe

7、2=new doublem+P.m+1;double * coe3=new doublem+P.m+1;Polylist *C;if (p=NULL|q=NULL)/0C=new Polylist;return C;for (int i=0;i=m+P.m;i+)coe1i=0;coe2i=0;for (int i=0;iexp)/i洢coe1i=p-coef;p=p-next;if (q!=NULL&i=q-exp)coe2i=q-coef;q=q-next;if (p=NULL&q=NULL)/pq,p0break;int n=0;/for (int i=0;i=m+P.m;i+)coe3

8、i=0;for (int j=0;j=i;j+)/:coe3i+=coe1j*coe2i-j;if (coe3i!=0)/0n+;element *e3=new elementn;for (int i=0,j=0;i=m+P.m;i+)/if (coe3i!=0)e3j.coef=coe3i;e3j.exp=i;j+;C=new Polylist(e3,n);/delete coe1;delete coe2;delete coe3;return C;/()element* cinPolylist(int &n,char* c)coutc():endl;coutn;element *e=NULL

9、;if (n=0)return e;e=new elementn;/for (int i=0;in;i+)couti+1ei.exp;while (i!=0&ei.exp=ei-1.exp)coutei.exp;couti+1ei.coef;while (ei.coef=0)coutei.coef;coutendl;return e;void main()/int m;element *e1=cinPolylist(m,1);Polylist P1(e1,m);P1.PrintList(1);delete e1;e1=NULL;/(P1.PrintList(1)element *e2=cinP

10、olylist(m,2);Polylist P2(e2,m);P2.PrintList(2);delete e2;e2=NULL;/(P2.PrintList(1)/5coutendl1. 2. 3. 4. 5.endln;coutendl;switch (n)case 1:coutF1(X)+F2(X) = ;P1.Add(&P2,+);P1.PrintList(3);break;case 2:coutF1(X)-F2(X) = ;P1.Add(&P2,-);P1.PrintList(3);break;case 3:double x;coutx;coutF1(x) = ;P1.Calculate(x);coutF2(x) = ;P2.Calculate(x);coutendl;break;case 4:P1.Pi();P1.PrintList(1);P2.Pi();P2.PrintList(2);break;case 5:coutPrintList(3);delete P3;P3=NULL;/(Multiply(P2)break;coutendl;system(pause);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1