多项式(运行环境VS2008)
来源:互联网 发布:微软远程桌面 mac 编辑:程序博客网 时间:2024/05/16 06:49
Polynomial.h
class polynomial
{
public:
polynomial();//默认构造函数
polynomial(int,int);//重载构造函数
~polynomial();//析构函数
int getCoefficient(int);
int getIndex(int);
void setPolynomial(int,int);//设置值
polynomial &operator+( polynomial &);//重载运算符+
polynomial &operator-(polynomial &);//重载运算符-
polynomial &operator*(polynomial &);//重载运算符*
polynomial &operator=(polynomial &);//重载运算符=
polynomial & operator+=(polynomial &);//重载运算符+=
polynomial & operator-=(polynomial &);//重载运算符-=
polynomial & operator*=(polynomial &);//重载运算符*=
void printPolynomial();//输出多项式
private:
struct polyNode{
int Coefficient;//多项式系数
int Index;//多项式指数
struct polyNode *next;
};
polyNode *p;
polyNode *head;
static int size;
};
Polynomial.cpp:
#include<iostream>
#include<assert.h>
#include"polynomial.h"
using namespace std;
int polynomial::size=0;
polynomial::polynomial()
{
size=1;
head=new polyNode;
p=head;
assert(p!=0);
p->Coefficient=0;
p->Index=0;
p->next=NULL;
}
polynomial::polynomial(int c,int i)
{
size=1;
head=new polyNode;
p=head;
setPolynomial(c,i);
}
polynomial::~polynomial()
{
polyNode *q;
q=head;
while(head->next!=NULL)
{
q=q->next;
delete head;
head=q;
size--;
}
delete head;
}
void polynomial::setPolynomial(int c,int i)
{
p->Coefficient=c;
p->Index=i;
polyNode *q;
q=new polyNode;
p->next=q;
p=q;
p->next=NULL;
p->Coefficient=0;
p->Index=0;
size++;
}
int polynomial::getCoefficient(int s)
{
int i=0;
polyNode *q;
q=head;
while(i<s && q!=p)
{
q=q->next;
i++;
}
return q->Coefficient;
}
int polynomial::getIndex(int s)
{
int i=0;
polyNode *q;
q=head;
while(i<s && q!=p)
{
q=q->next;
i++;
}
return q->Index;
}
polynomial &polynomial::operator+( polynomial &a)
{
int judge;
polyNode *q;
q=head;
polyNode *t;
t=a.head;
polynomial *sum=new polynomial;
int in,c;
while(q!=p)
{
t=a.head;
judge=0;
while(t!=a.p)
{
if(t->Index==q->Index)
{
in=t->Index;
c=t->Coefficient+q->Coefficient;
sum->setPolynomial(c,in);
judge=1;
}
t=t->next;
}
if(judge==0)
sum->setPolynomial(q->Coefficient,q->Index);
q=q->next;
}
t=a.head;
polyNode *r;
while(t!=a.p)
{
r=sum->head;
judge=0;
while(r!=sum->p)
{
if(t->Index==r->Index)
{
judge=0;
break;
}
else
judge=1;
r=r->next;
}
if(judge==1)
sum->setPolynomial(t->Coefficient,t->Index);
t=t->next;
}
polyNode *g,*h,*w;
w=new polyNode;
g=sum->head;
while(g!=sum->p)
{
h=g->next;
while(h!=sum->p)
{
if(h->Index>g->Index)
{
w->Coefficient=g->Coefficient;
w->Index=g->Index;
g->Coefficient=h->Coefficient;
g->Index=h->Index;
h->Coefficient=w->Coefficient;
h->Index=w->Index;
}
h=h->next;
}
g=g->next;
}
delete w;
return *sum;
}
polynomial &polynomial::operator-(polynomial &a)
{
int judge;
polyNode *q;
q=head;
polyNode *t;
t=a.head;
polynomial *sub=new polynomial;
int in,c;
while(q!=p)
{
t=a.head;
judge=0;
while(t!=a.p)
{
if(t->Index==q->Index)
{
in=t->Index;
c=q->Coefficient-t->Coefficient;
sub->setPolynomial(c,in);
judge=1;
}
t=t->next;
}
if(judge==0)
sub->setPolynomial(q->Coefficient,q->Index);
q=q->next;
}
t=a.head;
polyNode *r;
while(t!=a.p)
{
r=sub->head;
judge=0;
while(r!=sub->p)
{
if(t->Index==r->Index)
{
judge=0;
break;
}
else
judge=1;
r=r->next;
}
if(judge==1)
sub->setPolynomial(-t->Coefficient,t->Index);
t=t->next;
}
polyNode *g,*h,*w;
w=new polyNode;
g=sub->head;
while(g!=sub->p)
{
h=g->next;
while(h!=sub->p)
{
if(h->Index>g->Index)
{
w->Coefficient=g->Coefficient;
w->Index=g->Index;
g->Coefficient=h->Coefficient;
g->Index=h->Index;
h->Coefficient=w->Coefficient;
h->Index=w->Index;
}
h=h->next;
}
g=g->next;
}
delete w;
return *sub;
}
polynomial &polynomial::operator*(polynomial &a)
{
polynomial *mul=new polynomial;
polyNode *q,*t,*r;
int c,in,judge;
q=head;
while(q!=p)
{
t=a.head;
while(t!=a.p)
{
c=q->Coefficient*t->Coefficient;
in=q->Index+t->Index;
r=mul->head;judge=0;
while(r!=mul->p)
{
if(in==r->Index)
{
judge=1;
r->Coefficient+=c;
break;
}
r=r->next;
}
if(judge==0)
mul->setPolynomial(c,in);
t=t->next;
}
q=q->next;
}
polyNode *g,*h,*w;
w=new polyNode;
g=mul->head;
while(g!=mul->p)
{
h=g->next;
while(h!=mul->p)
{
if(h->Index>g->Index)
{
w->Coefficient=g->Coefficient;
w->Index=g->Index;
g->Coefficient=h->Coefficient;
g->Index=h->Index;
h->Coefficient=w->Coefficient;
h->Index=w->Index;
}
h=h->next;
}
g=g->next;
}
delete w;
return *mul;
}
polynomial &polynomial::operator=(polynomial &right)
{
polyNode *q;
q=head;
while(head!=p){head=head->next;delete q;q=head;size--;}
polyNode *t;
t=right.head;
while(t!=right.p)
{
setPolynomial(t->Coefficient,t->Index);
t=t->next;
}
return *this;
}
polynomial &polynomial::operator+=(polynomial &a)//重载运算符+=
{
int judge;
polyNode *q;
q=head;
polyNode *t;
t=a.head;
polynomial *sum=new polynomial;
int in,c;
while(q!=p)
{
t=a.head;
judge=0;
while(t!=a.p)
{
if(t->Index==q->Index)
{
in=t->Index;
c=t->Coefficient+q->Coefficient;
sum->setPolynomial(c,in);
judge=1;
}
t=t->next;
}
if(judge==0)
sum->setPolynomial(q->Coefficient,q->Index);
q=q->next;
}
t=a.head;
polyNode *r;
while(t!=a.p)
{
r=sum->head;
judge=0;
while(r!=sum->p)
{
if(t->Index==r->Index)
{
judge=0;
break;
}
else
judge=1;
r=r->next;
}
if(judge==1)
sum->setPolynomial(t->Coefficient,t->Index);
t=t->next;
}
polyNode *g,*h,*w;
w=new polyNode;
g=sum->head;
while(g!=sum->p)
{
h=g->next;
while(h!=sum->p)
{
if(h->Index>g->Index)
{
w->Coefficient=g->Coefficient;
w->Index=g->Index;
g->Coefficient=h->Coefficient;
g->Index=h->Index;
h->Coefficient=w->Coefficient;
h->Index=w->Index;
}
h=h->next;
}
g=g->next;
}
delete w;
q=head;
while(head->next!=NULL)
{
q=q->next;
delete head;
head=q;
size--;
}
delete head;
head=sum->head;
p=sum->p;
return *this;
}
polynomial &polynomial:: operator-=(polynomial &a)//重载运算符-=
{
int judge;
polyNode *q;
q=head;
polyNode *t;
t=a.head;
polynomial *sub=new polynomial;
int in,c;
while(q!=p)
{
t=a.head;
judge=0;
while(t!=a.p)
{
if(t->Index==q->Index)
{
in=t->Index;
c=q->Coefficient-t->Coefficient;
sub->setPolynomial(c,in);
judge=1;
}
t=t->next;
}
if(judge==0)
sub->setPolynomial(q->Coefficient,q->Index);
q=q->next;
}
t=a.head;
polyNode *r;
while(t!=a.p)
{
r=sub->head;
judge=0;
while(r!=sub->p)
{
if(t->Index==r->Index)
{
judge=0;
break;
}
else
judge=1;
r=r->next;
}
if(judge==1)
sub->setPolynomial(-t->Coefficient,t->Index);
t=t->next;
}
polyNode *g,*h,*w;
w=new polyNode;
g=sub->head;
while(g!=sub->p)
{
h=g->next;
while(h!=sub->p)
{
if(h->Index>g->Index)
{
w->Coefficient=g->Coefficient;
w->Index=g->Index;
g->Coefficient=h->Coefficient;
g->Index=h->Index;
h->Coefficient=w->Coefficient;
h->Index=w->Index;
}
h=h->next;
}
g=g->next;
}
delete w;
q=head;
while(head->next!=NULL)
{
q=q->next;
delete head;
head=q;
size--;
}
delete head;
head=sub->head;
p=sub->p;
return *this;
}
polynomial &polynomial::operator*=(polynomial &a)//重载运算符*=
{
polynomial *mul=new polynomial;
polyNode *q,*t,*r;
int c,in,judge;
q=head;
while(q!=p)
{
t=a.head;
while(t!=a.p)
{
c=q->Coefficient*t->Coefficient;
in=q->Index+t->Index;
r=mul->head;judge=0;
while(r!=mul->p)
{
if(in==r->Index)
{
judge=1;
r->Coefficient+=c;
break;
}
r=r->next;
}
if(judge==0)
mul->setPolynomial(c,in);
t=t->next;
}
q=q->next;
}
polyNode *g,*h,*w;
w=new polyNode;
g=mul->head;
while(g!=mul->p)
{
h=g->next;
while(h!=mul->p)
{
if(h->Index>g->Index)
{
w->Coefficient=g->Coefficient;
w->Index=g->Index;
g->Coefficient=h->Coefficient;
g->Index=h->Index;
h->Coefficient=w->Coefficient;
h->Index=w->Index;
}
h=h->next;
}
g=g->next;
}
delete w;
q=head;
while(head->next!=NULL)
{
q=q->next;
delete head;
head=q;
size--;
}
delete head;
head=mul->head;
p=mul->p;
return *this;
}
void polynomial::printPolynomial()
{
polyNode *q;q=head;
while(q!=p)
{
if(q==head && head->Index==0 && head->Coefficient!=0)
cout<<q->Coefficient;
else if(q==head && head->Index!=0 && head->Coefficient!=0)
{
if(head->Coefficient==1)
cout<<"x^"<<q->Index;
else if(head->Coefficient==-1)
cout<<"-x^"<<q->Index;
else
cout<<q->Coefficient<<"x^"<<q->Index;
}
else if(q!=head && q->Coefficient!=0)
{
if(q->Coefficient>0 && q->Coefficient!=1)
{
if(q->Index!=0)
cout<<"+"<<q->Coefficient<<"x^"<<q->Index;
else
cout<<"+"<<q->Coefficient;
}
if(q->Coefficient==1)
{
if(q->Index!=0)
cout<<"+x^"<<q->Index;
else
cout<<"+1";
}
if(q->Coefficient<0 && q->Coefficient!=-1)
{
if(q->Index!=0)
cout<<q->Coefficient<<"x^"<<q->Index;
else
cout<<q->Coefficient;
}
if(q->Coefficient==-1)
{
if(q->Index!=0)
cout<<"-x^"<<q->Index;
else
cout<<"-1";
}
}
q=q->next;
}
}
testMain.cpp:
#include<iostream>
#include"polynomial.h"
using namespace std;
int main()
{
int size,c,in;
polynomial x[2],z[4],y;
cout<<"请输入两个多项式:"<<endl;//输入两个多项式
for(int j=0;j<2;j++)
{
cout<<"请输出该多项式共有多少项:";
cin>>size;
for(int i=0;i<size;i++)
{
cout<<"请输入系数:";
cin>>c;
cout<<"请输入指数:";
cin>>in;
x[j].setPolynomial(c,in);
}
}
//输出两个多项式
for(int k=0;k<2;k++)
{
cout<<"x["<<k<<"]=";
x[k].printPolynomial();
cout<<endl;
}
//重载+运算符
z[0]=x[0]+x[1];
cout<<"z[0]=";
z[0].printPolynomial();
cout<<"------z[0]=x[0]+x[1]"<<endl;
//重载运算符-
z[1]=x[0]-x[1];
cout<<"z[1]=";
z[1].printPolynomial();
cout<<"------z[1]=x[0]-x[1]"<<endl;
//重载运算符+=
x[0]+=x[1];
cout<<"x[0]=";
x[0].printPolynomial();
cout<<"-----x[0]+=x[1]"<<endl;
//重载运算符-=
x[0]-=x[1];
cout<<"x[0]=";
x[0].printPolynomial();
cout<<"-----x[0]-=x[1]"<<endl;
//重载运算符*
z[2]=x[0]*x[1];
cout<<"z[2]=";
z[2].printPolynomial();
cout<<"----z[2]=x[0]*x[1]"<<endl;
//重载运算符*=
z[2]*=z[0];
cout<<"z[2]=";
z[2].printPolynomial();
cout<<"----z[2]*=z[0]"<<endl;
return 0;
}
- 多项式(运行环境VS2008)
- QwtPlot3D在VS2008编程环境下运行
- VS2008 MFC 与CUDA5.0环境设置及程序运行
- WIN8环境VS2008运行HALCON DEMO程序出错的解决方法
- VS2005运行ssis包(vs2008,sql2008)
- VS2008中MFC的单独exe运行出错,但是从VS环境下打开运行没问题
- NTL使用方法(VS2008环境下)
- Boost库安装(Vs2008环境)
- windows7环境下驱动程序开发环境搭建(VS2008+WDK)
- VS2008环境QT开发(一)环境搭建
- 使用VS2008开发运行在.net 2.0环境下的 ASP.NET AJAX 网站
- OGRE+VS2008+win7 环境搭建(SDK 方式)+教程框架程序运行
- vs2008 在多线程环境下,有没有调试单个线程运行的功能?
- windows环境下VS2008编译程序在有些电脑上运行不了的问题
- 配置VS2008开发环境
- VS2008 选择默认环境
- VS2008默认环境设置
- Vs2008界面环境重置
- RS-485网络配置
- linux 下使用短信 api 给自己发送手机短信
- Keil中C语言汇编代码比较
- datepicker 动态input bug
- C/C++中变量及函数命名的讨论
- 多项式(运行环境VS2008)
- 一个人学FLASH有的难
- C语言编程艺术--C语言中enum变量类型的使用
- SDRAM原理初探
- Protel 中从PCB导出GERBER光绘文件
- C语言编程艺术--C语言字符串
- 我自己建立的论坛 网站 里面包含我学习的资料
- C语言编程艺术--条件编译
- ORCAL数据库导入导出