📄 polynomial.cpp
字号:
#ifndef _MY_POLY1_
#define _MY_POLY1_
#define safedelete(p) if(p) { delete p; p = NULL; }
#include "Polynomial.h"
#include <math.h>
//建立一个空表
CPolynomial::CPolynomial( )
{
m_pHead = new CTerm;
m_pHead->SetNext( m_pHead );
m_pHead->SetPrev( m_pHead );
}
CPolynomial::CPolynomial(CTerm *Know)
{
m_pHead = new CTerm;
m_pHead->SetNext( m_pHead );
m_pHead->SetPrev( m_pHead );
CTerm *m, *n,*next;
m = Know->GetNext();
next = m_pHead;
while ( m != Know )
{
n = new CTerm( m->GetDegree(), m->GetCoef(), m_pHead );
n->SetPrev( next );
next->SetNext( n );
m_pHead->SetPrev( n );
next = n;
m = ( m->GetNext() );
}
}
//析构函数
CPolynomial::~CPolynomial( )
{
Empty();
safedelete( m_pHead );
}
int CPolynomial::Empty()
{
CTerm *p;
if ( m_pHead->GetNext() != NULL )
{
while ( m_pHead->GetNext() != m_pHead )
{
p = m_pHead->GetNext();
m_pHead->SetNext( p->GetNext() );
safedelete(p);
}
}
m_pHead->SetPrev(m_pHead);
return 1;
}
int CPolynomial::Length( )
{
CTerm *p = m_pHead->GetNext();
int len = 0;
while ( p != m_pHead )
{
len++;
p = p->GetNext();
}
return len;
}
CTerm *CPolynomial::GetPoint(int val )
{
if ( val<0 || val>=Length() )
{
cout<<"error of the length of val"<<endl;
}
CTerm *p = m_pHead;
for( int i=0; i<=val; i++)
{
p = p->GetNext();
}
return p;
}
int CPolynomial::PushBack( int Degree, double Coef )
{
CTerm *p = new CTerm( Degree, Coef, m_pHead );
CTerm *next;
next = m_pHead;
while ( next->GetNext() != m_pHead )
{
next = next->GetNext();
}
p->SetPrev( next );
next->SetNext( p );
m_pHead->SetPrev( p );
return 1;
}
int CPolynomial::Insert(CTerm *p, int Degree, double Ceof )
{
//to do here....
if ( p!= NULL)
{
CTerm *q = new CTerm( Degree, Ceof, p );
q->SetPrev( p->GetPrev() );
p->GetPrev()->SetNext( q );
p->SetPrev( q );
}
return 1;
}
int CPolynomial::Delete( CTerm *p )
{
if ( p != NULL)
{
p->GetPrev()->SetNext( p->GetNext() );
p->GetNext()->SetPrev( p->GetPrev() );
safedelete( p );
}
return 1;
}
int CPolynomial::GetDegree(int val )
{
//to do here...
return 1;
}
int CPolynomial::Print()
{
cout<<endl;
CTerm *p;
p = m_pHead->GetNext();
while ( p != m_pHead )
{
cout<<p->GetDegree()<<" "<<p->GetCoef()<<endl;
p = p->GetNext();
}
cout<<"ok"<<endl;
return 1;
}
int CPolynomial::Read1( )
{
ifstream istrm( "p1.txt" );
if ( !istrm.good() )
{
cout<<"cann't open the file! "<<endl;
}
//read the file
char p;
int size;
istrm>>p>>size;
//read the degree and the ceof
int degree, coef;
CTerm *m, *q;
q = m_pHead->GetNext();
istrm>>degree>>coef;
//cout<<degree<<coef<<"shen"<<endl;
PushBack( degree, coef );
for ( int i=0; i<size-1; i++)
{
istrm>>degree>>coef;
while (q->GetDegree() <= degree && q != m_pHead )
{
q = q->GetNext();
}
if ( q->GetPrev()->GetDegree() != degree)
{
Insert( q, degree, coef );
}
else
{
m = ( q->GetPrev() );
m->SetCoef( m->GetCoef() + coef);
}
q = m_pHead->GetNext();
}
istrm.close();
return 1;
}
int CPolynomial::Read2( )
{
//to do here...
ifstream istrm( "p2.txt" );
if ( !istrm.good() )
{
cout<<"cann't open the file! "<<endl;
}
//read the file
char p;
int size;
istrm>>p>>size;
//read the degree and the ceof
int degree, coef;
CTerm *m, *q;
q = m_pHead->GetNext();
istrm>>degree>>coef;
PushBack( degree, coef );
for ( int i=0; i<size-1; i++)
{
istrm>>degree>>coef;
while (q->GetDegree() <= degree && q != m_pHead )
{
q = q->GetNext();
}
if ( q->GetPrev()->GetDegree() != degree)
{
Insert( q, degree, coef );
}
else
{
m = ( q->GetPrev() );
m->SetCoef( m->GetCoef() + coef);
}
q = m_pHead->GetNext();
}
istrm.close();
return 1;
}
//用成员函数重载赋值运算符
CPolynomial& CPolynomial::operator= (const CPolynomial &p )
{
Empty();
CTerm *m, *n,*next;
m = p.m_pHead->GetNext();
next = m_pHead;
while ( m != p.m_pHead )
{
n = new CTerm( m->GetDegree(), m->GetCoef(), m_pHead );
n->SetPrev( next );
next->SetNext( n );
m_pHead->SetPrev( n );
next = n;
m = ( m->GetNext() );
}
return *this;
}
//重载加号运算符
CPolynomial operator+ ( const CPolynomial &p1, const CPolynomial &p2)
{
CPolynomial Result;
CTerm *next1, *next2;
next1 = p1.m_pHead->GetNext();
next2 = p2.m_pHead->GetNext();
int degree1,degree2;
while ( next1!=p1.m_pHead && next2 != p2.m_pHead )
{
degree1 = next1->GetDegree();
degree2 = next2->GetDegree();
if ( degree1 < degree2 )
{
Result.PushBack(next1->GetDegree(), next1->GetCoef() );
next1 = next1->GetNext();
}
else if( degree1 == degree2 )
{
Result.PushBack(next1->GetDegree(), next2->GetCoef()+next1->GetCoef() );
next1 = next1->GetNext();
next2 = next2->GetNext();
}
else
{
Result.PushBack(next2->GetDegree(), next2->GetCoef() );
next2 = next2->GetNext();
}
}
while (next1 != p1.m_pHead)
{
Result.PushBack(next1->GetDegree(),next1->GetCoef());
next1 = next1->GetNext();
}
while (next2 != p2.m_pHead )
{
Result.PushBack(next2->GetDegree(),next2->GetCoef());
next2 = next2->GetNext();
}
return CPolynomial( Result.m_pHead );
}
//重载减好运算符
CPolynomial operator- ( const CPolynomial &p1, const CPolynomial &p2)
{
CPolynomial Result;
CTerm *next1, *next2;
next1 = p1.m_pHead->GetNext();
next2 = p2.m_pHead->GetNext();
int degree1,degree2;
while ( next1!=p1.m_pHead && next2 != p2.m_pHead )
{
degree1 = next1->GetDegree();
degree2 = next2->GetDegree();
if ( degree1 < degree2 )
{
Result.PushBack(next1->GetDegree(), next1->GetCoef() );
next1 = next1->GetNext();
}
else if( degree1 == degree2 )
{
if ( (next2->GetCoef()-next1->GetCoef()) != 0 )
{
Result.PushBack(next1->GetDegree(), next1->GetCoef()-next2->GetCoef() );
}
next1 = next1->GetNext();
next2 = next2->GetNext();
}
else
{
Result.PushBack(next2->GetDegree(), -next2->GetCoef() );
next2 = next2->GetNext();
}
}
while (next1 != p1.m_pHead)
{
Result.PushBack(next1->GetDegree(),next1->GetCoef());
next1 = next1->GetNext();
}
while (next2 != p2.m_pHead )
{
Result.PushBack(next2->GetDegree(),-next2->GetCoef());
next2 = next2->GetNext();
}
return CPolynomial( Result.m_pHead );
}
CPolynomial operator* ( const CPolynomial &p1, const CPolynomial &p2)
{
//ji ben suan fa
CPolynomial Temp, Result;
CTerm *next1,*next2;
Temp = p2;
next1 = p1.m_pHead->GetNext();
next2 = Temp.m_pHead->GetNext();
while ( next1!= p1.m_pHead )
{
while ( next2 != Temp.m_pHead )
{
next2->SetDegree(next1->GetDegree() + next2->GetDegree() );
next2->SetCoef(next1->GetCoef() * next2->GetCoef());
next2 = next2->GetNext();
}
Result = Result + Temp;
next1 = next1->GetNext();
Temp = p2;
next2 = Temp.GetHead()->GetNext();
}
return CPolynomial( Result.m_pHead );
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -