多项式加减乘法.doc_第1页
多项式加减乘法.doc_第2页
多项式加减乘法.doc_第3页
多项式加减乘法.doc_第4页
多项式加减乘法.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#include#includestruct nodeint coef;int exp;node *next;node* create() /以指数为标准降序创建链表,存储多项式node *L,*t,*p,*q; /L表示头指针,t指向尾节点char ch;int m=0;L=new node;L-next=NULL;q=t=L;int c,e;coutch;for(;ch!=y&ch!=n;)coutch;while(ch!=n)m+;cout第mce;if(c=0)goto end;elsep=new node;p-coef=c;p-exp=e;p-next=NULL;if(!L-next)L-next=p;t=p;elseif(t-exp)(p-exp)t-next=p;t=p;elseq=L;while(q-next!=t)&(q-next-exp)(p-exp)q=q-next;if(q-next-exp)=(p-exp)q-next-coef+=p-coef;elsep-next=q-next;q-next=p;end:coutch;for(;ch!=y&ch!=n;)coutch;return L;node* Append(node *L1,node *L2) /求两个多项式的和node *p1,*q1,*q2; p1=L1;q1=L1-next;q2=L2-next;while(q1&q2)if(q1-expq2-exp)q1=q1-next;p1=p1-next;else if(q1-exp=q2-exp)node *t1,*t2;t1=q1;t2=q2;q1-coef+=q2-coef;q2=q2-next;free(t2);if(q1-coef=0)q1=q1-next;p1-next=q1;free(t1);elseL2-next=q2-next;p1-next=q2;q2-next=q1;p1=q2;q2=L2-next;if(q2)p1-next=q2;L2-next=NULL;return L1;node* product(node *L1,node *L2) /多项式相乘node *pL,*L;node *p,*p1,*p2,*t;pL=new node;L=new node;pL-next=L-next=NULL;p1=L1-next;while(p1)p2=L2-next;p=pL;while(p2)t=new node;t-coef=p1-coef*p2-coef;t-exp=p1-exp+p2-exp;t-next=NULL;p-next=t;p=t;p2=p2-next;Append(L,pL);p1=p1-next;return L;int productofx(int x,int e) /求X的e次方的值if(e=0)return 1;else if(e=1)return x;else return x*productofx(x,e-1);double sum(node *L,int x) /求当X的值为x时多项式的值node *p;double sum=0;p=L-next;while(p)if(p-exp0)sum+=(p-coef)*productofx(x,p-exp);elsesum+=(p-coef)/productofx(x,-(p-exp);p=p-next;return sum;void opposite(node *L) /多项式的取反node *p;p=L-next;while(p)p-coef=-(p-coef);p=p-next;void clearL(node *L) /销毁多项式node *p,*t;p=L-next;while(p)t=p;p=p-next;L-next=p;free(t);void show(node *L) /输出多项式node *p;p=L-next;if(!p)coutnext)if(p-coef0)coutexp=0)coutcoef;else if(p-exp=1)if(p-coef=1)coutcoef=-1)cout-X;elsecoutcoefcoef=1)coutXexp;else if(p-coef=-1)cout-Xexp;elsecoutcoefXexp;p=p-next;coutendl;void valua(node *L) /求多项式的值函数的调用char c;coutc;for(;c!=y&c!=n;)coutc;if(c=y)int x;double s;coutx;cout当X为x时以上多项式和的值为:n;show(L);s=sum(L,x);cout=snext=NULL;for(;)cout *=*n $ 菜单 $n $-$n $ 1-求多项式的和 $n $ 2-求多项式的差 $n $ 3-求多项式的积 $n $ 0-退出 $n *=*n;coutch;switch(ch)case 0:goto end;break;case 1:system(cls);L1=create();cout请输入下一个多项式:n;L2=create();coutn;coutA=;show(L1);coutB=;show(L2);L1=Append(L1,L2);coutA+B=;show(L1);system(pause);valua(L1);clearL(L1);break;case 2:system(cls);L1=create();cout请输入下一个多项式:n;L2=create();coutn;coutA=;show(L1);coutB=;show(L2);opposite(L2);L1=Append(L1,L2);coutA-B=;show(L1);system(pause);valua(L1);clearL(L1);break;case 3:node *L;L=new node;system(cls);L1=create();cout请输入下一个多项式:n;L2=create();coutn;coutA=;show(L1);coutB=;show(L2);L=product(L1,L2);coutA*B=;s

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论