多项式计算
设计程序,实现对多项式的加、减、乘、除、微分和积分的运算。考虑分别用顺序分配的线性表和链接分配的线性表表示多项式的实现。估计各项运算的算法复杂性。
设计程序,实现对多项式的加、减、乘、除、微分和积分的运算。考虑分别用顺序分配的线性表和链接分配的线性表表示多项式的实现。估计各项运算的算法复杂性。
先定义类吧……


struct Term
{
int coef;
int order;
Term *next;
Term *pre;
};
class Poly
{
friend Poly operator+ (Poly &lhs, Poly &rhs);
friend Poly operator- (Poly &lhs, Poly &rhs);
friend Poly operator* (Poly &lhs, Poly &rhs);
friend Poly operator/ (Poly &lhs, Poly &rhs);
friend Poly der(Poly &lhs);
friend Poly cal(Poly &rhs);
public:
Poly();
Poly(const Poly &cpy);
//~Poly();
void cpInsert(Term *term);
void Insert(Term *term);
void del(Term *term);
void print();
Poly cpy();
private:
Term *pHead;
Term *pTail;
};
痛苦阿,才做这么一点点,开始做加法……
(long time latter)加法完毕:(损失了析够函数,帮忙看看哪里错了)


inline void cpyterm(Term *lhs, Term* rhs)
{
lhs->coef = rhs->coef;
lhs->order = rhs->order;
}
Poly::Poly()
{
pHead = NULL;
pTail = NULL;
}
Poly::Poly(const Poly &cpy)
{
pHead = cpy.pHead;
pTail = cpy.pTail;
}
/*Poly::~Poly()
{
Term *p = pHead, *pNext;
while(NULL != p)
{
pNext = p->next;
delete p;
p = pNext;
}
} */
Poly Poly::cpy()
{
Poly poly;
Term *p = pHead;
Term *term = poly.pHead;
while(NULL != p)
{
term = new(Term);
cpyterm(term,p);
poly.Insert(term);
}
return poly;
}
void Poly::cpInsert(Term* term)
{
Term *temp = new(Term);
cpyterm(temp,term);
Insert(temp);
}
void Poly::Insert(Term *term)
{
Term* pc = pHead;
if(NULL == pc)
{
pHead = term;
pTail = term;
term->pre = NULL;
term->next = NULL;
}
else
{
while(NULL != pc)
{
if(pc->order < term->order)
{
if(NULL == pc->pre)
{
pc->pre = term;
term->next = pc;
term->pre = NULL;
pHead = term;
return;
}
else
{
pc->pre->next = term;
term->pre = pc->pre;
pc->pre = term;
term->next = pc;
return;
}
}
else if(pc->order == term->order)
{
pc->coef += term->coef;
return;
}
else
{
if(NULL == pc->next)
{
pc->next = term;
term->pre = pc;
term->next = NULL;
pTail = term;
return;
}
else
pc = pc->next;
}
}
}
}
void Poly::print()
{
Term *p = pHead;
if(NULL == p)
cout << "It is an empty chain" << endl;
while(NULL != p)
{
if(0 != p->coef)
cout << p->coef << "X^" << p->order << '+';
p = p->next;
}
}
Poly operator+ (Poly &lhs, Poly &rhs)
{
Poly result;
Term *lt = lhs.pHead;
Term *rt = rhs.pHead;
Term *temp = NULL;
while(NULL != lt && NULL != rt)
{
temp = new(Term);
if(lt->order > rt->order)
{
cpyterm(temp,lt);
result.Insert(temp);
lt = lt->next;
}
else if(lt->order < rt->order)
{
cpyterm(temp,rt);
result.Insert(temp);
rt = rt->next;
}
else
{
temp->coef = lt->coef + rt->coef;
temp->order = lt->order;
result.Insert(temp);
lt = lt->next;
rt = rt->next;
}
}
while(NULL != lt)
{
temp = new(Term);
cpyterm(temp,lt);
result.Insert(temp);
lt = lt->next;
}
while(NULL != lt)
{
temp = new(Term);
cpyterm(temp,lt);
result.Insert(temp);
rt = rt->next;
}
return result;
}
再加一段恶心的代码,bug多多,脆弱版。


#include <iostream>
using namespace std;
struct Term
{
double coef;
int order;
Term *next;
Term *pre;
};
class Poly
{
friend Poly operator+ (Poly &lhs, Poly &rhs);
friend Poly operator- (Poly &lhs, Poly &rhs);
friend Poly operator* (Poly &lhs, Poly &rhs);
friend void div(Poly &lhs, Poly &rhs, Poly &result, Poly &remain);
friend Poly der(Poly &lhs);
friend Poly cal(Poly &rhs);
public:
Poly();
Poly(const Poly &cpy);
void del();
//~Poly();
void cpInsert(Term *term);
void Insert(Term *term);
void del(Term *term);
void print();
Poly cpy();
//private:
Term *pHead;
Term *pTail;
};
inline void cpyterm(Term *lhs, Term* rhs)
{
lhs->coef = rhs->coef;
lhs->order = rhs->order;
}
Poly::Poly()
{
pHead = NULL;
pTail = NULL;
}
Poly::Poly(const Poly &cpy)
{
pHead = cpy.pHead;
pTail = cpy.pTail;
}
void Poly::del()
{
Term *p = pHead;
Term *pNext;
while(NULL != p)
{
pNext = p->next;
delete p;
p = pNext;
}
pHead = NULL;
pTail = NULL;
}
/*Poly::~Poly()
{
Term *p = pHead;
Term *pNext;
while(NULL != p)
{
pNext = p->next;
delete p;
p = pNext;
}
} */
Poly Poly::cpy()
{
Poly poly;
Term *p = pHead;
Term *term = poly.pHead;
while(NULL != p)
{
term = new(Term);
cpyterm(term,p);
poly.Insert(term);
p = p->next;
}
return poly;
}
void Poly::cpInsert(Term* term)
{
Term *temp = new(Term);
cpyterm(temp,term);
Insert(temp);
}
void Poly::Insert(Term *term)
{
Term* pc = pHead;
if(0 == term->coef)
return;
if(NULL == pc)
{
pHead = term;
pTail = term;
term->pre = NULL;
term->next = NULL;
}
else
{
while(NULL != pc)
{
if(pc->order < term->order)
{
if(NULL == pc->pre)
{
pc->pre = term;
term->next = pc;
term->pre = NULL;
pHead = term;
return;
}
else
{
pc->pre->next = term;
term->pre = pc->pre;
pc->pre = term;
term->next = pc;
return;
}
}
else if(pc->order == term->order)
{
pc->coef += term->coef;
return;
}
else
{
if(NULL == pc->next)
{
pc->next = term;
term->pre = pc;
term->next = NULL;
pTail = term;
return;
}
else
pc = pc->next;
}
}
}
}
void Poly::print()
{
Term *p = pHead;
if(NULL == p)
cout << "It is an empty chain" << endl;
while(NULL != p)
{
//if(0 != p->coef)
cout << p->coef << "X^" << p->order << '+';
p = p->next;
}
cout<<endl;
}
Poly operator+ (Poly &lhs, Poly &rhs)
{
Poly result;
Term *lt = lhs.pHead;
Term *rt = rhs.pHead;
Term *temp = NULL;
while(NULL != lt && NULL != rt)
{
temp = new(Term);
if(lt->order > rt->order)
{
cpyterm(temp,lt);
result.Insert(temp);
lt = lt->next;
}
else if(lt->order < rt->order)
{
cpyterm(temp,rt);
result.Insert(temp);
rt = rt->next;
}
else
{
temp->coef = lt->coef + rt->coef;
temp->order = lt->order;
result.Insert(temp);
lt = lt->next;
rt = rt->next;
}
}
while(NULL != lt)
{
temp = new(Term);
cpyterm(temp,lt);
result.Insert(temp);
lt = lt->next;
}
while(NULL != lt)
{
temp = new(Term);
cpyterm(temp,lt);
result.Insert(temp);
rt = rt->next;
}
return result;
}
Poly operator- (Poly &lhs, Poly &rhs)
{
Term *p;
Poly result = rhs.cpy();
p = result.pHead;
while(NULL != p)
{
p->coef = (-1)*(p->coef);
p = p->next;
}
result = lhs + result;
return result;
}
Poly operator* (Poly &lhs, Poly &rhs)
{
Term *lt, *rt,*temp;
Poly result,ass;
int coef,order;
lt = lhs.pHead;
rt = rhs.pHead;
while(NULL != lt)
{
rt = rhs.pHead;
while(NULL != rt)
{
temp = new(Term);
temp->coef = (lt->coef)*(rt->coef);
temp->order = (lt->order)+(rt->order);
ass.Insert(temp);
rt = rt->next;
}
result = ass + result;
ass.del();
lt = lt->next;
}
return result;
}
void div(Poly &lhs, Poly &rhs, Poly &result, Poly &remain)
{
Term *temp;
Poly ass1,ass2;
result.del();
remain = lhs.cpy();
while(remain.pHead->order >= rhs.pHead->order)
{
temp = new(Term);
temp->coef = (remain.pHead->coef)/(rhs.pHead->coef);
temp->order = (remain.pHead->order) - (rhs.pHead->order);
result.cpInsert(temp);
ass1.Insert(temp);
ass2 = ass1*rhs;
remain = remain - ass2;
remain.print();
ass1.del();
}
}
int main()
{
Term *term;
Poly poly1,poly2,result,remain;
for(int i=0; i < 10; ++i)
{
term = new(Term);
term->coef = i;
term->order = i;
poly1.Insert(term);
}
for(int i=0; i< 5; ++i)
{
term = new(Term);
term->coef = 10-i;
term->order = i;
poly2.Insert(term);
}
poly1.print();
cout<<"========================================="<<endl;
poly2.print();
cout<<"========================================="<<endl;
cout<<poly2.pHead<<endl;
cout<<poly2.pTail<<endl;
cout<<"==========================================="<<endl;
div(poly1,poly2,result,remain);
cout<<"========================================="<<endl;
cout<<"result:";
result.print();
cout<<"remain:";
remain.print();
int x;cin>>x;
return 0;
}
贴一段完整的,待美化


#include <iostream>
using namespace std;
struct Term
{
double coef;
int order;
Term *next;
Term *pre;
};
class Poly
{
friend Poly operator+ (Poly &lhs, Poly &rhs);
friend Poly operator- (Poly &lhs, Poly &rhs);
friend Poly operator* (Poly &lhs, Poly &rhs);
friend void div(Poly &lhs, Poly &rhs, Poly &result, Poly &remain);
friend Poly der(Poly &lhs);
friend Poly cal(Poly &rhs);
public:
Poly();
Poly(const Poly &cpy);
void del();
//~Poly();
void cpInsert(Term *term);
void Insert(Term *term);
void del(Term *term);
void print();
Poly cpy();
//private:
Term *pHead;
Term *pTail;
};
inline void cpyterm(Term *lhs, Term* rhs)
{
lhs->coef = rhs->coef;
lhs->order = rhs->order;
}
Poly::Poly()
{
pHead = NULL;
pTail = NULL;
}
Poly::Poly(const Poly &cpy)
{
pHead = cpy.pHead;
pTail = cpy.pTail;
}
void Poly::del()
{
Term *p = pHead;
Term *pNext;
while(NULL != p)
{
pNext = p->next;
delete p;
p = pNext;
}
pHead = NULL;
pTail = NULL;
}
/*Poly::~Poly()
{
Term *p = pHead;
Term *pNext;
while(NULL != p)
{
pNext = p->next;
delete p;
p = pNext;
}
} */
Poly Poly::cpy()
{
Poly poly;
Term *p = pHead;
Term *term = poly.pHead;
while(NULL != p)
{
term = new(Term);
cpyterm(term,p);
poly.Insert(term);
p = p->next;
}
return poly;
}
void Poly::cpInsert(Term* term)
{
Term *temp = new(Term);
cpyterm(temp,term);
Insert(temp);
}
void Poly::Insert(Term *term)
{
Term* pc = pHead;
if(0 == term->coef)
return;
if(NULL == pc)
{
pHead = term;
pTail = term;
term->pre = NULL;
term->next = NULL;
}
else
{
while(NULL != pc)
{
if(pc->order < term->order)
{
if(NULL == pc->pre)
{
pc->pre = term;
term->next = pc;
term->pre = NULL;
pHead = term;
return;
}
else
{
pc->pre->next = term;
term->pre = pc->pre;
pc->pre = term;
term->next = pc;
return;
}
}
else if(pc->order == term->order)
{
pc->coef += term->coef;
return;
}
else
{
if(NULL == pc->next)
{
pc->next = term;
term->pre = pc;
term->next = NULL;
pTail = term;
return;
}
else
pc = pc->next;
}
}
}
}
void Poly::print()
{
Term *p = pHead;
if(NULL == p)
cout << "It is an empty chain" << endl;
while(NULL != p)
{
//if(0 != p->coef)
cout << p->coef << "X^" << p->order << '+';
p = p->next;
}
cout<<endl;
}
Poly operator+ (Poly &lhs, Poly &rhs)
{
Poly result;
Term *lt = lhs.pHead;
Term *rt = rhs.pHead;
Term *temp = NULL;
while(NULL != lt && NULL != rt)
{
temp = new(Term);
if(lt->order > rt->order)
{
cpyterm(temp,lt);
result.Insert(temp);
lt = lt->next;
}
else if(lt->order < rt->order)
{
cpyterm(temp,rt);
result.Insert(temp);
rt = rt->next;
}
else
{
temp->coef = lt->coef + rt->coef;
temp->order = lt->order;
result.Insert(temp);
lt = lt->next;
rt = rt->next;
}
}
while(NULL != lt)
{
temp = new(Term);
cpyterm(temp,lt);
result.Insert(temp);
lt = lt->next;
}
while(NULL != lt)
{
temp = new(Term);
cpyterm(temp,lt);
result.Insert(temp);
rt = rt->next;
}
return result;
}
Poly operator- (Poly &lhs, Poly &rhs)
{
Term *p;
Poly result = rhs.cpy();
p = result.pHead;
while(NULL != p)
{
p->coef = (-1)*(p->coef);
p = p->next;
}
result = lhs + result;
return result;
}
Poly operator* (Poly &lhs, Poly &rhs)
{
Term *lt, *rt,*temp;
Poly result,ass;
int coef,order;
lt = lhs.pHead;
rt = rhs.pHead;
while(NULL != lt)
{
rt = rhs.pHead;
while(NULL != rt)
{
temp = new(Term);
temp->coef = (lt->coef)*(rt->coef);
temp->order = (lt->order)+(rt->order);
ass.Insert(temp);
rt = rt->next;
}
result = ass + result;
ass.del();
lt = lt->next;
}
return result;
}
void div(Poly &lhs, Poly &rhs, Poly &result, Poly &remain)
{
Term *temp;
Poly ass1,ass2;
result.del();
remain = lhs.cpy();
while(remain.pHead->order >= rhs.pHead->order)
{
temp = new(Term);
temp->coef = (remain.pHead->coef)/(rhs.pHead->coef);
temp->order = (remain.pHead->order) - (rhs.pHead->order);
result.cpInsert(temp);
ass1.Insert(temp);
ass2 = ass1*rhs;
remain = remain - ass2;
remain.print();
ass1.del();
}
}
Poly der(Poly &lhs)
{
Poly result;
Term *p=lhs.pHead, *temp;
while(NULL != p)
{
temp = new(Term);
temp->coef = (p->coef)*(p->order);
temp->order = p->order - 1;
result.Insert(temp);
p = p->next;
}
return result;
}
Poly cal(Poly &rhs)
{
Poly result;
Term *p=rhs.pHead, *temp;
while(NULL != p)
{
temp = new(Term);
temp->coef = (p->coef)/(p->order+1);
temp->order = p->order+1;
result.Insert(temp);
p = p->next;
}
return result;
}
int main()
{
Term *term;
Poly poly1,poly2,result,remain;
for(int i=0; i < 10; ++i)
{
term = new(Term);
term->coef = i;
term->order = i;
poly1.Insert(term);
}
for(int i=0; i< 5; ++i)
{
term = new(Term);
term->coef = i+1;
term->order = i;
poly2.Insert(term);
}
poly1.print();
cout<<"========================================="<<endl;
poly2.print();
cout<<"========================================="<<endl;
cout<<poly2.pHead<<endl;
cout<<poly2.pTail<<endl;
cout<<"==========================================="<<endl;
div(poly1,poly2,result,remain);
cout<<"========================================="<<endl;
cout<<"result:";
result.print();
cout<<"remain:";
remain.print();
poly1 = der(poly2);
cout<<"der:";
poly1.print();
poly1 = cal(poly2);
poly1.print();
int x;cin>>x;
return 0;
}
稍微美化版……备份一下


#include <iostream>
using namespace std;
struct Term
{
double coef;
double order;
Term *next;
Term *pre;
};
class Poly
{
friend Poly operator+ (Poly &lhs, Poly &rhs);
friend Poly operator- (Poly &lhs, Poly &rhs);
friend Poly operator* (Poly &lhs, Poly &rhs);
friend void div(Poly &lhs, Poly &rhs, Poly &result, Poly &remain);
friend Poly der(Poly &lhs);
friend Poly cal(Poly &rhs);
public:
Poly();
Poly(const Poly &cpy);
void del();
void cpInsert(Term *term);
void Insert(Term *term);
void del(Term *term);
void print();
Poly cpy();
private:
Term *pHead;
Term *pTail;
};
inline void cpyterm(Term *lhs, Term* rhs)
{
lhs->coef = rhs->coef;
lhs->order = rhs->order;
}
Poly::Poly()
{
pHead = NULL;
pTail = NULL;
}
Poly::Poly(const Poly &cpy)
{
pHead = cpy.pHead;
pTail = cpy.pTail;
}
void Poly::del()
{
Term *p = pHead;
Term *pNext;
while(NULL != p)
{
pNext = p->next;
delete p;
p = pNext;
}
pHead = NULL;
pTail = NULL;
}
Poly Poly::cpy()
{
Poly poly;
Term *p = pHead;
Term *term = poly.pHead;
while(NULL != p)
{
term = new(Term);
cpyterm(term,p);
poly.Insert(term);
p = p->next;
}
return poly;
}
void Poly::cpInsert(Term* term)
{
Term *temp = new(Term);
cpyterm(temp,term);
Insert(temp);
}
void Poly::Insert(Term *term) //sort-insert
{
Term* pc = pHead;
if(0 == term->coef)
return;
if(NULL == pc)
{
pHead = term;
pTail = term;
term->pre = NULL;
term->next = NULL;
}
else
{
while(NULL != pc)
{
if(pc->order < term->order)
{
if(NULL == pc->pre)
{
pc->pre = term;
term->next = pc;
term->pre = NULL;
pHead = term;
return;
}
else
{
pc->pre->next = term;
term->pre = pc->pre;
pc->pre = term;
term->next = pc;
return;
}
}
else if(pc->order == term->order)
{
pc->coef += term->coef;
return;
}
else
{
if(NULL == pc->next)
{
pc->next = term;
term->pre = pc;
term->next = NULL;
pTail = term;
return;
}
else
pc = pc->next;
}
}
}
}
void Poly::print()
{
Term *p = pHead;
if(NULL == p)
cout << "It is an empty chain" << endl;
while(NULL != p)
{
cout << p->coef << "X^" << p->order << '+';
p = p->next;
}
cout<<endl;
}
Poly operator+ (Poly &lhs, Poly &rhs)
{
Poly result;
Term *lt = lhs.pHead;
Term *rt = rhs.pHead;
Term *temp = NULL;
while(NULL != lt && NULL != rt)
{
temp = new(Term);
if(lt->order > rt->order)
{
cpyterm(temp,lt);
result.Insert(temp);
lt = lt->next;
}
else if(lt->order < rt->order)
{
cpyterm(temp,rt);
result.Insert(temp);
rt = rt->next;
}
else
{
temp->coef = lt->coef + rt->coef;
temp->order = lt->order;
result.Insert(temp);
lt = lt->next;
rt = rt->next;
}
}
while(NULL != lt)
{
temp = new(Term);
cpyterm(temp,lt);
result.Insert(temp);
lt = lt->next;
}
while(NULL != lt)
{
temp = new(Term);
cpyterm(temp,lt);
result.Insert(temp);
rt = rt->next;
}
return result;
}
Poly operator- (Poly &lhs, Poly &rhs)
{
Term *p;
Poly result = rhs.cpy();
p = result.pHead;
while(NULL != p)
{
p->coef = (-1)*(p->coef);
p = p->next;
}
result = lhs + result;
return result;
}
Poly operator* (Poly &lhs, Poly &rhs)
{
Term *lt, *rt,*temp;
Poly result,ass;
int coef,order;
lt = lhs.pHead;
rt = rhs.pHead;
while(NULL != lt)
{
rt = rhs.pHead;
while(NULL != rt)
{
temp = new(Term);
temp->coef = (lt->coef)*(rt->coef);
temp->order = (lt->order)+(rt->order);
ass.Insert(temp);
rt = rt->next;
}
result = ass + result;
ass.del();
lt = lt->next;
}
return result;
}
void div(Poly &lhs, Poly &rhs, Poly &result, Poly &remain)
{
Term *temp;
Poly ass1,ass2;
result.del();
remain = lhs.cpy();
while(remain.pHead->order >= rhs.pHead->order)
{
temp = new(Term);
temp->coef = (remain.pHead->coef)/(rhs.pHead->coef);
temp->order = (remain.pHead->order) - (rhs.pHead->order);
result.cpInsert(temp);
ass1.Insert(temp);
ass2 = ass1*rhs;
remain = remain - ass2;
ass1.del();
}
}
Poly der(Poly &lhs)
{
Poly result;
Term *p=lhs.pHead, *temp;
while(NULL != p)
{
temp = new(Term);
temp->coef = (p->coef)*(p->order);
temp->order = p->order - 1;
result.Insert(temp);
p = p->next;
}
return result;
}
Poly cal(Poly &rhs)
{
Poly result;
Term *p=rhs.pHead, *temp;
while(NULL != p)
{
temp = new(Term);
temp->coef = (p->coef)/(p->order+1);
temp->order = p->order+1;
result.Insert(temp);
p = p->next;
}
return result;
}
int main()
{
Term *term;
Poly poly1,poly2,result,remain;
for(int i=0; i < 10; ++i)
{
term = new(Term);
term->coef = i;
term->order = i;
poly1.Insert(term);
}
for(int i=0; i< 5; ++i)
{
term = new(Term);
term->coef = i+1;
term->order = i;
poly2.Insert(term);
}
poly1.print();
cout<<"========================================="<<endl;
poly2.print();
cout<<"==========================================="<<endl;
div(poly1,poly2,result,remain);
cout<<"result:";
result.print();
cout<<"remain:";
remain.print();
poly1 = der(poly2);
cout<<"der:";
poly1.print();
cout<<"cal:";
poly1 = cal(poly2);
poly1.print();
int x;cin>>x;
return 0;
}
改进一下加法
Poly operator+ (Poly &lhs, Poly &rhs)
{
Poly result;
Term* lt = lhs.pHead;
Term* rt = rhs.pHead;
while(NULL != lt)
{
result.cpInsert(lt);
lt = lt->next;
}
while(NULL != rt)
{
result.cpInsert(rt);
rt = rt->next;
}
return result;
}
{
Poly result;
Term* lt = lhs.pHead;
Term* rt = rhs.pHead;
while(NULL != lt)
{
result.cpInsert(lt);
lt = lt->next;
}
while(NULL != rt)
{
result.cpInsert(rt);
rt = rt->next;
}
return result;
}


#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
struct Term
{
double coef;
int order;
Term *next;
Term *pre;
};
class Poly
{
friend Poly operator+ (Poly &lhs, Poly &rhs);
friend Poly operator- (Poly &lhs, Poly &rhs);
friend Poly operator* (Poly &lhs, Poly &rhs);
friend void div(Poly &lhs, Poly &rhs, Poly &result, Poly &remain);
friend Poly der(Poly &lhs);
friend Poly cal(Poly &rhs);
public:
Poly();
Poly(const Poly &cpy);
void del();
void cpInsert(Term *term);
void Insert(Term *term);
void del(Term *term);
void print();
Poly cpy();
private:
Term *pHead;
Term *pTail;
};
inline void cpyterm(Term *lhs, Term* rhs)
{
lhs->coef = rhs->coef;
lhs->order = rhs->order;
}
Poly::Poly()
{
pHead = NULL;
pTail = NULL;
}
Poly::Poly(const Poly &cpy)
{
pHead = cpy.pHead;
pTail = cpy.pTail;
}
void Poly::del()
{
Term *p = pHead;
Term *pNext;
while(NULL != p)
{
pNext = p->next;
delete p;
p = pNext;
}
pHead = NULL;
pTail = NULL;
}
Poly Poly::cpy()
{
Poly poly;
Term *p = pHead;
Term *term = poly.pHead;
while(NULL != p)
{
term = new(Term);
cpyterm(term,p);
poly.Insert(term);
p = p->next;
}
return poly;
}
void Poly::cpInsert(Term* term)
{
Term *temp = new(Term);
cpyterm(temp,term);
Insert(temp);
}
void Poly::Insert(Term *term) //sort-insert
{
Term* pc = pHead;
if(0 == term->coef)
return;
if(NULL == pc)
{
pHead = term;
pTail = term;
term->pre = NULL;
term->next = NULL;
}
else
{
while(NULL != pc)
{
if(pc->order < term->order)
{
if(NULL == pc->pre)
{
pc->pre = term;
term->next = pc;
term->pre = NULL;
pHead = term;
return;
}
else
{
pc->pre->next = term;
term->pre = pc->pre;
pc->pre = term;
term->next = pc;
return;
}
}
else if(pc->order == term->order)
{
pc->coef += term->coef;
return;
}
else
{
if(NULL == pc->next)
{
pc->next = term;
term->pre = pc;
term->next = NULL;
pTail = term;
return;
}
else
pc = pc->next;
}
}
}
}
void Poly::print()
{
Term *p = pHead;
if(NULL == p)
cout << "0" << endl;
while(NULL != p)
{
cout << p->coef << "X^" << p->order << '+';
p = p->next;
}
cout<<endl;
}
/*
Poly operator+ (Poly &lhs, Poly &rhs)
{
Poly result;
Term *lt = lhs.pHead;
Term *rt = rhs.pHead;
Term *temp = NULL;
while(NULL != lt && NULL != rt)
{
temp = new(Term);
if(lt->order > rt->order)
{
cpyterm(temp,lt);
result.Insert(temp);
lt = lt->next;
}
else if(lt->order < rt->order)
{
cpyterm(temp,rt);
result.Insert(temp);
rt = rt->next;
}
else
{
temp->coef = lt->coef + rt->coef;
temp->order = lt->order;
result.Insert(temp);
lt = lt->next;
rt = rt->next;
}
}
while(NULL != lt)
{
temp = new(Term);
cpyterm(temp,lt);
result.Insert(temp);
lt = lt->next;
}
while(NULL != lt)
{
temp = new(Term);
cpyterm(temp,lt);
result.Insert(temp);
rt = rt->next;
}
return result;
}
*/
Poly operator+ (Poly &lhs, Poly &rhs)
{
Poly result;
Term* lt = lhs.pHead;
Term* rt = rhs.pHead;
while(NULL != lt)
{
result.cpInsert(lt);
lt = lt->next;
}
while(NULL != rt)
{
result.cpInsert(rt);
rt = rt->next;
}
return result;
}
Poly operator- (Poly &lhs, Poly &rhs)
{
Term *p;
Poly result = rhs.cpy();
p = result.pHead;
while(NULL != p)
{
p->coef = (-1)*(p->coef);
p = p->next;
}
result = lhs + result;
return result;
}
Poly operator* (Poly &lhs, Poly &rhs)
{
Term *lt, *rt,*temp;
Poly result,ass;
int coef,order;
lt = lhs.pHead;
rt = rhs.pHead;
while(NULL != lt)
{
rt = rhs.pHead;
while(NULL != rt)
{
temp = new(Term);
temp->coef = (lt->coef)*(rt->coef);
temp->order = (lt->order)+(rt->order);
ass.Insert(temp);
rt = rt->next;
}
result = ass + result;
ass.del();
lt = lt->next;
}
return result;
}
void div(Poly &lhs, Poly &rhs, Poly &result, Poly &remain)
{
Term *temp;
Poly ass1,ass2;
result.del();
remain = lhs.cpy();
while(remain.pHead->order >= rhs.pHead->order)
{
temp = new(Term);
temp->coef = (remain.pHead->coef)/(rhs.pHead->coef);
temp->order = (remain.pHead->order) - (rhs.pHead->order);
result.cpInsert(temp);
ass1.Insert(temp);
ass2 = ass1*rhs;
remain = remain - ass2;
ass1.del();
}
}
Poly der(Poly &lhs)
{
Poly result;
Term *p=lhs.pHead, *temp;
while(NULL != p)
{
temp = new(Term);
temp->coef = (p->coef)*(p->order);
temp->order = p->order - 1;
result.Insert(temp);
p = p->next;
}
return result;
}
Poly cal(Poly &rhs)
{
Poly result;
Term *p=rhs.pHead, *temp;
while(NULL != p)
{
temp = new(Term);
temp->coef = (p->coef)/(p->order+1);
temp->order = p->order+1;
result.Insert(temp);
p = p->next;
}
return result;
}
int main()
{
Term *term;
Poly poly1,poly2,result,remain;
int n1,n2;
srand(time(0));
cout<<"Input two orders:(int)";
cin >> n1 >> n2;
for(int i=0; i < n1; ++i)
{
term = new(Term);
term->coef = rand()%100;
term->order = rand()%(n1+1);
poly1.Insert(term);
}
for(int i=0; i< n2; ++i)
{
term = new(Term);
term->coef = rand()%100;
term->order = rand()%(n2+1);
poly2.Insert(term);
}
cout<<"Two polynomials created randomly :"<<endl<<endl;
poly1.print();
cout<<"==========================================="<<endl;
poly2.print();
cout<<"==========================================="<<endl;
cout << endl << "sum:";
(poly1+poly2).print();
cout << endl << "minus:";
(poly1-poly2).print();
cout << endl << "product:";
(poly1*poly2).print();
cout << endl << "poly1 / poly2 :"<<endl;
div(poly1,poly2,result,remain);
cout << endl << "result:";
result.print();
cout << "remain:";
remain.print();
poly1 = der(poly2);
cout << endl << "der(2):";
poly1.print();
cout << endl << "cal(2):";
poly1 = cal(poly2);
poly1.print();
int x;cin>>x;
return 0;
}
(To Be Continued)