一元稀疏多项式计算器加减乘除运算.docx
- 文档编号:10177340
- 上传时间:2023-02-09
- 格式:DOCX
- 页数:28
- 大小:19.42KB
一元稀疏多项式计算器加减乘除运算.docx
《一元稀疏多项式计算器加减乘除运算.docx》由会员分享,可在线阅读,更多相关《一元稀疏多项式计算器加减乘除运算.docx(28页珍藏版)》请在冰豆网上搜索。
一元稀疏多项式计算器加减乘除运算
山东大学
14级计算机科学与技术学院,王士彬
#include
#include
#include
#include
usingnamespacestd;
classterm{
public:
term(intc=0,inte=0):
coef(c),exp(e){}
intcoef;//系数
intexp;//指数
};
classPolynomial{
private:
intdegree;//阶数
list
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(){
list
:
iteratoriter=termList.begin();
termtemp=(term)*iter;
intdegree=temp.exp;
returndegree;
}
doublePolynomial:
:
Value(doublex){
doublesum=0;
inti;
list
:
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;
list
:
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;
list
:
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<<",请输入多项式的项数:
";
cin>>n;
cout<<"按升幂输入多项式的系数和指数"< for(inti=1;i<=n;i++){ termt_temp; cout<<"请输入第"< "; cin>>t_temp.coef; cin>>t_temp.exp; if(t_temp.coef! =0){ termList.push_back(t_temp); } } } voidPolynomial: : OutPut(){ if(termList.empty()){ cout<<"0"; }else{ list : iteratorit=termList.begin(); for(;it! =termList.end();){ termt11=*it; if(t11.exp! =0){ if(t11.coef==1){ if(t11.exp==1){ cout<<"x"; if(++it! =termList.end()){ termt112=*it; if(t112.coef>0){ cout<<"+"; }else{ cout<<""; } } }else{ cout<<"x^"< if(++it! =termList.end()){ termt112=*it; if(t112.coef>0){ cout<<"+"; }else{ cout<<""; } } } }elseif(t11.coef==(-1)){ if(t11.exp==1){ cout<<"-x"; if(++it! =termList.end()){ termt112=*it; if(t112.coef>0){ cout<<"+"; }else{ cout<<""; } } }else{ cout<<"-x^"< if(++it! =termList.end()){ termt112=*it; if(t112.coef>0){ cout<<"+"; }else{ cout<<""; } } } }else{ if(t11.exp==1){ cout< if(++it! =termList.end()){ termt112=*it; if(t112.coef>0){ cout<<"+"; }else{ cout<<""; } } }else{ cout< if(++it! =termList.end()){ termt112=*it; if(t112.coef>0){ cout<<"+"; }else{ cout<<""; } } } } }else{ cout< if(++it! =termList.end()){ termt112=*it; if(t112.coef>0){ cout<<"+"; }else{ cout<<""; } } } } } } PolynomialPolynomial: : Add(Polynomial&b){ Polynomialc; list : iteratoriter_a=termList.begin(); list : 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.exp c.termList.push_back(t_b); iter_b++; }elseif(t_a.exp>t_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; list : iteratoriter_a=termList.begin(); list : 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.exp>t_b.exp){ c.termList.push_back(t_a); iter_a++; }elseif(t_a.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.push_back(temp2); } Polynomialc1=c.paixu(); returnc1; } PolynomialPolynomial: : Multiply(Polynomial&b){ Polynomialc; list : iteratoriter_a=termList.begin(); for(;iter_a! =termList.end();iter_a++){ Polynomialc1;//这个c1的位置只能放在这里,局部变量 list : iteratoriter_b=b.termList.begin(); for(;iter_b! =b.termList.end();iter_b++){ termtemp; termt_a=(term)*iter_a; termt_b=(term)*iter_b; temp.coef=t_a.coef*t_b.coef; temp.exp=t_a.exp+t_b.exp; c1.termList.push_back(temp); } c=c.Add(c1); c1.Clear(); } returnc; } PolynomialPolynomial: : Divide(Polynomial&b){ Polynomiala;//被除数 list : iteratoriter_a=termList.begin(); for(;iter_a! =termList.end();iter_a++){ termtemp; termt_a=(term)*iter_a; temp.coef=t_a.coef; temp.exp=t_a.exp; a.termList.push_back(temp); } //--------------------------------------------------------- Polynomialquotient,remainder,temp1,temp2; list : iteratorqa_a=a.termList.begin(); list : iteratorqb_b=b.termList.begin(); termqa=(term)*qa_a; termqb=(term)*qb_b; temp1=a; //cout<<"要到达while循环了"< while(qa.exp>=qb.exp){ termtemp2_term; temp2_term.coef=qa.coef/qb.coef; temp2_term.exp=qa.exp-qb.exp; quotient.termList.push_back(temp2_term); Polynomialcenter_quotient; center_quotient.termList.push_back(temp2_term); Polynomialtt=b*center_quotient; a=a-tt; a=a.paixu(); list : iteratorqa_1=a.termList.begin(); qa=(term)*qa_1; } Polynomialji=quotient*b;//Polynomial remainder=temp1-ji; returnremainder; } istream&operator>>(istream&is,Polynomial&obj){ intn; cout<<"请输入多项式的项数: "; cin>>n; cout<<"按升幂输入多项式的系数和指数"< for(inti=1;i<=n;i++){ termt_temp; cout<<"请输入第"< "; cin>>t_temp.coef; cin>>t_temp.exp; if(t_temp.coef! =0){ obj.termList.push_back(t_temp); } } returnis; } ostream&operator<<(ostream&os,Polynomial&obj){ if(obj.termList.empty()){ cout<<"0"; }else{ list : iteratorit=obj.termList.begin(); for(;it! =obj.termList.end();){ termt11=*it; if(t11.exp! =0){ if(t11.coef==1){ if(t11.exp==1){ cout<<"x"; if(++it! =obj.termList.end()){ termt112=*it; if(t112.coef>0){ cout<<"+"; }else{ cout<<""; } } }else{ cout<<"x^"< if(++it! =obj.termList.end()){ termt112=*it; if(t112.coef>0){ cout<<"+"; }else{ cout<<""; } } } }elseif(t11.coef==(-1)){ if(t11.exp==1){ cout<<"-x"; if(++it! =obj.termList.end()){ termt112=*it; if(t112.coef>0){ cout<<"+"; }else{ cout<<""; } } }else{ cout<<"-x^"< if(++it! =obj.termList.end()){ termt112=*it; if(t112.coef>0){ cout<<"+"; }else{ cout<<""; } } } }else{ if(t11.exp==1){ cout< if(++it! =obj.termList.end()){ termt112=*it; if(t112.coef>0){ cout<<"+"; }else{ cout<<""; } } }else{ cout< if(++it! =obj.termList.end()){ termt112=*it; if(t112.coef>0){ cout<<"+"; }else{ cout<<""; } } } } }else{ cout<
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 一元 稀疏 多项式 计算器 加减乘除 运算
![提示](https://static.bdocx.com/images/bang_tan.gif)