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

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;
}
}
...全文
155 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
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
  • 打赏
  • 举报
回复
有点难度哦
knowledge_Is_Life 2008-05-01
  • 打赏
  • 举报
回复
我也想了解,谢谢LZ.

33,027

社区成员

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

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