多项式相乘~小弟我如何的不到正确结果,请帮帮忙~
多项式相乘~我怎么的不到正确结果,请大虾帮帮忙~~
#include <iostream.h>
class List;
class Item
{
friend class List;
private:
double quot;
int exp;
Item *next;
public:
Item(double _quot,int _exp)
{quot=_quot;exp=_exp;next=NULL;}//1 初始化各元素 quot系数,exp指数
};
class List
{
private:
Item *list;
public:
List()
{
list=NULL;
}
void reverseList();
void multiplyList(List L1,List L2);
void createList();
};
void List::createList()
{
Item *p,*u,*pre;
int exp;
double quot;
list=NULL;
while(1)
{
cout < < "输入多项式中的一项(系数,指数): " < <endl;
cin> > quot> > exp;
if(exp < 0)
break; //指数小于0,输入结束
if(exp == 0)
continue;
p=list;
while(p != NULL && p-> exp > exp)//2 查找插入点,插入
{
pre=p;
p=p-> next;
}
if(p != NULL && exp == p-> exp)//输入的指数与前面的相同,将系数相加
{
p-> quot+=quot;
continue;
}
u=new Item(quot,exp);//3 p为空,表中就u一项
if(p == list)
list = u;
else
pre-> next = u; //非空,把u插入到指定位置
u-> next = p;
}
}
void List::reverseList()
{
Item *p,*u;
if(list==NULL)
return; //为空 退出
p=list-> next;
list-> next=NULL;
while(p!=NULL)
{
u=p-> next;
p-> next=list;
list=p;
p=u;
}
}
void List::multiplyList(List L1,List L2)
{
Item *pL1,*pL2,*u;
int k,maxExp;
double quot;
maxExp=L1.list-> exp + L2.list-> exp;//4
L2.reverseList();
list=NULL;
for(k = maxExp;k > = 0;k --)
{
pL1 = L1.list;
while(pL1!=NULL && pL1-> exp > k)
pL1 = pL1-> next;
pL2 = L2.list;
while(pL2!=NULL && (pL1-> exp + pL2-> exp) < k)//5
pL2 = pL2-> next;
quot=0.0;
while(pL2!=NULL && pL1!=NULL )
{
if((pL1-> exp + pL2-> exp) == k)
{
quot += pL1-> quot*pL2-> quot;//6
pL1=pL1-> next;
pL2=pL2-> next;
}
else if((pL1-> exp + pL2-> exp) > k)
pL1=pL1-> next;
else
pL2=pL2-> next;
}
if(quot!=0.0)
u=new Item(quot,k);
u-> next=list;
list=u;
}
L2.reverseList();
#include <iostream.h>
class List;
class Item
{
friend class List;
private:
double quot;
int exp;
Item *next;
public:
Item(double _quot,int _exp)
{quot=_quot;exp=_exp;next=NULL;}//1 初始化各元素 quot系数,exp指数
};
class List
{
private:
Item *list;
public:
List()
{
list=NULL;
}
void reverseList();
void multiplyList(List L1,List L2);
void createList();
};
void List::createList()
{
Item *p,*u,*pre;
int exp;
double quot;
list=NULL;
while(1)
{
cout < < "输入多项式中的一项(系数,指数): " < <endl;
cin> > quot> > exp;
if(exp < 0)
break; //指数小于0,输入结束
if(exp == 0)
continue;
p=list;
while(p != NULL && p-> exp > exp)//2 查找插入点,插入
{
pre=p;
p=p-> next;
}
if(p != NULL && exp == p-> exp)//输入的指数与前面的相同,将系数相加
{
p-> quot+=quot;
continue;
}
u=new Item(quot,exp);//3 p为空,表中就u一项
if(p == list)
list = u;
else
pre-> next = u; //非空,把u插入到指定位置
u-> next = p;
}
}
void List::reverseList()
{
Item *p,*u;
if(list==NULL)
return; //为空 退出
p=list-> next;
list-> next=NULL;
while(p!=NULL)
{
u=p-> next;
p-> next=list;
list=p;
p=u;
}
}
void List::multiplyList(List L1,List L2)
{
Item *pL1,*pL2,*u;
int k,maxExp;
double quot;
maxExp=L1.list-> exp + L2.list-> exp;//4
L2.reverseList();
list=NULL;
for(k = maxExp;k > = 0;k --)
{
pL1 = L1.list;
while(pL1!=NULL && pL1-> exp > k)
pL1 = pL1-> next;
pL2 = L2.list;
while(pL2!=NULL && (pL1-> exp + pL2-> exp) < k)//5
pL2 = pL2-> next;
quot=0.0;
while(pL2!=NULL && pL1!=NULL )
{
if((pL1-> exp + pL2-> exp) == k)
{
quot += pL1-> quot*pL2-> quot;//6
pL1=pL1-> next;
pL2=pL2-> next;
}
else if((pL1-> exp + pL2-> exp) > k)
pL1=pL1-> next;
else
pL2=pL2-> next;
}
if(quot!=0.0)
u=new Item(quot,k);
u-> next=list;
list=u;
}
L2.reverseList();