| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 562 人关注过本帖
标题:[求助]用循环单链实现的多项式计算
只看楼主 加入收藏
不懂就是不懂
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-9-24
收藏
 问题点数:0 回复次数:0 
[求助]用循环单链实现的多项式计算
[求助]用循环单链实现的多项式计算

/*----------main.cpp-----------*/

#include <iostream.h> #include <stdlib.h> #include "polynominal.h" void main() { float b; polynomial a; a.initpolynomial(); cout<<"the polynomial is:"; a.print(); cout<<endl; cout<<"when x is :"; cin>>b; cout<<endl; cout<<"the answer is:"; cout<<a.cacl(b)<<endl; }

/*----------polynomial.h---------------*/

#include <iostream.h> #include <stdlib.h> struct term { float coef;//系数 int exp;//指数 void init(float c,int e){coef=c;exp=e;}//初始化 }; class polynomial; template <class type>class CircList; template <class type>class node { friend class CircList<type>; private: public: node<type>*next; type data; node(node<type>*pnext=NULL); node (const type&item,node<type>*pnext=NULL); void setnext(node<type>*p){next=p;} void setdata(type x){data=x;}

}; template <class type>class CircList { friend class polynomial; private: node<type>*head; node<type>*pcurrent; public: CircList(); ~CircList(); int Length()const; int IsEmpty(){return head->next==head;}//循环链表中的定义,若为单链则为NULL node<type>*Reset(int i); node<type>*Next(); int EndofList()const;

}; class polynomial { public: float cacl(const float &x); void initpolynomial(); void print()const; private: CircList<term>poly; }; //结点类实现 template<class type>node<type>::node(node<type>*pnext) {next=pnext;} template<class type>node<type>::node(const type&item,node<type>*pnext) { data=item; next=pnext; } //链表类实现 template<class type>CircList<type>::CircList() { head=pcurrent=new node<type>(); head->next=head; } template<class type>CircList<type>::~CircList(){} template<class type>int CircList<type>::Length()const { node<type>*p=head->next; int len=0; while(p!=head) {len++;p=p->next;} return len; } template<class type>node<type>*CircList<type>::Reset(int i) { if(head==NULL||i<0)return NULL; if(i==0) {pcurrent=head;return head;} node<type>*p=head; int k=0; while(p->next!=head&&k<i) {p=p->next;k++;} pcurrent=p; return p; } template<class type>node<type>*CircList<type>::Next() { if(pcurrent!=NULL)pcurrent=pcurrent->next; return pcurrent; } template<class type>int CircList<type>::EndofList()const { if(pcurrent->next==head)return 1; else return 0; } //多项式类的实现 float polynomial::cacl(const float &x) { node<term>*p; term t; float sum=0,sum1,powX=x,j=x; float coeft; int expt; p=poly.Reset(1); while(!poly.EndofList()) { t=p->data; coeft=t.coef; expt=t.exp; powX=x; if(expt==0) {powX=1;} if(expt==1) {powX=x;} if(expt>1) { for(int i=1;i<expt;i++) {powX=powX*x;} } if(expt<0) { int l=expt-2*expt; for(int i=1;i<l;i++) { j=j*x; } powX=1/j; } sum1=coeft*powX; sum=sum+sum1; p=poly.Next(); } return sum; } void polynomial::initpolynomial() /*Post:the polynomial is read from cin*/ { int y; cout<<"请确定多项式的项数为:"; cin>>y; for(int i=1;i<=y;i++) { node<term>* temp=new node<term>; int c=y+1-i; cout<<"第"<<c<<"项的指数"<<"是:"; cin>>temp->data.exp; cout<<"第"<<c<<"项的系数"<<"是:"; cin>>temp->data.coef; temp->next=poly.Reset(0); poly.Reset(0)->next=temp; } } void polynomial::print()const /*Post:the polynomial is printed to cout*/ { node<term>*p=poly.head; while(p->next==poly.head) {cerr<<"链表为空"<<endl;} p=p->next; cout<<p->data.coef; if(p->data.exp==0) cout<<""; if(p->data.exp==1) cout<<"x"; if(p->data.exp!=1&&p->data.exp!=0) cout<<"x^"<<p->data.exp; p=p->next; while(p->next!=poly.head) { if(p->data.coef<0) cout<<p->data.coef; if(p->data.coef>0) cout<<"+"<<p->data.coef; if(p->data.exp==0) cout<<""; if(p->data.exp==1) cout<<"x"; if(p->data.exp!=1&&p->data.exp!=0) cout<<"x^"<<p->data.exp; p=p->next; } }

输出: 请确定多项式的项数为:3 第3项的指数是:3 第3项的系数是:3 第2项的指数是:4 第2项的系数是:4 第1项的指数是:5 第1项的系数是:5 the polynomial is:5x^5-4.31602e+008x^-842150451 when x is :2

the answer is:0 Press any key to continue...

不知道是那里错了使初始化(我猜,或者其他问题)出错导致不能实现程序的功能,希望有高手相助,给找找错误,谢谢!

搜索更多相关主题的帖子: 单链 多项式 
2005-09-24 19:41
快速回复:[求助]用循环单链实现的多项式计算
数据加载中...
 
   



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

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