| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 337 人关注过本帖
标题:一元多项式(链表类实现)求大神纠错
只看楼主 加入收藏
教皇
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2011-10-6
结帖率:95.65%
收藏
 问题点数:0 回复次数:0 
一元多项式(链表类实现)求大神纠错
程序的目的就是用链表类来表示一元多项式
#include<iostream.h>

template <typename T>
class linklist;//类定义
template <typename T>
class node
{
 friend class linklist<T>;
 T data;
 node <T>*next;
 public:
  node(){ next = NULL; }                    //无参数结点构造函数,用于未给定参数时结点的初始化
  node(T dat, node<T>* link){ data = dat; next = link; }
};

template <typename T>
class linklist//单链表类定义
{
 node<T>*head;
 public:
     listlink(){head=new node<T>;}
     ~listlink()
     {
      clear();
      delete head;
     }
     void clear();//删除动态空间函数
     int length();//返回节点数
     node<T>*find(T x);//查找函数返回node类指针
     node<T>*find(int i);//同上
     int insert(T x,int i);//插入函数,将一个T类数据插入链表
     int remove(int i);//移除函数,将一个T类数据移除
     T *get(int i);//查找函数返回T类指针
     
};

template <typename T>
void linklist<T>::clear()//删除动态空间
{
 node<T> *p;
 
 while(head->next!=NULL)
 {
  p=head->next;
  head->next=p->next;
  delete(p);
 }
}

template <typename T>
int linklist<T>::length()//计算线性表的长度
{
 node<T> *p=head->next;
 int count=0;
 while(p!=NULL)
 {
  count++;
  p=p->next;
 }
 return count;     
     
}

template<typename T>
node<T> *linklist<T>::find(T x)
{
 node<T> *p=head->next;
 while(p!=NULL&&p->data!=x)
   p=p->next;
 return p;
}

template<typename T>
node<T> *linklist<T>::find(int i)
{
 if(i<-1)
     return NULL;
 if(i==-1)
     return head;
 node<T>*p=head->next;
 int count=0;
 while(p!=NULL&&count<i)
 {
  count++;
  p=p->next;
 }
  return p;
}



template<typename T>
int linklist<T>::insert(T x,int i)
{
 node<T>*q=find(i-1);
 if(q==NULL)
     return 0;//位置不合理
 node <T> *p=new node<T>(x);
 p->next=q->next;
 q->next=p;
 return 1;//
}
template<typename T>
int linklist<T>::remove(T x,int i)
{
 node<T>*q=find(i-1);
 if(q==NULL)
     return 0;//位置不合理
 node <T> *p=q->next;
 
 q->next=p->next;
 delete p;
 return 1;//
}

template <typename T>
T *linklist<T>::get(int i)
{
 node <T>*p=find(i);
 if(p==NULL||p==head)
     return NULL;
 else
     return &p->data;//返回地址,T类型的地址

}





struct polydata//多项式
{
 float coef;//系数
 int exp;//指数

};

class polytion
{
 linklist <polydata> poly;
 public:
  polytion(){}//构造函数
  void show();
  void Insert(polydata &p,int i);
};

void polytion::Insert(polydata &p,int i)
{
 if(poly.insert(p,i)) cout<<"插入数据成功";
 else
     cout<<"插入数据失败";
}

void polytion::show()
{
 polydata *p1,*p2;

 int i=1;
 while(i<=poly.length())
 {
  p1=poly.get(i);
  if(p1->coef==0){}//当系数为0,不执行
  else if(p1->coef>0)
  {
   cout<<p1->coef;
   if(p1->exp==0){}//当指数为0,不执行
   else if(p1->exp>0) {cout<<'x'<<'^'<<p1->exp; }
   else{cout<<'x'<<'^'<<'('<<p1->exp<<')';}
  }
  else
  {
   cout<<p1->coef;
   if(p1->exp==0){}//当指数为0,不执行
   else if(p1->exp>0) {cout<<'x'<<'^'<<p1->exp; }
   else{cout<<'x'<<'^'<<'('<<p1->exp<<')';}
  }
  p2=poly.get(i+1);
  if(p2->coef>0) cout<<'+';
  i++;
 }
}


void main()
{
 polytion p;
 polydata t;
 int x,i=1,m;
 float n;
 cout<<"请输入项数:"<<endl;
 cin>>x;
 while(i<=x)
 {
  cout<<"请输入系数和指数"<<endl;
  cin>>n>>m;
  t.coef=n;
  t.exp=m;
  p.Insert(t,i);
  i++;
 
 }
 p.show();
}

[ 本帖最后由 教皇 于 2012-5-8 20:14 编辑 ]
搜索更多相关主题的帖子: next head include public friend 
2012-05-08 20:04
快速回复:一元多项式(链表类实现)求大神纠错
数据加载中...
 
   



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

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