#include <iostream.h>
#include <math.h>
//|----------|
//|class term|
//|----------|
class term
{
friend ostream& operator<<(ostream&,const term&);
friend term operator-(const term&);
friend term abs(const term&);
public:
term(double=0.0,unsigned=0);
int operator==(const term&) const;
int operator!=(const term&) const;
int operator<(const term&) const;
int operator>(const term&) const;
int operator<=(const term&) const;
double _coef;
unsigned _exp;
};
//------------------------------------------------------------------------------
ostream& operator<<(ostream& ostr,const term& term1){
if(term1._exp==0)
return ostr<<term1._coef;
if(term1._coef==1)
ostr<<"x";
else if(term1._coef==-1)
ostr<<"-x";
else
ostr<<term1._coef<<"x";
if(term1._exp==1)
return ostr;
else
return ostr<<"^"<<term1._exp;
}
//------------------------------------------------------------------------------
term operator-(const term& term1){
term t(term1);
t._coef *=-1.0;
return t;
}
//------------------------------------------------------------------------------
term abs(const term& t){
term term1(t);
if (term1._coef<0) term1._coef*=-1.0;
return term1;
}
//------------------------------------------------------------------------------
term::term(double coef,unsigned exp):_coef(coef),_exp(exp){}
//-------------------------------------------------------------------------------
int term::operator==(const term& t) const{
return _exp==t._exp && _coef==t._coef;
}
int term::operator!=(const term& t) const{
return _exp!=t._exp || _coef!=t._coef;
}
int term::operator<(const term& t) const{
return _exp<t._exp; //is it correct??
}
int term::operator>(const term& t) const{
return _exp>t._exp; //is it correct??
}
int term::operator<=(const term& t) const{
return !(_exp>t._exp); //is it correct??
}
//******************************************************************************
//|----------|
//|class node|
//|----------|
class node{ //!!
public:
node(const term& data=term(),node *prev=0,node *next=0)
:_data(data),_prev(prev),_next(next)
{if(_prev==0) _prev=this;
if(_next==0) _next=this;
}
term _data;
node *_prev,*_next;
};
//******************************************************************************
//|--------------|
//|class iterator|
//|--------------|
class iterator{
friend class list;
public:
iterator(node *p):_(p){}
term& operator*(){return _->_data;}
void operator=(const iterator& it){_=it._;} //is it correct?
int operator==(const iterator& it){return _==it._;}
int operator!=(const iterator& it){return _!=it._;}
iterator operator++(int) //postfix
{
iterator it(_);
_=_->_next;
return it;
}
iterator& operator++(){_=_->_next; return *this;}
iterator operator--(int)
{
iterator it(_);
_=_->_prev;
return it;
}
iterator& operator--(){_=_->_prev; return *this;}
//protected:
node *_;
};
//******************************************************************************
//|----------|
//|class list|
//|----------|
class list{
protected:
node *_; //!!
int _size; //!!`
public:
list();
list(const list&);
list(int);
list(int,const term&);
list(iterator&,iterator&);
~list();
int size() const;
int isempty() const;
iterator begin() const;
iterator end() const;
void push_back(const term&);
iterator insert(iterator&,const term&);
};
//------------------------------------------------------------------------------
list::list(){_=new node();}
//------------------------------------------------------------------------------
list::list(const list& l):_size(l._size){
_=new node();
node *pp=_;
for(node *p=l._->_next;p!=l._;p=p->_next , pp=pp->_next)
pp->_next=pp->_next->_prev=new node(p->_data,_,pp);
}
//------------------------------------------------------------------------------
list::list(int n):_size(n){
_=new node();
node *p=_;
for(int i=0;i<n;i++)
p=p->_prev=new node(term(),_,p);
_->_next=p;
}
//------------------------------------------------------------------------------
list::list(int n,const term& t){
_=new node();
node *p=_;
for(int i=0;i<n;i++)
p=p->_prev=new node(t,_,p);
_->_next=p;
}
//-----------------------------------------------------------------------------
list::~list(){
node *p=_->_next;
while(p!=_)
{node *pp=p->_next;
delete p;
p=pp;
}
delete _;
}
//------------------------------------------------------------------------------
int list::size() const{return _size;}
//------------------------------------------------------------------------------
int list::isempty() const { return _size==0;}
//------------------------------------------------------------------------------
iterator list::begin() const{return iterator(_->_next);}
//------------------------------------------------------------------------------
iterator list::end() const {return iterator(_);}
//------------------------------------------------------------------------------
void list::push_back(const term& x){
_->_prev=_->_prev->_next=new node(x,_->_prev,_);
++_size;
}
//------------------------------------------------------------------------------
iterator list::insert(iterator &it,const term &t){
it._->_prev=it._->_prev->_next=new node(t,it._->_prev,it._);
it._=it._->_prev;
++ _size;
return it;
}
//******************************************************************************
//|----------|
//|class poly|
//|----------|
class poly {
friend ostream& operator<<(ostream&,const poly&);
//friend poly operator-(const poly&,const poly&);
//friend poly operator*(const poly&,const poly&);
//friend poly operator+(const poly&,const poly&);
//friend poly operator-(const poly&);
//friend list merge(iterator b1,iterator e1,iterator b2,iterator e2);
private:
long int _degree;
//void reduce();
public:
poly(double=0,unsigned=0);
double operator()(double) const;
long degreep() const;
unsigned termsp() const;
//static const poly ZERO; //p(x)=0
//static const poly ONE; //p(x)=1
//static const poly X; //p(x)=x
list _terms; //WARNING:IT IS TEMPORARY!!!
};
//------------------------------------------------------------------------------
poly::poly(double coef,unsigned exp){
if(coef==0.0){
_terms=list(0);
_degree= -1;
}
else
{_terms=list(1,term(coef,exp));
_degree=exp;
}
}
//------------------------------------------------------------------------------
//const poly poly::ZERO(0,0);
//const poly poly::ONE(1.0,0);
//const poly poly::X(1.0,1);
//------------------------------------------------------------------------------
long max(long a,long b){
return (a>b?a:b);
}
//------------------------------------------------------------------------------
list merge(iterator b1,iterator e1,iterator b2,iterator e2){
list destlist;
while((b1!=e1)&&(b2!=e2))
{if((*b1)<(*b2))
{destlist.push_back(*b1);
b1++;
}
else
{destlist.push_back(*b2);
b2++;
}
}
while(b1!=e1)
{destlist.push_back(*b1);
b1++;
}
while(b2!=e2)
{destlist.push_back(*b2);
b2++;
}
return destlist;
}
//------------------------------------------------------------------------------
ostream& operator<<(ostream &ostr,const poly &p){
//if(p==poly::ZERO) return ostr<<0;
// iterator itr=p._terms.begin();
// ostr<<*itr++;
// while(itr!=p._terms.end())
// if (((*itr)._coef)<0) ostr<<"-"<<abs(*itr++);
// else ostr<<"+"<<*itr++;
// return ostr;
}
//------------------------------------------------------------------------------
//poly operator+(const poly& p1,const poly& p2){
// poly p;
// p._degree=max(p1._degree,p2._degree);
// p._terms=list(p1._terms.size()+p2._terms.size());
// merge(p1._terms.begin(),p1._terms.end(),p2._terms.begin(),p2._terms.end());
// p.reduce();
// return p;
//}
//------------------------------------------------------------------------------
//poly operator*(const poly &p1,const poly &p2){
// poly p;
// p._degree=p1._degree+p2._degree;
// for(iterator it1=p._terms.begin();it1!=p.terms._end;it1++)
// for(iterator it2=p._terms.begin();it2!=p.terms._end;it2++){
// double coef=(*it1)._coef * (*it2)._coef;
// unsigned exp=(*it2)._coef * (*it2)._coef;
// iterator it=p._terms.begin();
// for( ;it!=p._terms.end();it++)
// if ((*it)._exp <=exp) break;
// if((*it)._exp == exp)
// (*it)._coef +=coef;
// else
// p._terms.insert(it,term(coef,exp));
// }
// p.reduce();
// return p;
// }
//------------------------------------------------------------------------------
//double poly::operator()(double x) const{
//
// iterator it=_terms.begin();
// if(it==_terms.end()) return 0;
//
// unsigned e1=(*it)._exp;
// double y=(*it)._coef;
// while(++it!=_terms.end())
// {unsigned e2=(*it)._exp;
// y*=pow(x,(e1-e2));
// y+=(*it)._coef;
// e1=e2;
// }
// return y*pow(x,e1);
// }
//------------------------------------------------------------------------------
void main(){
}