C链表实现多项式加法乘法

上传人:re****.1 文档编号:508326332 上传时间:2023-08-29 格式:DOC 页数:8 大小:22.50KB
返回 下载 相关 举报
C链表实现多项式加法乘法_第1页
第1页 / 共8页
C链表实现多项式加法乘法_第2页
第2页 / 共8页
C链表实现多项式加法乘法_第3页
第3页 / 共8页
C链表实现多项式加法乘法_第4页
第4页 / 共8页
C链表实现多项式加法乘法_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《C链表实现多项式加法乘法》由会员分享,可在线阅读,更多相关《C链表实现多项式加法乘法(8页珍藏版)》请在金锄头文库上搜索。

1、#includeusingnamespacestd;classTermpublic:floatcoef:系数intexpn;/指数Term*next;classPolypublic:Poly()构造L=newTerm;L-next二NULL;Poly(constPoly&);/复制xPoly()/析构Term*p;while(L)p=L-next;deleteL;L二p;voidcreatepoly();voiddisplaypoly();intgetlenO;friendvoidADDpoly(Polyft,Poly&);voidmulpoly(Poly&,Term*);voidmultip

2、lypoly(Polyft,Poly&);private:Term*L;Poly:Poly(constPoly&pl)/深复制L=newTerm;Term*p=L,*r=p1.L-next;while(r)p-next=newTerm;p-next-coef=r-coef;p-next-expn=r-expn;p=p-next;r=r-next;p-next二NULL;intPoly:getlenO/多项式长度Term*p=L-next;intlen=0;while(p)len+;p=p-next;returnlen;voidPoly:createpoly()输入规则:项数/系数1指数1系数2

3、指数2.Term*p=L,*q;intn;cout/zPleaseinputquantityofterms:z,n;cout/zPleaseinputthePolynomial:,zendl;for(inti=0;iq-coef;cinq-expn;p-next=q;P二q;pnext二NULL;voidPoly:displaypoly()Term水p二L-next;coutVThePolynomialis:endl;if(!p)return;wh订e(p-next!=NULL)if(p-expn=O)coutcoef;i.f(p-next-coef=0)cout+;elsecoutcoefX

4、p-expn;i.f(p-next-coef=0)coutnext;coutp-coefX,zexpnnext,*pb=hb-next;while(pa&pb)if(pa-expnexpn)ha=pa;pa=pa-next;elseif(pa-expnpb-expn)ha-next=pb;hb-next二pb-next;pb-next=pa;pb二hb-next;ha=ha-next;else/相等辻(pa-coef+pb-coef!=0.0)/系数不为0pa-coefz:pa-coef+pb-coef;ha=pa;elseha-next=pa-next;deletepa;hb-next二pb

5、-next;deletepb;pa=ha-next;pb二hb-next;辻(pb)hd-next二pb;/若pb不为空,则将剩余接到ha后面deletehb;/C(X)=A(X)*B(X)/=A(X)*blXel+b2Xe2.+bnXen/=(sigmai二ln)biA(X)XeivoidPoly::mulpoly(Poly&pol,Term*p2)/对A(x)进行系数加权Term*pl=po1.L-next,*p=L;while(pl)p-next=newTerm;p-next-coef=pl-coef*p2-coef;p-next-expn二pl-expn+p2-expn;p=p-nex

6、t;pl二pl-next;p-next二NULL;voidPoly::multiplypoly(Poly&pol,Poly&po2)/将多项式乘法分解为一系列加法运算Polypoi;Term*p2=po2L-next;while(p2)poi.mulpoly(pol,p2);ADDpoly(*this),poi);poi.L=newTerm;poi.L-next二NULL;p2二p2-next;intmdin()Polypl,p2,p3,p4,p5;cout*ADD*endl;pl.createpoly();pl.displaypoly();p2createpoly();p2displaypoly();coutPl+P2:ADDpoly(pl,p2);pl.displaypoly();coutz,*MUTIPLY*z,endl;p3createpoly();p3displaypoly();p4.createpoly();p4.displaypoly();coutPl*P2:;p5multiplypoly(p3,p4);p5displaypoly();return0;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 活动策划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号