欢迎来到天天文库
浏览记录
ID:33857180
大小:135.97 KB
页数:20页
时间:2019-03-01
《数据结构与算法分析 第6章 答案 larry nyhoff 清华大学出版社》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Chapter6Chapter6:ListsExercises6.31-6.//-----Polynomial.h-----#include#ifndefPOLYNOMIAL#definePOLYNOMIALconstintMAX_DEGREE=100;typedefdoubleCoefType;classPolynomial{public:voidread(istream&in);voiddisplay(ostream&out)const;Polynomialoperator+(constPolynomial&p
2、oly);Polynomialoperator*(constPolynomial&poly);CoefTypeevaluate(doublevalue);private:intmyDegree;CoefTypemyCoeff[MAX_DEGREE+1];};istream&operator>>(istream&in,Polynomial&p);ostream&operator<<(ostream&out,constPolynomial&p);#endif//-----Polynomial.cpp------#include3、eam>#includeusingnamespacestd;#include"Polynomial.h"//--Definitionof>>istream&operator>>(istream&in,Polynomial&p){p.read(in);returnin;}//--Definitionofread()voidPolynomial::read(istream&in){cout<<"Enterthedegree(<="<>myDegree;//thedegreeoft4、hispolynomialassert(myDegree<=MAX_DEGREE);–26–Chapter6CoefTypeco;//thecoefficientsinascendingordercout<<"Entercoefficientsinascendingorder:";for(intindex=0;index<=myDegree;index++){in>>co;myCoeff[index]=co;}}//--Definitionof<5、mial&p){p.display(out);returnout;}//--Definitionofdisplay()voidPolynomial::display(ostream&out)const{for(intindex=0;index6、valuate(CoefTypevalue){CoefTypepower=1,result=0;for(intindex=0;index<=myDegree;index++){result+=myCoeff[index]*power;power*=value;}returnresult;}//--Definitionof+PolynomialPolynomial::operator+(constPolynomial&b){Polynomialc;if(myDegree7、egree;i++)c.myCoeff[i]+=myCoeff[i];c.myDegree=b.myDegree;}else{c=*this;–27–Chapter6for(inti=0;i<=b.myDegree;i++)c.myCoeff[i]+=b.myCoeff[i];while(c.myCoeff[c.myDegree]==0)c.myDegree--;}returnc;}//--Definitionof*PolynomialPolynomial::operator*(constPolynomial&b){Polynomia8、lc;c.myDegree=myDegree+b.myDegree;for(inti=0;i<=c.myDegree;i++)c.myCoeff[i]=0;for(inti=0;i<=myDegree;i++)for(i
3、eam>#includeusingnamespacestd;#include"Polynomial.h"//--Definitionof>>istream&operator>>(istream&in,Polynomial&p){p.read(in);returnin;}//--Definitionofread()voidPolynomial::read(istream&in){cout<<"Enterthedegree(<="<>myDegree;//thedegreeoft
4、hispolynomialassert(myDegree<=MAX_DEGREE);–26–Chapter6CoefTypeco;//thecoefficientsinascendingordercout<<"Entercoefficientsinascendingorder:";for(intindex=0;index<=myDegree;index++){in>>co;myCoeff[index]=co;}}//--Definitionof<5、mial&p){p.display(out);returnout;}//--Definitionofdisplay()voidPolynomial::display(ostream&out)const{for(intindex=0;index6、valuate(CoefTypevalue){CoefTypepower=1,result=0;for(intindex=0;index<=myDegree;index++){result+=myCoeff[index]*power;power*=value;}returnresult;}//--Definitionof+PolynomialPolynomial::operator+(constPolynomial&b){Polynomialc;if(myDegree7、egree;i++)c.myCoeff[i]+=myCoeff[i];c.myDegree=b.myDegree;}else{c=*this;–27–Chapter6for(inti=0;i<=b.myDegree;i++)c.myCoeff[i]+=b.myCoeff[i];while(c.myCoeff[c.myDegree]==0)c.myDegree--;}returnc;}//--Definitionof*PolynomialPolynomial::operator*(constPolynomial&b){Polynomia8、lc;c.myDegree=myDegree+b.myDegree;for(inti=0;i<=c.myDegree;i++)c.myCoeff[i]=0;for(inti=0;i<=myDegree;i++)for(i
5、mial&p){p.display(out);returnout;}//--Definitionofdisplay()voidPolynomial::display(ostream&out)const{for(intindex=0;index6、valuate(CoefTypevalue){CoefTypepower=1,result=0;for(intindex=0;index<=myDegree;index++){result+=myCoeff[index]*power;power*=value;}returnresult;}//--Definitionof+PolynomialPolynomial::operator+(constPolynomial&b){Polynomialc;if(myDegree7、egree;i++)c.myCoeff[i]+=myCoeff[i];c.myDegree=b.myDegree;}else{c=*this;–27–Chapter6for(inti=0;i<=b.myDegree;i++)c.myCoeff[i]+=b.myCoeff[i];while(c.myCoeff[c.myDegree]==0)c.myDegree--;}returnc;}//--Definitionof*PolynomialPolynomial::operator*(constPolynomial&b){Polynomia8、lc;c.myDegree=myDegree+b.myDegree;for(inti=0;i<=c.myDegree;i++)c.myCoeff[i]=0;for(inti=0;i<=myDegree;i++)for(i
6、valuate(CoefTypevalue){CoefTypepower=1,result=0;for(intindex=0;index<=myDegree;index++){result+=myCoeff[index]*power;power*=value;}returnresult;}//--Definitionof+PolynomialPolynomial::operator+(constPolynomial&b){Polynomialc;if(myDegree7、egree;i++)c.myCoeff[i]+=myCoeff[i];c.myDegree=b.myDegree;}else{c=*this;–27–Chapter6for(inti=0;i<=b.myDegree;i++)c.myCoeff[i]+=b.myCoeff[i];while(c.myCoeff[c.myDegree]==0)c.myDegree--;}returnc;}//--Definitionof*PolynomialPolynomial::operator*(constPolynomial&b){Polynomia8、lc;c.myDegree=myDegree+b.myDegree;for(inti=0;i<=c.myDegree;i++)c.myCoeff[i]=0;for(inti=0;i<=myDegree;i++)for(i
7、egree;i++)c.myCoeff[i]+=myCoeff[i];c.myDegree=b.myDegree;}else{c=*this;–27–Chapter6for(inti=0;i<=b.myDegree;i++)c.myCoeff[i]+=b.myCoeff[i];while(c.myCoeff[c.myDegree]==0)c.myDegree--;}returnc;}//--Definitionof*PolynomialPolynomial::operator*(constPolynomial&b){Polynomia
8、lc;c.myDegree=myDegree+b.myDegree;for(inti=0;i<=c.myDegree;i++)c.myCoeff[i]=0;for(inti=0;i<=myDegree;i++)for(i
此文档下载收益归作者所有