我这个 一元多项式相加 结果的系数为什么不变呢?(万分感谢)

gengyigang 2008-03-24 08:49:10
#include "stdlib.h"
typedef struct LNode
{
int coef;
int expn;
struct LNode *next;
}LNode,*ListNode;
ListNode CreateFromTail()
{
ListNode l,p,tail;
int m;
int n;
l=(ListNode)malloc(sizeof(LNode));
l->next=NULL;
tail=l;
printf("\nplease put the coef:");
scanf("%d",&m);
printf("\nplease put the expn:");
scanf("%d",&n);
while(m)
{
p=(ListNode)malloc(sizeof(LNode));
p->coef=m;
p->expn=n;
tail->next=p;
tail=p;
printf("\nplease put the coef:");
scanf("%d",&m);
printf("\nplease put the expn:");
scanf("%d",&n);
}
tail->next=NULL;
return l;
}
ListNode MergeList(ListNode la,ListNode lb)
{
ListNode ra,rb,pa,pb;
int sum;
ra=la;
rb=lb;
pa=la->next;
pb=la->next;
while(pa&&pb)
{
if(pa->expn>pb->expn)
{
ra=ra->next;
pa=pa->next;
}
else if(pa->expn <pb->expn)
{
rb->next=pb->next;
pb->next=ra->next;
ra->next=pb;
ra=ra->next;
pa=ra->next;
pb=rb->next;
}
else
{
if((pa->coef+pb->coef)!=0)
{
sum=pa->coef+pb->coef;
pa->coef=sum;
ra=ra->next;
pa=pa->next;
rb=rb->next;
pb=pb->next;
}
else
{
pa=pa->next;
pb=pb->next;
ra->next=pa;
rb->next=pb;
}
}
}
if(pb)
ra->next=pb;
free(lb);
return la;
}
main()
{
ListNode m,n,l,p;
printf("the first equation:");
m=CreateFromTail();
printf("\n");
printf("the second equation:");
n=CreateFromTail();
l=MergeList(m,n);
p=l->next;
while(p!=NULL)
{
printf(" %d %d\t",p->coef,p->expn);
p=p->next;
}
}
...全文
83 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
C1053710211 2008-05-25

#include "stdlib.h"
#include <stdio.h>
typedef struct LNode
{
int coef;
int expn;
struct LNode *next;
}LNode,*ListNode;

ListNode CreateFromTail()
{
ListNode l,p,tail;
int m;
int n;
l=(ListNode)malloc(sizeof(LNode));
l->next=NULL;
tail=l;
printf("\nplease put the coef:");
scanf("%d",&m);
printf("\nplease put the expn:");
scanf("%d",&n);
while(m)
{
p=(ListNode)malloc(sizeof(LNode));
p->coef=m;
p->expn=n;
tail->next=p;
tail=p;
printf("\nplease put the coef:");
scanf("%d",&m);
printf("\nplease put the expn:");
scanf("%d",&n);
}
tail->next=NULL;
return l;
}
ListNode MergeList(ListNode la,ListNode lb)
{
ListNode ra,rb,pa,pb;
int sum;
ra=la;
rb=lb;
pa=la->next;
pb=lb->next; //这里你原先是 pb=la->next;
while(pa&&pb)
{
if(pa->expn>pb->expn)
{
ra=ra->next;
pa=pa->next;
}
else if(pa->expn<pb->expn)
{
rb->next=pb->next;
pb->next=ra->next;
ra->next=pb;
ra=ra->next;
pa=ra->next;
pb=rb->next;
}
else
{
if((pa->coef+pb->coef)!=0)
{
sum=pa->coef+pb->coef;
pa->coef=sum;
ra=ra->next;
pa=pa->next;
rb=rb->next;
pb=pb->next;
}
else
{
pa=pa->next;
pb=pb->next;
ra->next=pa;
rb->next=pb;
}
}
}
if(pb)
ra->next=pb;
free(lb);
return la;
}
main()
{
ListNode m,n,l,p;
printf("the first equation:");
m=CreateFromTail();
printf("\n");
printf("the second equation:");
n=CreateFromTail();
l=MergeList(m,n);
p=l->next;
while(p!=NULL)
{
printf(" %d %d\t",p->coef,p->expn);
p=p->next;
}
system("pause");
}

回复
Supercaller 2008-05-25
结果中每一项都有系数,而且经过相加一般都会变化,但是lz说的系数不变有几种可能:
1:你运行时输入的两个多项式中相同指数的两项的系数都为相反数(你的程序自身没错)
2:你运行时输入的两个多项式中根本就不存在任何指数相同的两项(你的程序自身没错)
3:你运行的结果中某些变了,但你没有注意到(你的程序)
4:由于我这里没有工具,所以只能猜测一下,下次希望能找出原因
回复
meiZiNick 2008-05-01
有点难度哦
回复
我也想了解,谢谢LZ.
回复
相关推荐
发帖
数据结构与算法
创建于2007-08-27

3.2w+

社区成员

数据结构与算法相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2008-03-24 08:49
社区公告
暂无公告