您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 经营企划 > 一元稀疏多项式计算器加,减,乘,除运算
山东大学14级计算机科学与技术学院,王士彬#includectime#includeiostream#includelist#includestringusingnamespacestd;classterm{public:term(intc=0,inte=0):coef(c),exp(e){}intcoef;//系数intexp;//指数};classPolynomial{private:intdegree;//阶数listtermtermList;//存储多项式public:Polynomial(){degree=0;};//---------------------------------------构造函数,创造一个0阶多项式intDegree();//--------------------------------------------------求多项式的阶数doubleValue(doublex);//----------------------------------------返回多项式在x下的值voidClear();//--------------------------------------------------清空多项式Polynomialshunxu();//-----------------------指数递增变递减,递减变递增Polynomialpaixu();//------------------------用来对计算后结果的排序处理(主要去掉系数为0的项)/*函数方法*/voidInput();//----------------------------------------------------------------多项式输入voidOutPut();//---------------------------------------------------------------多项式输出PolynomialAdd(Polynomial&b);//-----------------------------------------------多项式相加PolynomialSubtract(Polynomial&b);//------------------------------------------多项式相减PolynomialMultiply(Polynomial&b);//------------------------------------------多项式相乘PolynomialDivide(Polynomial&b);//-------------------------------------------多项式相除/*操作符重载*/friendistream&operator(istream&is,Polynomial&obj);//----------------输入符号重载friendostream&operator(ostream&os,Polynomial&obj);//----------------输出符号重载Polynomialoperator+(Polynomial&b);//------------------------------------加号重载Polynomialoperator-(Polynomial&b);//------------------------------------减号重载Polynomialoperator*(Polynomial&b);//------------------------------------乘号重载Polynomialoperator/(Polynomial&b);//------------------------------------除号重载};intPolynomial::Degree(){listterm::iteratoriter=termList.begin();termtemp=(term)*iter;intdegree=temp.exp;returndegree;}doublePolynomial::Value(doublex){doublesum=0;inti;listterm::iteratoriter=termList.begin();for(;iter!=termList.end();iter++){termtemp=(term)*iter;intt=1;for(i=temp.exp;i!=0;i--){t=t*x;}sum+=temp.coef*t;}returnsum;}voidPolynomial::Clear(){termList.clear();}PolynomialPolynomial::shunxu(){Polynomialp1;listterm::iteratoriter=termList.begin();for(;iter!=termList.end();iter++){termtemp;termt_a=(term)*iter;temp.coef=t_a.coef;temp.exp=t_a.exp;p1.termList.push_front(temp);}returnp1;}//用来对计算后结果的排序处理(在输入时已经对系数为0的情况处理过了,但得出的结果系数可能有等于0的,此方法就是对系数为0的情况处理)PolynomialPolynomial::paixu(){Polynomialaaa;listterm::iteratorit=termList.begin();for(;it!=termList.end();){termt11=*it;if(t11.coef==0){it++;}else{aaa.termList.push_back(t11);it++;}}returnaaa;}voidPolynomial::Input(){intn;cout,请输入多项式的项数:;cinn;cout按升幂输入多项式的系数和指数endl;for(inti=1;i=n;i++){termt_temp;cout请输入第i项系数和指数,中间以空格为界:;cint_temp.coef;cint_temp.exp;if(t_temp.coef!=0){termList.push_back(t_temp);}}}voidPolynomial::OutPut(){if(termList.empty()){cout0;}else{listterm::iteratorit=termList.begin();for(;it!=termList.end();){termt11=*it;if(t11.exp!=0){if(t11.coef==1){if(t11.exp==1){coutx;if(++it!=termList.end()){termt112=*it;if(t112.coef0){cout+;}else{cout;}}}else{coutx^t11.exp;if(++it!=termList.end()){termt112=*it;if(t112.coef0){cout+;}else{cout;}}}}elseif(t11.coef==(-1)){if(t11.exp==1){cout-x;if(++it!=termList.end()){termt112=*it;if(t112.coef0){cout+;}else{cout;}}}else{cout-x^t11.exp;if(++it!=termList.end()){termt112=*it;if(t112.coef0){cout+;}else{cout;}}}}else{if(t11.exp==1){coutt11.coefx;if(++it!=termList.end()){termt112=*it;if(t112.coef0){cout+;}else{cout;}}}else{coutt11.coefx^t11.exp;if(++it!=termList.end()){termt112=*it;if(t112.coef0){cout+;}else{cout;}}}}}else{coutt11.coef;if(++it!=termList.end()){termt112=*it;if(t112.coef0){cout+;}else{cout;}}}}}}PolynomialPolynomial::Add(Polynomial&b){Polynomialc;listterm::iteratoriter_a=termList.begin();listterm::iteratoriter_b=b.termList.begin();while(iter_a!=termList.end()&&iter_b!=b.termList.end()){termt_temp;termt_a=(term)*iter_a;termt_b=(term)*iter_b;if(t_a.expt_b.exp){c.termList.push_back(t_b);iter_b++;}elseif(t_a.expt_b.exp){c.termList.push_back(t_a);iter_a++;}else{t_temp.coef=t_a.coef+t_b.coef;t_temp.exp=t_a.exp;c.termList.push_back(t_temp);iter_a++;iter_b++;}}for(;iter_a!=termList.end();iter_a++){c.termList.push_back(*iter_a);}for(;iter_b!=b.termList.end();iter_b++){c.termList.push_back(*iter_b);}Polynomialc1=c.paixu();returnc1;}PolynomialPolynomial::Subtract(Polynomial&b){Polynomialc;listterm::iteratoriter_a=termList.begin();listterm::iteratoriter_b=b.termList.begin();while(iter_a!=termList.end()&&iter_b!=b.termList.end()){termtemp;termt_a=(term)*iter_a;termt_b=(term)*iter_b;if(t_a.expt_b.exp){c.termList.push_back(t_a);iter_a++;}elseif(t_a.expt_b.exp){temp.coef=-t_b.coef;temp.exp=t_b.exp;c.termList.push_back(temp);iter_b++;}else{temp.coef=t_a.coef-t_b.coef;temp.exp=t_a.exp;c.termList.push_back(temp);iter_a++;iter_b++;}}for(;iter_a!=termList.end();iter_a++){c.termList.push_back(*iter_a);}for(;iter_b!=b.termList.end();iter_b++){termtemp2;termt_b=(term)*iter_b;temp2.coef=-t_b.coef;temp2.exp=t_b.exp;c.termList.
本文标题:一元稀疏多项式计算器加,减,乘,除运算
链接地址:https://www.777doc.com/doc-3369178 .html