多项式运算Cpp程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

//===用带表头结点的单链表实现多项式的加-减-乘-除运算的C++程序====//======================一元多项式的运算:OOP======================//1.降幂建立一个一元多项式并输出----------------------------------//2.求两个多项式的和并输出(原多项式不变)--------------------------//3.利用求和函数,求两个多项式的差并输出(原多项式不变)------------//4.利用求和与求差函数,求两个多项式的积并输出(原多项式不变)------//5.利用求和、差、积函数,求两多项式的商并输出(原多项式不变)------#includeconio.h//getch#includecstdlib//exit#includecmath//fabs#includeiostream//cout,cinusingnamespacestd;typedefstruct{//元素类型定义floatcoef;//系数(Coefficient)intexpn;//指数(exponential)}EType;//元素类型名(ElementType)typedefstructLNode{//链表结点类型定义ETyped;//结点数据域structLNode*next;//结点指针域}LNode,*LinkNP;//结点及结点指针类型名typedefstruct{//表头结点类型LinkNPhead,tail;//链表头尾指针intlen;//链表长度}LHNode;//链表头结点类型名typedefLHNode*polyn;//多项式表头结点指针类型名//================多项式类的定义================//classpolynomail{private:polynp;//数据成员public:polynomail()//==构造函数:初始化表头结点及其指针=={p=newLHNode;if(!p)errExit(NewLHNodeerror!);//检测指针是否为空p-head=NULL,p-tail=NULL,p-len=0;}voiderrExit(char*str)//遇错自动退出程序{coutstr;exit(0);}intCmpElemExpn(ETypea,ETypeb)//==比较两项的幂次=={if(a.expnb.expn)return1;elseif(a.expnb.expn)return-1;elsereturn0;}intMakeNode(LinkNP&L,ETypee)//==生成值为e的结点,指针存入L=={L=newLNode;//向内存申请空间if(!L)errExit(newLinkNPerror!);//检测指针是否为空L-d=e;//给结点赋值L-next=NULL;return1;}voidAppNode(LinkNPL)//==结点链入尾部=={if(p-head==NULL)//若表为空p-head=p-tail=L;else{p-tail-next=L;//L连到尾部p-tail=L;//修改头结点的尾指针}p-len+=1;//表长增1}voidInsFirst(LinkNPs)//==s插入链首结点前=={if(!p-head)AppNode(s);elses-next=p-head,p-head=s,p-len+=1;}voidInsSortNode(LinkNPs)//==s所指插入有序链p中=={LinkNPh=p-head,t=p-tail;if(!h||s-d.expnt-d.expn)//若表空或s比尾大...AppNode(s);//...则直接连入尾部elseif(s-d.expnh-d.expn)//s比首元素指数大InsFirst(s);//作为首元素插入else{while(1){//查找插入位置if(s-d.expnh-next-d.expn)break;//h停在待插点前h=h-next;}s-next=h-next;//插入h之后h-next=s;p-len+=1;}}intLocateElem(ETypee,LinkNP&q)//==p中找e且指针入q=={LinkNPh=p-head;while(h&&CmpElemExpn(h-d,e))//查找是否有eh=h-next;q=h;//指针存入qif(q)return1;//若有elsereturn0;//若无}voidCreatPoly(char*str)//==逐个降幂有序创建=={inti=1;LinkNPs,q;ETypee;printf(\nCreatpolyn%s:\n,str);while(1){printf(\tcoef%d,expn%d=,i,i);scanf(%f,%d,&e.coef,&e.expn);//输入系数及幂次if(e.coef==0&&e.expn==0)break;//若全0则结束if(fabs(e.coef)1.0e-30||(e.expn0))//若系数或指数非法{coutError!\n;continue;}//重新输入if(!LocateElem(e,q)){//若该项不存在...if(MakeNode(s,e))//若结点创建成功InsSortNode(s);//将s所指有序插入}else{coutError!\n;continue;}//...否则重新输入i++;//记录项数}}voidItemPrint(floata,intn)//输出某项ax^n{if(n==0)couta;//指数为0elseif(n==1){//指数为1if(a==1)coutx;elseif(a==-1)cout-x;elsecoutax;}else{//指数大于1if(a==1)coutx^n;elseif(a==-1)cout-x^n;elsecoutax^n;}}voidPrintPoly(char*str)//==输出多项式=={LinkNPL;L=p-head;if(L){//若非空则输出首元结点printf(\n多项式%s中有%d项:,str,p-len);ItemPrint(L-d.coef,L-d.expn);L=L-next;}elseprintf(\n多项式%s为空!,str);while(L){//输出之后所有结点if(L-d.coef0)cout+;ItemPrint(L-d.coef,L-d.expn);L=L-next;}}//==============两多项式类相加==============//voidAddPoly(polynomailpa,polynomailpb)//求两个多项式的和{LinkNPha,hb,ht;ETypeea,eb,ec;ha=pa.p-head,hb=pb.p-head;while(ha&&hb){//a,b都非空时循环ea=ha-d,eb=hb-d;switch(CmpElemExpn(ea,eb)){//比较大小case1://若ea大则将ea加入ecif(MakeNode(ht,ea))AppNode(ht);ha=ha-next;//a表指针后移break;case0://若ea与ec相等则将和加入ecec.coef=ea.coef+eb.coef;ec.expn=ea.expn;if(ec.coef)//若系数不为0if(MakeNode(ht,ec))//若开辟结点成功AppNode(ht);//将结点指针L链入cha=ha-next,hb=hb-next;//a,b两表指针同时后移break;case-1://若eb大则将eb加入ecif(MakeNode(ht,eb))AppNode(ht);hb=hb-next;//b表指针后移break;}}while(ha){//若ea有剩余,则将剩余加入ea=ha-d;if(MakeNode(ht,ea))AppNode(ht);ha=ha-next;}while(hb){//若eb有剩余,则将剩余加入eb=hb-d;if(MakeNode(ht,eb))AppNode(ht);hb=hb-next;}}voidCopyPoly(polynomails)//拷贝多项式{LinkNPnp=s.p-head,q;while(np){if(MakeNode(q,np-d))AppNode(q);np=np-next;}}voidRevSignPoly(polynomail&pn)//系数反号{LinkNPp=pn.p-head;while(p){p-d.coef=-p-d.coef;//反号p=p-next;}}//==============两多项式类相减==============//voidSubPoly(polynomaila,polynomailb)//将b反号与a相加{polynomailpn;pn.CopyPoly(b);RevSignPoly(pn);AddPoly(a,pn);}//==============两多项式类相乘==============//voidMulPoly(polynomaila,polynomailb,polynomail&c){//a与b相乘存入cLinkNPha=a.p-head,hb=b.p-head;polynomailpa,pb,pc,pt;if(!ha||!hb){c=pt;return;};//空多项式即0while(ha){//取多项式的某项pb.CopyPoly(b);hb=pb.p-head;while(hb){//与多项式b相乘hb-d.coef*=ha-d.coef;hb-d.expn+=ha-d.expn;hb=hb-next;}pt.CopyPoly(c);//c复制到ptc.Destroy();//c清零c.AddPoly(pt,pb);//pt与pb之和存入cpt.Destroy();//pt清零pb.Destroy();ha=ha-next;}}//==============两多项式类相除==============//voidDivPoly(polynomaila,polynomailb,polynomail&q,polynomail&r){//a与b相除商存入q余存入rpolynomailpa,pb,pt;LinkNPep,h,ha=a.p-head,hb=b.p-head;if(!ha){q=r=pt;return;}if(!hb)errExit(\n多项式b不能为零);//除式不能为0pa.CopyPoly(a);while(ha&&hb&&ha-d.expn=hb-d.expn){//a比b首项指数大ep=newLNode;ep-next=NULL;ep-d.coef=ha-d.coef/hb-d.coef;ep-d.expn=ha-d.expn-hb-d.expn;q.AppNode(ep);//商式因子连入qpb.CopyPoly(b);h=pb.p-head;while(h){//商式与除式相乘h-d.coef*=ep-d.coef;h-d.expn+=ep-d.expn;h=h-next;}pt.SubPoly(pa,pb);//再与被除式相减pa.Destroy();pb.Destroy();pa.CopyPoly(pt);pt.Destroy();ha=pa.p-head;}r.CopyPoly(pa);}voidDestroy()//释放内存空间=={Lin

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功