多项式(运行环境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;
}

原创粉丝点击