C++版数据结构课程设计(一元多项式)
以下是类的具体操作全文请见我的博客[url=http://best.xiao.blog.]http://best.xiao.blog.[/url]
#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 caozuo
class 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 caozuo
caozuo::caozuo() //caozuo构造函数实现过程
{
head= new xiang(0,0); //使head指向头结点
record=head; //record初始化指向头指针
}
//end of function caozuo in class caozuo
//---------------------------------------------------------------
//begin of function gethead() in class caozuo
xiang * caozuo::gethead() //gethead函数实现过程
{
return head; //返回头指针
}
//end of function gethead() in class caozuo
//---------------------------------------------------------------
//begin of function creat() in class caozuo
void 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 caozuo
xiang* 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 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)
{
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 global
void menuupdown() //显示15次━,用作菜单
{ int i;
for(i=0;i<15;i++)
cout<<"━";
}