一个一元多项式的相加问题。(C++编写,各位大虾指点)

scgjc 2003-04-30 06:47:15
程序主要目的是将两个多项式相加。每个多项式是一个链表,根据两个多项式的X的指数判断进行插入删除等操作,最后将合并后的多项式输出到屏幕上。我写的程序编译通过,但运算结果不正确。问题主要在多项式相加那一块。还请各位高手指教。
代码如下:
#include<iostream>
using namespace std;

struct ListNode //多项式的单项定义
{
int coef;
int exp;
ListNode *next;
};
//////////////////////////////////////////////////////////////////////
//多项式类polynomial的定义
class Polynomial
{
public:
ListNode *pNode;
ListNode *head; //链首指针
ListNode *pEnd; //链尾指针
public:
//////////////////////////////////////////////////////////////////
//构造函数
Polynomial()
{
pNode=new ListNode; //注意:pNode是一个指针
int i=0;
head=pNode;
pEnd=pNode;

while(pNode->coef!=0)
{

pEnd->next=pNode;

pEnd=pNode;
pNode=new ListNode;
i++;
cout<<"请输入第"<<i<<"项的系数:";
cin>>pNode->coef;
cout<<"请输入第"<<i<<"项的指数:";
cin>>pNode->exp;
}
pEnd->next=0;
delete pNode;
}

/////////////////////////////////////////////////////////////////////
/////遍历
void ShowList(ListNode *head)
{
ListNode *p=head;
cout<<"此多项式为: ";
if(p)
{
p=p->next;
cout<<p->coef<<"X("<<p->exp<<")";
p=p->next;
}
while(p)
{
cout<<"+"<<p->coef<<"X("<<p->exp<<")";
p=p->next;
}
cout<<endl;
}
/////////////////////////////////////////////////////////////////
///指数比较
char compare(int a,int b)
{
if(a>b) return '>';
if(a=b) return '=';
if(a<b) return '<';
}
/////////////////////////////////////////////////////////////////
///多项式相加
Polynomial AddPoly(Polynomial &ah,Polynomial &bh)
{
ListNode *pa,*pb,*pc,*pd,*p;
pa=pc=ah.head;
pb=pd=bh.head;
pa=pa->next;
pb=pb->next;
while( pa && pb)
{
switch( compare(pa->exp,pb->exp))
{
case '<':
pa=pa->next;
pc=pc->next;
break;
case '=':
pa->coef=pa->coef+pb->coef;
if(pa->coef)
{
pa=pa->next;
pc=pc->next;
pb=pb->next;
pd=pd->next;
}
else
{
p=pa;
pa=pa->next;
pc=p->next;
p=pb;
pb=pb->next;
pd=p->next;
}
break;
case '>':
p=pb;
pb=pb->next;
p->next=pc->next;
pc->next=p;
break;
}
}
if(pa->next=NULL) p->next=pb;
return ah;
}
};
/////////////////////////////////////////////////////////////////////
/////主函数

void main()
{
Polynomial a;
a.ShowList(a.head);
Polynomial b;
b.ShowList(b.head);
Polynomial c=a.AddPoly(a,b);
c.ShowList(c.head);
}
...全文
568 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
bm1408 2003-05-02
  • 打赏
  • 举报
回复
难点在于如果为0的话,要把此结点删除!
其它的没有什么了!
数据结构书上多的是!
自己看吧!
想要的话,可以给我发短信!
bm1408 2003-05-02
  • 打赏
  • 举报
回复
c语言的要不要?
ZhangYv 2003-05-02
  • 打赏
  • 举报
回复
是啊,但开一个index[100]的空间,用完就释放没什么大不了的。但这种做法的好处是利用空间来换时间。如果是利用链表,在定位幂次那里最多可能花费O(n^2)的时间,而开一个索引数组则可以随机定位,建索引要O(n)时间,随机访问只要O(1)时间。而且可以减少编程复杂度,总得来说是划得来。我上面的pascal程序就是用这种方法做。
用链表和数组各有优势。
jerryllk 2003-05-02
  • 打赏
  • 举报
回复
ZhangYv ,照你那么做的话,如果多项式是3*X^100+5*x+1,岂不是要用int index[100],很浪费空间。
我觉得你的方法行不通。
scgjc 2003-05-02
  • 打赏
  • 举报
回复
up
yanpinghui 2003-05-01
  • 打赏
  • 举报
回复
http://www.jxyc.gov.cn/leida/bbs讨论过这个问题。。
ZhangYv 2003-05-01
  • 打赏
  • 举报
回复
是定位幂次那里有问题...
ZhangYv 2003-05-01
  • 打赏
  • 举报
回复
你那样也行,如果相加结果出错的话最后可能是定位系数那里有问题,再检查一下吧。你可以参考线性表的LOCATE运算的写法。
scgjc 2003-04-30
  • 打赏
  • 举报
回复
ZhangYv() :
你的意思我似乎懂得一点,但pascal我实在看不懂。我只需将程序正确调试出来即可。不过还是谢谢你!
ZhangYv 2003-04-30
  • 打赏
  • 举报
回复
直接用链表来实现加or乘很不直观,可以为多项式的幂建个索引index[MaxSize]。于是计算加法时可这样:index[式子->幂] += 式子->系数,比如计算式子A:3*x^2 - x + 1和B:2*x-1的相加结果
1) index[3] = {0, 0, 0};
2) index[2] += 3; index[1] += -1; index[0] += 1; //A的结果
3) index[1] += 2; index[0] += -1; //和B相加
乘法也类似,index[式子A->幂 + 式子B->幂] += 式子A->系数 * 式子B->系数

可以参考这里一段pascal实现多项式加和乘法的程序。
program Multiply;
type
Pointer = ^NODE;
NODE = record
n :integer;
e :integer;
next :Pointer;
end;

procedure Create(var head :Pointer ;n :integer);
var
link :Pointer;
i :integer;
begin
link := head;
for i := 0 to n do
begin
new (link^.next);
link := link^.next;
link^.n := random (10);
link^.e := n-i;
end;
link^.next := NIL;
end;

procedure Print(var link :Pointer);
var
p :Pointer;
begin
p := link^.next;
while (p <> Nil) and (p^.n = 0) do p := p^.next;
if (p = Nil) then Exit;
Write (p^.n ,'x^' ,p^.e);
p := p^.next;
while (p <> NIL) do
begin
if (p^.n <> 0) then
begin
if (p^.n <> 1) then Write ('+' ,p^.n);
if (p^.e >1) then Write ('x^' ,p^.e)
else
if (p^.e = 1) then Write ('x');
end;
p := p^.next;
end;
Writeln;
end;
{End Print}
procedure Main;
type
Index = array [0..30] of Pointer;
var
headA ,headB ,headC ,p :Pointer;
indexArr :Index;
max :integer;
procedure Initial(var indexArr :Index ;max :integer);
var
i :integer;
begin
for i := 0 to max do
begin
indexArr[i]^.n := 0;
indexArr[i]^.e := i;
indexArr[i+1]^.next := indexArr[i];
end;
indexArr[0]^.next := Nil;
end;
{End Initial}
procedure Mul(var headA ,headB :Pointer ;var indexArr :Index);
var
p ,q :Pointer;
begin
p := headA^.next;
while (p <> Nil) do
begin
q := headB^.next;
while (q <> Nil) do
begin
indexArr[q^.e+p^.e]^.n := indexArr[q^.e+p^.e]^.n+p^.n*q^.n;
q := q^.next;
end;
p := p^.next;
end;
end;
{End Initial}
{procedure Main}
begin
Randomize;
New (headA);
New (headB);
Read (headA^.e ,headB^.e);
Create(headA ,headA^.e);
Create(headB ,headB^.e);
Print(headA);
Print(headB);
max :=headA^.e+headB^.e;
Initial(indexArr ,max);
headC^.e := max;
headC^.next := indexArr[max];
Mul(headA ,headB ,indexArr);
Print(headC);
end;

begin
Main;
end.

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧