嵌入式

编程与应用
posts - 14, comments - 1, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

 

#ifndef POLYNOMINAL_CLASS
#define POLYNOMINAL_CLASS

#include
<iostream>
using namespace std;

class Term
{
private:
    
int coef;
    
int exp;
    Term
* link;
public:
    Term(
int c, int e);
    Term(
int c, int e, Term* nxt);
    Term
* InsertAfter(int c, int e);
    friend ostream
& operator<<(ostream &,const Term &);
    friend 
class Polynominal;
}
;

class Polynominal
{
public:
    Polynominal(
void);
    
~Polynominal(void);
private:
    Term
* thelist;

public:
    
void AddTerms(istream& in);
    
void Output(ostream& out)const;
    Polynominal
& PolyAdd(Polynominal& r);
    Polynominal
& PolyMul(Polynominal& r);
    Polynominal
& operator=(Polynominal& r);
    
void PolyClear();
    friend ostream
& operator<<(ostream& out,const Polynominal& x);
    friend istream
& operator>>(istream& in, Polynominal& x);
    friend Polynominal
& operator + (Polynominal& a, Polynominal& b);
    friend Polynominal
& operator * (Polynominal& a, Polynominal& b);
}
;

#endif//POLYNOMINAL_CLASS

Term::Term(
int c, int e)
{
    link
=0;
}


Term::Term(
int c, int e, Term* nex):coef(c),exp(e)
{
    link
=nex;
}


Term
* Term::InsertAfter(int c, int e)
{
    link
=new Term(c,e,link);
    
return link;
}



ostream 
&operator <<(ostream& out,const Term& val)
{
    
if(val.coef==0)
        
return out;
    
out<<val.coef;
    
switch(val.exp){
        
case 0:break;
        
case 1:out<<"x";break;
        
default:out<<"x^"<<val.exp;break;
    }

    
return out;
}

Polynominal
& Polynominal::operator=(Polynominal& r) 
{
    
this->PolyClear();
    Term
* q=thelist->link,* p;
    
for(p=r.thelist->link;p->exp>=0;p=p->link)
        q
=q->InsertAfter(p->exp,p->coef);
    
return *this;

}

Polynominal::Polynominal(
void)
{
    thelist
=new Term(0,-1);
    thelist
->link=thelist;
}


Polynominal::
~Polynominal(void)
{
    Term
* p=thelist->link;
    
while(p!=thelist){
        thelist
->link=p->link;
        delete p;
        p
=thelist->link;
    }

    delete thelist;
}


void Polynominal::PolyClear()
{
    Term
* p=thelist->link;
    
while(p->exp>=0){
        thelist
->link=p->link;
        delete p;
        p
=thelist->link;
    }

}


void Polynominal::AddTerms(istream& in)
{
    Term
* q=thelist;
    
int c,e;
    
for(;;)
    
{
        cout
<<"Input a term(coef,exp):";
        cin
>>c>>e;
        
if(e<0break;
        q
=q->InsertAfter(c,e);
    }

}


void Polynominal::Output(ostream& out)const
{
    
int first=1;Term* p=thelist->link;
    
for(;p!=thelist;p=p->link)
    
{
        
if(!first&&(p->coef>0)) out<<"+";
        first
=0;
        cout
<<*p;
    }

}


Polynominal
& Polynominal::PolyAdd(Polynominal& r)
{
    Term 
*q,*q1=thelist,*p;
    p
=r.thelist->link;
    q
=q1->link;
    
while(p->exp>=0){
        
while(p->exp<q->exp){
            q1
=q;q=q->link;
        }

        
if(p->exp==q->exp){
            q
->coef=q->coef+p->coef;
            
if(q->coef==0){
                q1
->link=q->link;
                delete (q);
                q
=q1->link;
            }

        
else{
            q1
=q;q=q->link;
        }

    }

    
else
        q1
=q1->InsertAfter(p->coef,p->exp);
        p
=p->link;
    }

    
return *this;
}


 Polynominal
& Polynominal::PolyMul(Polynominal& r)
{
    Term 
*q,*p;
    Polynominal 
*temp=new Polynominal,*t=new Polynominal;Term* qq;
    
for(p=r.thelist->link;p->exp>=0;p=p->link)
    
{
        
for(q=thelist->link,qq=temp->thelist->link;q->exp>=0;q=q->link){
            qq
=qq->InsertAfter(q->coef*p->coef,p->exp+q->exp);
        }

        t
->PolyAdd(*temp);
        temp
->PolyClear();
    }

    
return *t;
}


ostream
& operator<<(ostream& out,const Polynominal& x)
{
    x.Output(
out);return out;
}


istream
& operator>>(istream& in, Polynominal& x)
{
    x.AddTerms(
in);return in;
}


Polynominal
& operator + (Polynominal& a, Polynominal& b)
{
    
return a.PolyAdd(b);
}


Polynominal
& operator * (Polynominal& a, Polynominal& b)
{
     
return a.PolyMul(b);
}

#include
"Polynominal.h"
#include
<iostream>
using namespace std;

int main()
{
    Polynominal p,q,r;
    cin
>>p;
    cin
>>q;
    cout
<<"测试结果如下:"<<endl;
    cout
<<"p*q="<<p*q<<endl;
    cout
<<"p+q="<<p+q<<endl未}

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理