1、山东大学14 级计算机科学与技术学院,王士彬#include#include#include #includeusing namespace std;class termpublic:term(int c=0,int e=0):coef(c),exp(e)int coef;/系数 int exp;/指数 ;class Polynomialprivate:int degree;/阶数 list termList;/存储多项式 public:Polynomial()degree=0;/-构造函数,创造一个 0 阶多项式 int Degree();/-求多项式的阶数 double Value(dou
2、ble x);/-返回多项式在 x 下的值 void Clear();/-清空多项式 Polynomial shunxu();/-指数递增变递减,递减变递增 Polynomial paixu();/-用来对计算后结果的排序处理 (主要去掉系数为 0 的项)/*函数方法 */ void Input();/-多项式输入 void OutPut();/-多项式输出 Polynomial Add(Polynomial /-多项式相加 Polynomial Subtract(Polynomial /-多项式相减 Polynomial Multiply(Polynomial /-多项式相乘 Polynom
3、ial Divide(Polynomial /-多项式相除 /* 操作符重载 */ friend istream/-输入符号重载 friend ostreamterm temp=(term)*iter;int degree=temp.exp;return degree;double Polynomial:Value(double x)double sum=0;int i;list:iterator iter=termList.begin();for(;iter!=termList.end();iter+)term temp=(term)*iter;int t=1;for(i=temp.exp;
4、i!=0;i-)t=t*x; sum+=temp.coef*t;return sum;void Polynomial:Clear()termList.clear();Polynomial Polynomial:shunxu()Polynomial p1;list:iterator iter=termList.begin();for(;iter!=termList.end();iter+)term temp;term t_a=(term)*iter;temp.coef=t_a.coef;temp.exp=t_a.exp;p1.termList.push_front(temp);return p1
5、;/用来对计算后结果的排序处理 (在输入时已经对系数为 0 的情况处理过了,但得出的结果系数可能有等于 0 的,此方法就是对系数为 0 的情况处理) Polynomial Polynomial:paixu()Polynomial aaa;list:iterator it =termList.begin();for(;it!=termList.end();)term t11=*it;if(t11.coef=0)it+;elseaaa.termList.push_back(t11);it+;return aaa;void Polynomial:Input()int n;coutn;coutt_te
6、mp.coef;cint_temp.exp;if(t_temp.coef!=0)termList.push_back(t_temp); void Polynomial:OutPut()if(termList.empty()cout:iterator it = termList.begin();for(;it!=termList.end();)term t11=*it;if(t11.exp!=0)if(t11.coef=1)if(t11.exp=1)cout0)cout0)cout0)cout0)cout0)cout0)cout0)cout:iterator iter_a=termList.be
7、gin();list:iterator iter_b=b.termList.begin();while(iter_a!=termList.end()term t_a=(term)*iter_a;term t_b=(term)*iter_b;if(t_a.expt_b.exp)c.termList.push_back(t_a);iter_a+;elset_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();it
8、er_a+)c.termList.push_back(*iter_a);for(;iter_b!=b.termList.end();iter_b+)c.termList.push_back(*iter_b);Polynomial c1=c.paixu();return c1;Polynomial Polynomial:Subtract(Polynomial list:iterator iter_a=termList.begin();list:iterator iter_b=b.termList.begin();while(iter_a!=termList.end()term t_a=(term
9、)*iter_a;term t_b=(term)*iter_b;if(t_a.expt_b.exp)c.termList.push_back(t_a);iter_a+;else if(t_a.exp:iterator iter_a=termList.begin();for(;iter_a!=termList.end();iter_a+)Polynomial c1;/这个 c1 的位置只能放在这里,局部变量 list:iterator iter_b=b.termList.begin();for(;iter_b!=b.termList.end();iter_b+)term temp;term t_
10、a=(term)*iter_a;term t_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();return c; Polynomial Polynomial:Divide(Polynomial /被除数 list:iterator iter_a=termList.begin();for(;iter_a!=termList.end();iter_a+)term temp;term t_a=(term)*it
11、er_a;temp.coef=t_a.coef;temp.exp=t_a.exp;a.termList.push_back(temp); /-Polynomial quotient,remainder,temp1,temp2;list:iterator qa_a=a.termList.begin();list:iterator qb_b=b.termList.begin();term qa=(term)*qa_a;term qb=(term)*qb_b;temp1=a;/cout=qb.exp)term temp2_term;temp2_term.coef=qa.coef/qb.coef;te
12、mp2_term.exp=qa.exp-qb.exp;quotient.termList.push_back(temp2_term);Polynomial center_quotient;center_quotient.termList.push_back(temp2_term);Polynomial tt=b*center_quotient;a=a-tt;a=a.paixu();list:iterator qa_1=a.termList.begin();qa=(term)*qa_1;Polynomial ji=quotient*b; / Polynomial remainder =temp1
13、-ji;return remainder; istream coutn;coutt_temp.coef;cint_temp.exp;if(t_temp.coef!=0)obj.termList.push_back(t_temp); return is;ostreamfor(;it!=obj.termList.end();)term t11=*it;if(t11.exp!=0)if(t11.coef=1)if(t11.exp=1)cout0)cout0)cout0)cout0)cout0)cout0)cout0)cout:iterator iter_a=termList.begin();list
14、:iterator iter_b=b.termList.begin();while(iter_a!=termList.end()term t_a=(term)*iter_a;term t_b=(term)*iter_b;if(t_a.expt_b.exp)c.termList.push_back(t_a);iter_a+;elset_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.te
15、rmList.push_back(*iter_a);for(;iter_b!=b.termList.end();iter_b+)c.termList.push_back(*iter_b);Polynomial c1=c.paixu();return c1;Polynomial Polynomial:operator-(Polynomial list:iterator iter_a=termList.begin();list:iterator iter_b=b.termList.begin();while(iter_a!=termList.end()term t_a=(term)*iter_a;
16、term t_b=(term)*iter_b;if(t_a.expt_b.exp)c.termList.push_back(t_a);iter_a+;else if(t_a.exp:iterator iter_a=termList.begin();for(;iter_a!=termList.end();iter_a+)Polynomial c1;/这个 c1 的位置只能放在这里,局部变量 list:iterator iter_b=b.termList.begin();for(;iter_b!=b.termList.end();iter_b+)term temp;term t_a=(term)*
17、iter_a;term t_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+c1;return c;Polynomial Polynomial:operator/(Polynomial /被除数 list:iterator iter_a=termList.begin();for(;iter_a!=termList.end();iter_a+)term temp;term t_a=(term)*iter_a;temp.coef=t_a.coef
18、;temp.exp=t_a.exp;a.termList.push_back(temp); /-Polynomial quotient,remainder,temp1,temp2;list:iterator qa_a=a.termList.begin();list:iterator qb_b=b.termList.begin();term qa=(term)*qa_a;term qb=(term)*qb_b;temp1=a;/cout=qb.exp)term temp2_term;temp2_term.coef=qa.coef/qb.coef;temp2_term.exp=qa.exp-qb.
19、exp;quotient.termList.push_back(temp2_term);Polynomial center_quotient;center_quotient.termList.push_back(temp2_term);Polynomial tt=b*center_quotient;a=a-tt;a=a.paixu();list:iterator qa_1=a.termList.begin();qa=(term)*qa_1;/couti;while(i!=1)couti;doswitch(i)case 1:p1.Clear();p2.Clear();p1_1.Clear();p2_2.Clear();coutp1;coutp2;p1_1=p1.shunxu();/降幂 p2_2=p2.shunxu();/降幂couti;if(i=8)couti;if(i=8)couti;if(i=8)couti;if(i=8)couti;if(i=8)coutx;y=p1_1.Value(x);couti;if(i=8)couti;if(i=8)couti;if(i=8)cout“欢迎你下次使用“endl;break;while(i!=8);