现在的位置: 首页 > 综合 > 正文

一元多项式的实现

2013年09月19日 ⁄ 综合 ⁄ 共 3218字 ⁄ 字号 评论关闭
Term.h
#ifndef TERM_H
#define TERM_H

#include 
<iostream>
using namespace std;

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

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


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


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 == 0return out;
    
out<<val.coef;
    
switch(val.exp){
        
case 0:break;
        
case 1:out<<"X";break;
        
default:out<<"X^"<<val.exp;break;
    }

    
return out;
}

#endif //TERM_H

Polynominal.h

#ifndef POLYNOMINAL_H
#define POLYNOMINAL_H
#include 
<iostream>

#include 
"Term.h"
using namespace std;
class Polynominal{
public:
    Polynominal();
    
void AddTerms(istream& in);
    
void AddLast(int c,int e);
    
void Output(ostream& outconst;
    
void PolyAdd(Polynominal& r);
    
void MakeEmpty();
    
void PolyMultiply(Polynominal& );
private:
    Term
* theList;
    friend ostream 
& operator<<(ostream &const Polynominal &);
    friend istream 
& operator>>(istream &, Polynominal &);
    friend Polynominal
& operator+(Polynominal &, Polynominal &);
    friend Polynominal
& operator*(Polynominal &, Polynominal &);
}
;

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


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

}


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

     theList
->link = theList;
     delete p;
}


void Polynominal::AddLast(int c,int e){
     Term 
*= theList;
     
while(p->link != theList) p = p->link;
     Term 
*temp = new Term(c,e,theList);
     p
->link = temp;
}

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

}


void Polynominal::PolyAdd(Polynominal& r){
    Term 
* p = r.theList->link;
    Term 
*q1 = theList;
    Term 
*= theList->link;
    
while(p != r.theList){
        
while((q->exp > p->exp) && (q->link != theList)){
            q 
= q->link;
            q1 
= q1->link;
        }

        
if(q->exp < p->exp) {
                  q1
->InsertAfter(p->coef,p->exp);
                  q1 
= q1->link;
        }

        
else if(q->exp == p->exp){
            q
->coef += p->coef;
        }

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

        p 
= p->link;
    }

}


void Polynominal::PolyMultiply(Polynominal& b){
     Term 
*= theList->link;
     Term 
*= b.theList->link;
     Polynominal result,temp; 
     
if(p == theList || q == b.theList) return;
     
while(p != theList){

抱歉!评论已关闭.