Chinaunix首页 | 论坛 | 博客
  • 博客访问: 213174
  • 博文数量: 35
  • 博客积分: 1480
  • 博客等级: 上尉
  • 技术积分: 390
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-14 14:27
文章分类

全部博文(35)

文章存档

2008年(35)

我的朋友

分类: C/C++

2008-05-06 15:01:49

#include
using namespace std;
template
struct Term
{
       Term( T c=T(),int e=0,Term* next=NULL):coef(c),exp(e){link=next;}
       T coef;
       int exp;
       Term* link;
    
};
template
class  Polynominal
{
public:
    Polynominal();
 ~Polynominal();
 Polynominal(const Polynominal& x);
 void Output(std::ostream& out) const;
 void Input(std::istream& in);
 Polynominal &operator = (const Polynominal &x);
 Polynominal operator-() const;
 Polynominal operator + (const Polynominal &x) const;
 Polynominal operator * (const Polynominal &x) const;
 void Clear();
private:
 Term* thelist;
 friend std::ostream& operator << <>(std::ostream&,const Polynominal &x); 
 friend std::istream& operator >> <>(std::istream& in,Polynominal &x);
};
template
Polynominal::Polynominal()
{
 thelist=new Term();
}
template
Polynominal::~Polynominal()
{
    Clear();
}
template
Polynominal::Polynominal(const Polynominal& x)
{
 thelist=new Term();
 Term* mytemp=thelist;
 Term* xtemp=x.thelist;
 for(;xtemp->link!=NULL;xtemp=xtemp->link,mytemp=mytemp->link)
 {
  mytemp->coef=xtemp->coef;
  mytemp->exp=xtemp->exp;
  mytemp->link=new Term();
 }
}
template
Polynominal& Polynominal::operator = (const Polynominal& x)
{
 Term* xtemp=x.thelist;
  Clear();
 thelist=new Term();
 Term* head=thelist;
 for(;xtemp->link!=NULL;xtemp=xtemp->link,thelist=thelist->link)
 {
  thelist->coef=xtemp->coef;
  thelist->exp=xtemp->exp;
  thelist->link=new Term();
 }
 thelist->link=0;
 thelist=head;
 return *this;
}
template
Polynominal Polynominal::operator - () const
{
 Polynominal temp;
 Term* mytemp=thelist;
 Term* newhead=temp.thelist;
 for(;mytemp->link!=NULL;mytemp=mytemp->link,temp.thelist=temp.thelist->link)
 {
  temp.thelist->coef=-mytemp->coef;
  temp.thelist->exp=mytemp->exp;
     temp.thelist->link=new Term();
 }
 temp.thelist->link=0;
 temp.thelist=newhead;
 return temp;
}
template
Polynominal Polynominal::operator + (const Polynominal& x) const
{
 Polynominal temp;
 Term* mytemp=thelist;
 Term* xtemp=x.thelist;
 Term* newhead=temp.thelist;
 while(mytemp->link!=NULL&&xtemp->link!=NULL)
 {
  int result=mytemp->exp-xtemp->exp;
       if(result==0)
  {
   temp.thelist->exp=mytemp->exp;
      temp.thelist->coef=mytemp->coef+xtemp->coef;
   temp.thelist->link=new Term();
   if(temp.thelist->coef!=0)
   {
   mytemp=mytemp->link;
   xtemp=xtemp->link;
   temp.thelist=temp.thelist->link;
   }
  }
  if(result<0)
  {
   temp.thelist->exp=mytemp->exp;
   temp.thelist->coef=mytemp->coef;
   temp.thelist->link=new Term();
   mytemp=mytemp->link;
   temp.thelist=temp.thelist->link;
  }
  if(result>0)
  {
     temp.thelist->exp=xtemp->exp;
     temp.thelist->coef=xtemp->coef;
     temp.thelist->link=new Term();
     xtemp=xtemp->link;
     temp.thelist=temp.thelist->link;
  }
 }
 if(mytemp->link!=NULL)
 {
  temp.thelist->link=mytemp;
 
 }
 if(xtemp->link!=NULL)
 {
  for(;xtemp->link!=NULL;xtemp=xtemp->link,temp.thelist=temp.thelist->link)
  {
  temp.thelist->coef=xtemp->coef;
  temp.thelist->exp=xtemp->exp;
  temp.thelist->link=new Term();
  }
 }
 temp.thelist=newhead;
 return temp;
}
template
Polynominal Polynominal::operator * (const Polynominal& x) const
{
 Polynominal result;
 Term* mytemp=thelist;
 Term* xtemp=x.thelist;
for(;mytemp->link!=NULL;mytemp=mytemp->link)

 Polynominal temp;
 Term* temphead=temp.thelist;
 for(;xtemp->link!=NULL;xtemp=xtemp->link,temp.thelist=temp.thelist->link)
 {
  temp.thelist->coef=(mytemp->coef)*(xtemp->coef);
  temp.thelist->exp=(mytemp->exp)+(xtemp->exp);
  temp.thelist->link=new Term();
 }
 temp.thelist->link=0;
    temp.thelist=temphead;
 if(result.thelist->link!=NULL)
 result=result+temp;
 else
 result=temp;
 xtemp=x.thelist;
 
}
return result;
}
template
void Polynominal::Output(std::ostream& out) const
{
 out<<"the Polynominal is:"<    Term* head=thelist;
 while(head->link!=NULL)
 {
  if(head->coef!=0)
 {
  if(head!=thelist)
  out<<"+";
  if(head->coef!=1)
  out<coef;
  if(head->exp!=1)
  out<<"X^"<exp;
  else
  out<<"X";
 }
     head=head->link;
 }
 out<
}
template
void Polynominal::Input(std::istream& in)
{
Term* head=thelist;
bool result=false;
cout<<"input the Polynominal (-1 -1) for end"<do
{
result=(in>>thelist->coef>>thelist->exp);
bool end=(thelist->coef==-1)||(thelist->exp==-1);
if(result)
{
if(!end)
  {
  thelist->link=new Term();
 thelist=thelist->link;
  }
else
  break;
}
else
{
 cout<<"input error"< {
 thelist->coef=0;
 thelist->exp=0;
 thelist->link=0;
 }
 break;
}
}
while(true);
thelist=head;
}
template
void Polynominal::Clear()
{
Term* p=thelist;
for(;thelist!=NULL;p=thelist)
{
thelist=thelist->link;
delete p;
}
delete thelist;
}

template
std::ostream& operator << (std::ostream& out,const Polynominal& x)
{
x.Output(out);
return out;
}
template
std::istream& operator >> (std::istream& in,Polynominal& x)
{
x.Input(in);
return in;
}
int main()
{
 Polynominal x;
 Polynominal y;
 Polynominal z;
 cin>>x;
 cin>>y;
    z=x+y;
 cout< system("pause");
}
阅读(1654) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~