#include<iostream> using namespace std;
template<class T> class polyNomial; template<class T> class polyNode { friend class polyNomial<T>; public: polyNode(){} private: T quotiety; //项的系数 T quotiety_degree;//项的指数 polyNode<T> *next; };
template<class T> ostream& operator<<(ostream& outs,const polyNomial<T>& x);
template<class T> class polyNomial { public: polyNomial(){ first = new polyNode<T>; } //~polyNomial(); polyNomial<T> *creatPoly(); //操作结果:输入m项的系数和指数,建立一元多项式p bool isEmpty(); //多项式存在返回false,否则返回true polyNomial<T> *order(); polyNomial<T> *addPoly(polyNomial<T>& pa,polyNomial<T>& pb, polyNomial<T>& pc); //前条件:存在多项式pa,pb //后条件:完成多项式加法运算,pa = pa+pb,并销毁pb polyNomial<T> *subPoly(polyNomial<T>& pa,polyNomial<T>& pb,polyNomial<T>& pc); //前条件:存在多项式pa,pb //后条件:完成多项式加法运算,pa = pa-pb,并销毁pb polyNomial<T> *mulPoly(polyNomial<T>& pa,polyNomial<T>& pb, polyNomial<T>& pc); //前条件:存在多项式pa,pb //后条件:完成多项式加法运算,pa = pa*pb,并销毁pb void Output(ostream& outs) const ; //前条件:存在多项式 //后条件:输出多项式 ostream& operator<< <>(ostream& outs,const polyNomial<T>& x); //重载输出流 private: mutable T polyNumber; //项数 polyNode<T> *first; }; #include "multinomial.h"
//创建多项式函数 template<class T> polyNomial<T> *polyNomial<T>::creatPoly() { polyNode<T> *link,*s; link = first; cout<<"输入多项式的项数"<<endl; cin>> polyNumber ; for(int i = 0; i < polyNumber ; i++) { cout<<"输入多项式的系数"<<endl; s = new polyNode<T>; cin>>s->quotiety; cout<<"输入多项式的指数"<<endl; cin>>s->quotiety_degree;
link->next = s; link = s; } link->next = NULL; return this; }
/* //析构函数 template<class T> polyNomial<T>::~polyNomial<T>() { polyNode<T>* link; while(first) { link = first->next; delete first; first = link; } } */
//判断多项式是否为空 template<class T> bool polyNomial<T>::isEmpty() { if(first == NULL) return true; else return false; }
template<class T> polyNomial<T> *polyNomial<T>::order() { polyNode<T> *p = first->next; polyNode<T> *q = first->next; T _quotiety; T _quotiety_degree;
for( ; p ; p = p->next ) for( q = p->next ; q ; q = q->next ) if( (p->quotiety_degree)< (q->quotiety_degree)) { _quotiety = p->quotiety ; _quotiety_degree = p->quotiety_degree; p->quotiety = q->quotiety; p->quotiety_degree = q->quotiety_degree; q->quotiety = _quotiety; q->quotiety_degree = _quotiety_degree; } return this; }
//多项式相加函数 template<class T> polyNomial<T> *polyNomial<T>::addPoly(polyNomial<T>& pa,polyNomial<T>& pb,polyNomial<T>& pc) { //多项式的项已经以指数低到高的顺序排序了 polyNode<T> *p1 = pa.first->next; polyNode<T> *p2 = pb.first->next; polyNode<T> *link,*s; link = pc.first; // link 指向pc多项式(初始pc是空的)
while( p1 && p2 ) { if((p1->quotiety_degree)==(p2->quotiety_degree)) { //如果指数相同,则pa,pb的系数相加 s = new polyNode<T>; s->quotiety = p1->quotiety + p2->quotiety; s->quotiety_degree = p1->quotiety_degree;
link->next = s; link = s; p1 = p1->next ; p2 = p2->next ; }
else { //pa,pb的指数不同 s = new polyNode<T>; //把pa插入pc的最后1个节点 s->quotiety = p1->quotiety; s->quotiety_degree = p1->quotiety_degree; link->next = s; link =s; p1 = p1->next ; s = new polyNode<T>; //把pb插入pc的最后1个节点 s->quotiety = p2->quotiety; s->quotiety_degree = p2->quotiety_degree; link->next = s; link =s; p2 = p2->next ; } }
while (p1) //若pb的项数为空,则把pa继续插入pc的最后 { s = new polyNode<T>; s->quotiety = p1->quotiety; s->quotiety_degree = p1->quotiety_degree; link->next = s; link =s; p1 = p1->next ; }
while (p2 ) //若pa的项数为空,则把pb继续插入pc的最后 { s = new polyNode<T>; s->quotiety = p2->quotiety; s->quotiety_degree = p2->quotiety_degree; link->next = s; link =s; p2 = p2->next ; } link->next = NULL; return this; }
//多项式相减函数 template<class T> polyNomial<T> *polyNomial<T>::subPoly(polyNomial<T>& pa,polyNomial<T>& pb,polyNomial<T>& pc) { //多项式的项已经以指数低到高的顺序排序了 polyNode<T> *p1 = pa.first->next; polyNode<T> *p2 = pb.first->next; polyNode<T> *link,*s; link = pc.first; // link 指向pc多项式(初始pc是空的)
while( p1 && p2 ) { if((p1->quotiety_degree)==(p2->quotiety_degree)) { //如果指数相同,则pa,pb的系数相加 s = new polyNode<T>; s->quotiety = p1->quotiety - p2->quotiety; s->quotiety_degree = p1->quotiety_degree;
link->next = s; link = s; p1 = p1->next ; p2 = p2->next ; }
else { //pa,pb的指数不同 s = new polyNode<T>; //把pa插入pc的最后1个节点 s->quotiety = p1->quotiety; s->quotiety_degree = p1->quotiety_degree; link->next = s; link =s; p1 = p1->next ;
s = new polyNode<T>; //把pb插入pc的最后1个节点 s->quotiety = p2->quotiety; s->quotiety_degree = p2->quotiety_degree; link->next = s; link =s; p2 = p2->next ; } }
while (p1) //若pb的项数为空,则把pa继续插入pc的最后 { s = new polyNode<T>; s->quotiety = p1->quotiety; s->quotiety_degree = p1->quotiety_degree; link->next = s; link =s; p1 = p1->next ; }
while (p2 ) //若pa的项数为空,则把pb继续插入pc的最后 { s = new polyNode<T>; s->quotiety = p2->quotiety; s->quotiety_degree = p2->quotiety_degree; link->next = s; link =s; p2 = p2->next ; } link->next = NULL; return this; } //多项式相乘函数 template<class T> polyNomial<T> *polyNomial<T>::mulPoly(polyNomial<T>& pa,polyNomial<T>& pb, polyNomial<T>& pc) { polyNode<T> *p1 = pa.first->next; polyNode<T> *p2 = pb.first->next; polyNode<T> *q; polyNode<T> *link,*s; link = pc.first; // link 指向pc多项式(初始pc是空的) while(p1) { while(p2) //pa里的第1项和pb里的每一项,类推 { s = new polyNode<T>; s->quotiety = p1->quotiety * p2->quotiety; s->quotiety_degree =p1->quotiety_degree + p2->quotiety_degree; link->next = s; link = s; if(p2) p2 = p2->next;
} if(p1) p1 = p1->next; p2 = pb.first->next;
} link->next = NULL;
order(); //按指数从高到低排序
//整理相乘后的新链表:把相邻的元素比较,若指数相同的,则相加的结果放到比较的第2个项,并删除与之相连的项 link = pc.first ; //link指向当前指针的前驱
q = link->next ; for( ; q->next!=NULL ; link = link->next , q = link->next) { if(q->quotiety_degree == q->next->quotiety_degree)//若指数相同的,则把相加的结果放到比较的第1个项 { q->next->quotiety += q->quotiety; //指数相同的系数相加,指数不变 if(q->next!=NULL) link->next = q->next; //删除与之相连的项 q = q->next ; } } return this ; } //输出 template<class T> void polyNomial<T>::Output(ostream& outs) const { polyNode<T> *current; polyNode<T> *p; outs<<"结果是:"<<endl; current = first ; for( p = current->next ; p != NULL; current = current->next , p = current->next) //current指向当前节点的前驱, p指向当前节点 { if(p->quotiety == 0) //如果项系数是0的项,则删除 { current->next = p->next ; p = p->next ; } if(p->quotiety_degree!=0) { if(p->quotiety>0) //系数为>0则不打印+,系数为>0则不打印- outs<<"+" << p->quotiety<<"y" <<" ^ "<<p->quotiety_degree; else outs<<"-" <<"y" <<" ^ "<<p->quotiety_degree; } //指数为0的情况,则不打印 x else { if(p->quotiety>0) outs<<"+" << p->quotiety; else if(p->quotiety == 0) outs<<p->quotiety; else outs<<"-" <<p->quotiety; } } } //重载<< template<class T> ostream& operator<<(ostream& outs,const polyNomial<T>& x) { x.Output(outs); return outs; } #include"multinomial.cpp" int main() { polyNomial<int> pa,pb,add,sub,mul; pa.creatPoly(); pa.order(); cout<<"排序后的链表pa:"<<endl; cout<<pa<<endl;
pb.creatPoly(); pb.order(); cout<<"排序后的链表pb:"<<endl; cout<<pb<<endl;
add.addPoly(pa,pb,add); cout<<"输出相加后的链表:"<<endl; cout<<add<<endl;
sub.subPoly(pa,pb,sub); cout<<"输出相减后的链表:"<<endl; cout<<sub<<endl;
mul.mulPoly(pa,pb,mul); cout<<"输出2个多项式相乘的结果:"<<endl; cout<<mul<<endl;
system("pause"); return 0; }
自己用断点调试, 终于可以用了, 谢谢kai斑竹的提醒, 您的1句话,胜过帮我改一万个代码!