第1个回答 2009-12-14
/*这是第二个题的。应该欧可!自己研究研究*/
#include <iostream.h>
#include <stdlib.h>
//using namespace std;
struct Term{ //多项式的项结点
Term(int c,int e):coef(c),exp(e){link = NULL;} //构造函数1
Term(int c,int e,Term* next):coef(c),exp(e){link = next;}//构造函数2
Term * InsertAfter(int c,int e);//构造一个新项<c,e>结点,插在*this及其后继间
int coef;
int exp;
Term *link;
};
ostream&operator<<(ostream& out,const Term &val)
{
if(val.coef == 0) return out;
switch(val.exp){
case 0:out<<val.coef;break;
case 1:if(val.coef!=1) out<<val.coef;
out<<"X";break;
default:if(val.coef!=1)out<<val.coef;
out<<"X^"<<val.exp;break;
}
return out;
}
Term* Term::InsertAfter(int c,int e)
{
link = new Term(c,e,link);//构建新项结点<c,e>,插在当前结点及其后继结点间
return link; //函数返回值为新项结点的地址
}
class Ploynominal //多项式类
{
public:
Ploynominal();//构造只有表头结点的单循环链表
// ~Ploynominal();//释放多项式的各结点空间
void AddTerms(istream& in);
void Output(ostream& out)const;
void PolyAdd(const Ploynominal&r, const Ploynominal&m);
void PolyMuti(const Ploynominal&r,const Ploynominal&s,Ploynominal c[],Ploynominal e[]);
Ploynominal& operator+(const Ploynominal& b);
void operator=(const Ploynominal& x);
private:
Term *theList;//带头结点单循环链表存储一个多项式
int n;
friend ostream& operator<<(ostream&,const Ploynominal&);
friend istream& operator>>(istream&,Ploynominal&);
};
Ploynominal::Ploynominal()
{
theList = new Term(0,-1); //带头结点的空单循环链表
theList->link = theList;
n = 0;
}
void Ploynominal::AddTerms(istream& in)
{
Term* q = theList;
int c,e;
for(;;){
cout<<"Input a term(coef,exp):\n"<<endl;
in>>c>>e;
if(e<0)break;
q = q->InsertAfter(c,e);
n++;
}
}
void Ploynominal::Output(ostream& out) const
{
bool start = true;
Term *p = theList->link;
out<<"The ploynominal is :\n"<<endl;
for(;p!=theList;p = p->link){
if(!start&&p->coef>0) out<<'+';
start = false;
out<<*p;
}
out<<endl;
}
istream& operator>>(istream&in, Ploynominal& ploy)
{
ploy.AddTerms(in);
return in;
}
ostream& operator<<(ostream&out,const Ploynominal& ploy)
{
ploy.Output(out);
return out;
}
int ExpComp(int x,int y)
{
if(x == y) return 0;
else if(x > y) return 1;
else return -1;
}
void Ploynominal::PolyAdd(const Ploynominal&r,const Ploynominal&m)
{
if (r.theList->link == r.theList)
return;
Term *q,*preq,*q2,*p,*w;//preq是q的前驱结点
preq = m.theList,q = m.theList->link;
p = r.theList->link;
w = theList;
while(p->exp>=0)
{
int a1,a2; //Term *g= w;
switch(ExpComp(p->exp,q->exp))
{
case -1:w= w->InsertAfter(q->coef,q->exp);preq = q;q = q->link;break;
case 0:
a1 = q->coef + p->coef;
a2 = q->exp;
if(a1 == 0)
{
q2 = q;
preq->link = q->link;
q = q->link;
p = p->link;
delete(q2);
}
else {
w = w->InsertAfter(a1,a2);
preq = q;q = q->link;p=p->link;
}
break;
case 1:
w= w->InsertAfter(p->coef,p->exp);
p = p->link;break;
}
}
}
void Ploynominal::PolyMuti(const Ploynominal&r,const Ploynominal&s, Ploynominal c[],Ploynominal e[])
{
Term *q,*q1,*prep,*prep2[10];
prep = r.theList;
q = r.theList->link;
q1 = s.theList->link;
for (int k=0;k<10;k++)
prep2[k] = c[k].theList;
int m =0;
int j,count=0;
if (r.n>s.n)
{
j = r.n;
}
else
{
j = s.n;
}
if (j==r.n)
{
while (q1!=s.theList)
{
for (int i=0;i<j;i++)
{
prep2[m] = prep2[m]->InsertAfter(q->coef*q1->coef,q->exp+q1->exp);
q = q->link;
}
q1 = q1->link;
q = q->link;
m++;
}
}
else
{
while (q!=r.theList)
{
for (int i=0;i<j;i++)
{
prep2[m] = prep2[m]->InsertAfter(q->coef*q1->coef,q->exp+q1->exp);
q1=q1->link;
}
q = q->link;
q1 = q1->link;
m++;
}
}
if (m==1)
cout <<c[0];
else
{
int l = 1;
e[0].PolyAdd(c[1],c[0]);
for (int i=2;i<=m;i++)
{
int k = 0;
e[l].PolyAdd(c[i],e[l-1]);
l++;
}
cout<<"两个多项式的积为:"<<e[l-2]<<endl;
}
}
int main(int argc, char *argv[])
{
Ploynominal a,b,c[10],d,e[10],f;
cout<<"输入第一个多项式:"<<endl;
cin>>a;
cout<<"输入第二个多项式:"<<endl;
cin>>b;
d.PolyMuti(a,b,c,e);
system("PAUSE");
return 0;
}