| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1442 人关注过本帖
标题:链表类-多项式运算的链表类
只看楼主 加入收藏
想你的天空
Rank: 2
等 级:新手上路
威 望:5
帖 子:610
专家分:0
注 册:2004-12-30
收藏
 问题点数:0 回复次数:1 
链表类-多项式运算的链表类

#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(){} //~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); //前条件:存在多项式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; first = new polyNode<T>; 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->next!= NULL) && (p1->next!= NULL)) { 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; }

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;

s = new polyNode<T>; //把pb插入pc的最后1个节点 s->quotiety = p2->quotiety; s->quotiety_degree = p2->quotiety_degree; link->next = s; link =s; } }

while (p1 != NULL) //若pb的项数为空,则把pa继续插入pc的最后 { s = new polyNode<T>; s->quotiety = p1->quotiety; s->quotiety_degree = p1->quotiety_degree; link->next = s; link =s; }

while (p2 != NULL) //若pa的项数为空,则把pb继续插入pc的最后 { s = new polyNode<T>; s->quotiety = p2->quotiety; s->quotiety_degree = p2->quotiety_degree; link->next = s; link =s; } link->next = NULL; return this; } //多项式相减函数 template<class T> polyNomial<T> *polyNomial<T>::subPoly(polyNomial<T>& pa,polyNomial<T>& pb) { 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; }

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;

s = new polyNode<T>; //把pb插入pc的最后1个节点 s->quotiety = p2->quotiety; s->quotiety_degree = p2->quotiety_degree; link->next = s; link =s; } }

while (p1 != NULL) //若pb的项数为空,则把pa继续插入pc的最后 { s = new polyNode<T>; s->quotiety = p1->quotiety; s->quotiety_degree = p1->quotiety_degree; link->next = s; link =s; }

while (p2 != NULL) //若pa的项数为空,则把pb继续插入pc的最后 { s = new polyNode<T>; s->quotiety = p2->quotiety; s->quotiety_degree = p2->quotiety_degree; link->next = s; link =s; }

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,*s; 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; } p2 = pb.first; p1 = p1->next; } link->next = NULL; //整理相乘后的新链表:把相邻的元素比较,若指数相同的,则相加的结果放到比较的第1个项,并删除与之相连的项 link = first->next; q = link->next; for( ; link ; link = link->next,q = link->next) { if(link->quotiety_degree == q->quotiety_degree)//若指数相同的,则相加的结果放到比较的第1个项 { link->quotiety += q->quotiety; //指数相同的系数相加,指数不变 link->next = q->next; //删除与之相连的项 } }

}

//输出 template<class T> void polyNomial<T>::Output(ostream& outs) const { polyNode<T> *current; outs<<"结果是:"<<endl; for(current = first->next ; current != NULL; current = current->next) { if(current->quotiety_degree!=0) { if(current->quotiety>0) //系数为>0则不打印+,系数为>0则不打印- outs<<"+" << current->quotiety<<"y" <<" ^ "<<current->quotiety_degree; else outs<<"-" <<"y" <<" ^ "<<current->quotiety_degree; } //指数为0的情况,则不打印 x else { if(current->quotiety>0) outs<<"+" << current->quotiety; else outs<<"-" <<current->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,pc; pa.creatPoly(); pa.order(); cout<<"排序后的链表pa:"<<endl; cout<<pa<<endl;

pb.creatPoly(); pb.order(); cout<<"排序后的链表pb:"<<endl; cout<<pb<<endl;

pc.addPoly(pa,pb,pc); cout<<"输出相加后的链表:"<<endl; cout<<pc<<endl;

pc.subPoly(pa,pb,pc); cout<<"输出相减后的链表:"<<endl; cout<<pc<<end;

pc.mulPoly(pa,pb,pc) cout<<"输出2个多项式相乘的结果:"<<endl; cout<<pc<<endl;

system("pause"); return 0; }

// 开大仙救命啊,能成功输入和排序多项式,调用 addPoly函数 的时候就异常结束。

[此贴子已经被作者于2005-9-14 20:43:25编辑过]

搜索更多相关主题的帖子: 链表 多项式 class polyNomial template 
2005-09-14 20:25
想你的天空
Rank: 2
等 级:新手上路
威 望:5
帖 子:610
专家分:0
注 册:2004-12-30
收藏
得分:0 

2005-09-15 13:10
快速回复:链表类-多项式运算的链表类
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038043 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved