原文见http://best.xiao.blog.163.com
#include<iostream.h>#include<stdlib.h> void menuupdown(); //声明菜单的某个部分class xiang{ private: double valuea; //声明系数 double valueb; //声明指数 double corp; //在做和是只是是否计算过 xiang * next; //指向下一个元素的地址 public: xiang(double ax,double bx) //有参数构造函数,主要用于生成临时一元多项式的项和头指针 { valuea=ax; valueb=bx; corp=0; next=NULL; } xiang() //无参数构造函数,主要用于生成一元多项式时使用 { cin>>valuea>>valueb; corp=0; next=NULL; } friend class caozuo; //声明类caozuo为类xiang的友元类};void banquan(){ cout<<endl<<"/tThis programme made by Xiaodong. QQ:190675894"<<endl; cout<<endl<<"/tInner Mongolia University of Science and Technology"<<endl; cout<<endl<<"/t内蒙古科技大学 计算机科学与技术系"<<endl;}//end of class xiang//---------------------------------------------------------------//begin of class caozuoclass caozuo{private: xiang * head,*record; //定义head指向头指针,定义record记录链表最后指针位置,并用于链接链表public: caozuo(); //声明caozuo函数,用于生成链表头 xiang * gethead(); //取出多项式的头地址 void creat(); //声明creat函数,用于生成链表,即输入多项式 xiang* addsame(xiang* atemp); //声明函数,主要将单个多项式中相同的项相加 caozuo operator * (caozuo lasts); //声明函数,重载+符号用于多项式相加 void show(xiang * show); //声明函数,显示多项式 caozuo operator + (caozuo lasts); //声明函数,重载*符号用于多项式相乘};//end of class caozuo//---------------------------------------------------------------//begin of function caozuo() in class caozuocaozuo::caozuo() //caozuo构造函数实现过程 { head= new xiang(0,0); //使head指向头结点 record=head; //record初始化指向头指针 }//end of function caozuo in class caozuo//---------------------------------------------------------------//begin of function gethead() in class caozuoxiang * caozuo::gethead() //gethead函数实现过程 { return head; //返回头指针 }//end of function gethead() in class caozuo//---------------------------------------------------------------//begin of function creat() in class caozuovoid caozuo::creat() //creat函数实现过程 { int i,j; cout<<"请输入多项式项数:"<<endl; ::menuupdown(); //显示一长串--------- cout<<endl; cin>>j; for(i=1;i<=j;i++) { // 依次输入m个非零项 cout<<"输入第"<<i<<"非零项(系数 指数)"<<endl; xiang * p=new xiang; record->next=p; //以下两句是使链表相连 record=p; // } }//end of function creat() in class caozuo//---------------------------------------------------------------//begin of function addsame in class caozuoxiang* caozuo::addsame(xiang* atemp) { xiang * h=atemp; //声明的h指针,指向待检查的项 xiang * h1=atemp; //声明h1指针,指向被比较的项 xiang * htemp; //用作删除项 while(h1->next) //使被检查项指针在h(待检查项)循环完毕后循环 { htemp=h1; if(h->next) //若果h-next不为空,那么指向下一项 h=h->next; else h->next=NULL; while(h->valueb==h1->valueb&&htemp->next) //比较“待检查的项”系数与“被检查项”系数是否相等 { h1->valuea=h1->valuea+h->valuea; //相等的话使之相加,并把结果放到被检查项处 if(h->next) //如果待检查项的下一项不为空,那么指向,即删除待检查项 { htemp->next=h->next; h=h->next; } else { htemp->next=NULL; h->next=NULL; } } if(h1->next) h1=h1->next; else h1->next=NULL; } return atemp; }//end of function addsame in class caozuo//---------------------------------------------------------------//begin of function show in class caozuo void caozuo::show(xiang * show) { int i=0; if(show->next) //忽略头指针 show=show->next; //具体显示过程 while(show) { if(i>0&&show->valuea>0) //如果不是第一项,那么在前面加上+符号 cout<<"+"; if(show->valuea==1) //如果系数为1 if(show->valueb==1) //如果指数为1 cout<<"X"; else cout<<"X^"<<show->valueb; else if(show->valueb==1) cout<<show->valuea<<"X"; else cout<<show->valuea<<"X^"<<show->valueb; show=show->next; i=i+1; } cout<<endl; }//end of function show in class caozuo//---------------------------------------------------------------//begin of function operator * in class caozuocaozuo caozuo::operator * (caozuo lasts) { xiang * last=lasts.gethead(); //声明last指向被乘对象的头指针 if(last->next) last=last->next; caozuo answer; //声明定义临时answer对象,用于保存相乘结果 xiang * p=answer.gethead(); //p用于操作结果链表 xiang * headlast=last; //headlast用于操作被乘链表 caozuo thistemp=*this; xiang * headfirst=thistemp.gethead(); //取出乘号前的链表的头地址 if(headfirst->next) headfirst=headfirst->next; xiang * record=p; //record用于链接结果链表 //相乘过程,两层循环遍历所有乘数 while(headfirst) { while(headlast) { double valueatemp,valuebtemp; valueatemp=headfirst->valuea*headlast->valuea; valuebtemp=headfirst->valueb*headlast->valueb; xiang * temp=new xiang(valueatemp,valuebtemp); record->next=temp; record=temp; headlast=headlast->next; } headlast=last; headfirst=headfirst->next; } return answer; //返回结果对象 }//end of function operator * in class caozuo//---------------------------------------------------------------//begin of function operator + in class caozuo caozuo caozuo::operator + (caozuo lasts) { xiang * last=lasts.gethead(); //声明last指向被加对象的头指针 if(last->next) last=last->next; caozuo answer; //声明定义临时answer对象,用于保存相加结果 xiang * p=answer.gethead(); //p用于操作结果链 xiang * headlast=last; //headlast用于操作被加链表 caozuo thistemp=*this; xiang * headfirst=thistemp.gethead(); //取出加号前的链表的头地址 if(headfirst->next) headfirst=headfirst->next; xiang * record=p; //record用于链接结果链表 //相加过程,两层循环遍历所有加数 while(headfirst) { double answertemp=0; int flag=0; while(headlast) { if(headlast->valueb==headfirst->valueb) //如果两项指数相等 { if(flag==0) //如果是第一次执行 { answertemp=headlast->valuea+headfirst->valuea; flag=1; } else { answertemp=answertemp+headlast->valuea; } headlast->corp=1; //记录第二多项式项已相加 } headlast=headlast->next; } if(answertemp!=0) //若果相加过,即两式中有相同指数的项存在 { xiang * temp=new xiang(answertemp,headfirst->valueb); record->next=temp; record=temp; } else { xiang * temp=new xiang(headfirst->valuea,headfirst->valueb); record->next=temp; record=temp; } headlast=last; headfirst=headfirst->next; } headlast=last; while(headlast) //再次在第二多项式里遍历,找corp为零的项,即此项未相加过 { if(headlast->corp==0) { xiang * temp=new xiang(headlast->valuea,headlast->valueb); record->next=temp; record=temp; } if(headlast->next) headlast=headlast->next; else headlast=NULL; } return answer; }//end of function operator + in class caozuo//---------------------------------------------------------------//begin of function menuupdown in globalvoid menuupdown() //显示15次━,用作菜单{ int i; for(i=0;i<15;i++) cout<<"━";}//end of function menuupdown in global//---------------------------------------------------------------//begin of function menu in globalvoid menu() //菜单函数{ int i; for(i=0;i<2;i++) cout<<endl; menuupdown(); menuupdown(); banquan(); menuupdown(); menuupdown(); for(i=0;i<2;i++) cout<<endl; cout<<"/t/t/t"; cout<<"┏"; menuupdown(); cout<<"┓"<<endl; cout<<"/t/t/t┃/t/t/t/t┃"<<endl; cout<<"/t/t/t┃/t1,输入一元多项式。/t┃"<<endl; cout<<"/t/t/t┃/t2,输出一元多项式。/t┃"<<endl; cout<<"/t/t/t┃/t3,两多项式相加。/t┃"<<endl; cout<<"/t/t/t┃/t4,两多项式相乘。/t┃"<<endl; cout<<"/t/t/t┃/t5,退出。/t/t┃"<<endl; cout<<"/t/t/t┃/t/t/t/t┃"<<endl; cout<<"/t/t/t"; cout<<"┗"; menuupdown(); cout<<"┛"<<endl;}//end of function menu in global//---------------------------------------------------------------//begin of function main in globalvoid main(){ char inputchoose,YesOrNo; //声明两变量 ,分别保存选项,是否退出 int select=0; //标示是否已创建对象,如果已创建,则会赋值为1 caozuo news1; //声明第一个操作多项式 caozuo news2; //声明第二个操作多项式 caozuo answeradd; //声明,用于相加后保存结果 caozuo answermultiply; //声明,用于相乘后保存结果 xiang * heada; xiang * headb; xiang * mutiplytemp; do { system("cls"); //清屏函数 menu(); cout<<"/n/n/n"<<endl<<" 请输入选择"<<endl; menuupdown(); cout<<endl; cin>>inputchoose; switch(inputchoose) { case '1': //调用创建过程 system("cls"); news1.creat(); news2.creat(); select=1; heada=news1.gethead(); heada=news1.addsame(heada); headb=news2.gethead(); headb=news2.addsame(headb); system("pause"); //暂停函数,等待用户操作 break; case '2': //调用显示过程 if(select==1) { system("cls"); cout<<"/t多项式一为:/t"; cout<<" "; news1.show(heada); menuupdown(); cout<<endl; cout<<"/t多项式二为:/t"; cout<<" "; news2.show(headb); } else { cout<<"还没有创建对象!"<<endl; } system("pause"); break; case '3': //调用相加过程 if(select==1) { system("cls"); answeradd=news1+news2; cout<<" "; cout<<"/t多项式和为:/t"<<endl; menuupdown(); cout<<endl; answeradd.show(answeradd.gethead()); } else { cout<<"还没有创建对象!"<<endl; } system("pause"); break; case '4': //调用相乘过程 if(select==1) { system("cls"); answermultiply=news1*news2; cout<<" "; cout<<"/t多项式积为:/t"<<endl; menuupdown(); cout<<endl; mutiplytemp=answermultiply.gethead(); mutiplytemp=answermultiply.addsame(mutiplytemp); answermultiply.show(mutiplytemp); } else { cout<<"还没有创建对象!"<<endl; } system("pause"); break; case '5':YesOrNo='N';break; } }while(YesOrNo!='N'&&YesOrNo!='n');}//end main