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 == 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;
}
#endif //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 == 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;
}
#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& out) const;
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 *q = theList;
while(true){
cout<<"Input a term(coef,exp): "<<endl;
in>>c>>e;
if(e < 0) break;
q = q->InsertAfter(c,e);
}
}
void Polynominal::MakeEmpty(){
Term *p = 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 *p = theList;
while(p->link != theList) p = p->link;
Term *temp = new Term(c,e,theList);
p->link = temp;
}
void Polynominal::Output(ostream& out) const{
int first = 1;
Term *p = 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 *q = 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 *p = theList->link;
Term *q = b.theList->link;
Polynominal result,temp;
if(p == theList || q == b.theList) return;
while(p != theList){
#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& out) const;
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 *q = theList;
while(true){
cout<<"Input a term(coef,exp): "<<endl;
in>>c>>e;
if(e < 0) break;
q = q->InsertAfter(c,e);
}
}
void Polynominal::MakeEmpty(){
Term *p = 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 *p = theList;
while(p->link != theList) p = p->link;
Term *temp = new Term(c,e,theList);
p->link = temp;
}
void Polynominal::Output(ostream& out) const{
int first = 1;
Term *p = 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 *q = 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 *p = theList->link;
Term *q = b.theList->link;
Polynominal result,temp;
if(p == theList || q == b.theList) return;
while(p != theList){