数据结构中多项式求和问题
书中给出了头文件Polynomial.h和operator+的定义,却没有驱动程序,不是很懂,老师还要求写乘法,请问大侠们,对应的加法驱动程序该怎么写额
//Polynomial.h
#ifndef POLYNOMIAL
#define POLYNOMIAL
template <typename int>
class Polynomial
{
private:
class Term
{
public:
CoefType coef;
unsigned expo;
};//类Term结束
/***Node类***/
class Node
{
public:
Term data;
Node *next;
//--Node构造函数
//根据给定的初始值来构造一个Node
Node(CoefType co=0,int ex=0,Node *ptr=0)
{data.coef=co;
data.expo=ex;
next=ptr;
}
};//Node类结束
typedef Node * NodePointer;
public:
Polynomial<CoefType> operator+(const Polynomial<CoefType>&secondPoly);
/***函数成员***/
//多项式操作
private:
/***数据成员***/
int myDegree;
NodePointer myTermss;//一个具有头节,元素类型为Term的链表
};//Polynomial类模板结束
#endif
template <typename CoefType>
Polynomial <CoefType> Polynomial<CoefType>::operator+(
const Polynomial<CoefType>&secondPoly)
{
Polynomial<CoefType> sumPoly;
Polynomial<CoefType>::NodePointer
ptra = myTerms->next,
ptrb = secondPoly.myTerms->next,
ptrc = sumPoly.myTerm;
int degree = 0;
while(ptra != 0 || ptrb != 0)
{
if((ptrb==0) || (ptra !=0) && ptra->data.expo < ptrb->data.expo)
{
ptrc->next = new Polynomial<CoefType>::
Node(ptra->data.coef,ptra->data.expo);
degree = ptra->data.expo;
ptra = ptra->next;
ptrc = ptrc->next;
}
else if((ptra == 0) ||
(ptrb != 0 && ptrb->expo < ptra->expo))
{
ptrc->next = new Polynomial<CoefType>::
Node(ptrb->data.coef,ptrb->data.expo);
degree = ptrb->data.expo;
ptrb = ptrb->next;
ptrc = ptrc->next;
}
else
{
CoefType sum = ptra->data.coef + ptrb->data.coef;
if(sum != 0)
{
ptrc->next = new Pplynomial<CoefType>::
Node(sum,ptra->data.expo);
degree = ptra->data.expo;
ptrc = ptrc->next;
}
ptra = ptra->next;
ptrb = ptrb->next;
}
}
sumPoly.myDegree = degree;
return sumPoly;
}