一、首先,这个多项式是一个链表,多项式的每一项是链表一个节点,那么可以想到两种情况:
1)多项式只有一项或者是多项式的最后一项,那么这个节点就只需要有系数和指数两个元素,且不需要指向下 一个节点。
2)多项式的其中一项,那么这个节点就需要有系数、指数以及指向下一个节点的指针。
class Node
{
private:
int coef;
int exp;
Node * link;
public:
Node (int c, int e):coef(c),exp(e) //多项式不含指向下一个节点的构造函数
{
link = 0;
}
Node (int c, int e, Node * next):coef(c),exp(e) //含有指向下一个节点的构造函数
{
link = next;
}
}
另外节点要有插入的功能,所以节点类中要有公有成员函数Insert()函数:
Node * Insert(int c, int e)
{
link = new Node (c,e,link);
return link;
}
二、链表是由节点构成的,且链表要有添加节点、输出链表以及链表相加的功能。
class nodeList
{
private:
Node * theList;
public:
nodeList();
~nodeList();
void AddNode(istream & in);
void Output(ostream & out);
void ListAdd(nodeList & r);
}
三、由于对象是类,对于输出操作符不能直接输出节点类,所以我们要做一个运算符重载。
ostream & operator << (ostream & out, const Node & val)
整个思路即为上面的三大块,现在一块一块的分开分析。
第一块,链表元素节点:
class Node
{
private:
int coef; //系数
int exp; //指数
Node * link; //指向下一个节点的指针
public:
Node (int c, int e):coef(c),exp(e) //多项式不含指向下一个节点的构造函数
{
link = 0;
}
Node (int c, int e, Node * next):coef(c),exp(e) //含有指向下一个节点的构造函数
{
link = next;
}
Node * Insert (int c, int e) //在节点后插入一个节点
{
link = new Node(c,e,link);
return link;
}
friend class nodeList; //链接要操作节点
friend ostream & operator << (ostream &, const Node &); //节点的输出用到节点的私有成员
}
第二块:链表
class nodeList
{
friend ostream & operator << (ostream &, const nodeList & );
friend istream & operator >> (istream &, nodeList &);
friend nodeList & operator + (nodeList &, nodeList &);
private:
Node * theList;
public:
nodeList();
~nodeList();
void AddNode (istream & in);
void Output (ostream & out) const;
void Listadd (nodeList & r);
};
nodeList :: nodeList()
{
theList = new Node (0,-1);
theList ->link = theList;
}
nodeList::~nodeList()
{
Node * p = theList->link;
while (p != theList)
{
theList->link = p->link;
delete p;
p = theList->link;
}
delete theList;
}
void nodeList:: AddNode(istream & in)
{
Node * q = theList;
int count = 0;
int c,e;
for (;;)
{
cin >> c >>e;
if (c==0 && e ==-1)
{
break;
}
else
{
q = q->Insert(c,e);
}
count ++;
}
if (count == 0)
{
q = q->Insert(0,0);
cout << 0;
}
}
void nodeList:: Output (ostream & out )const
{
int i=0,j,k=0;
Node * m = theList->link;
for (; m!=theList & m->coef == 0;m=m->link)
{
i++;
}
m = theList->link;
for (;m!= theList; m=m->link)
{
k++;
}
m = theList->link;
if (k==1&& m->coef == 0)
{
cout << 0 << endl;
return;
}
if (k==i)
{
cout << 0<< endl;
return ;
}
for (j =0; jlink;
}
out << *m;
m=m->link;
for (;m!=theList;m=m->link)
{
if (m->coef > 0)
{
cout << "+";
out << *m;
}
else if (m->coef < 0)
{
out << *m;
}
}
cout << endl;
}
void nodeList::Listadd(nodeList & r)
{
Node * q,*q1 = theList,*p;
p = r.theList->link;
q = q1->link;
while(p->exp >= 0)
{
while (p->exp < q->exp)
{
q1=q;
q=q->link;
}
if (p->exp == q->exp)
{
q->coef = p->coef + q->coef;
if (p->coef == 0)
{
q1->link = q->link;
delete q;
q = q1->link;
}
else
{
q1=q;
q=q->link;
}
}
else
q1 = q1->Insert(p->coef,p->exp);
p=p->link;
}
}
ostream & operator <<(ostream & out, const nodeList & x)
{
x.Output(out);
return out;
}
istream & operator >> (istream & in, nodeList & x)
{
x.AddNode (in);
return in;
}
nodeList & operator + (nodeList & a, nodeList & b )
{
a.Listadd (b);
return a;
}
第三块:运算符重载,输出对象是节点类
ostream & operator << (ostream & out, const Node & val)
{
if (val.coef == 1)
{
switch (val.exp)
{
case 0:out << 1;break;
case 1:out << "X";break;
default : out << "X^" << val.exp; break;
}
return out;
}
if (val.coef == -1)
{
switch (val.exp)
{
case 0:out << -1;break;
case 1:out << "-X"; break;
default : out << "-X^"<< val.exp;break;
}
return out;
}
else
{
out << val.coef;
switch (val.exp)
{
case 0:break;
case 1:out <<"X"; break;
default : out<<"X^" << val.exp;break;
}
return out;
}
}