为什么我的多项式不能相加
#include<stdio.h>#include<malloc.h>
#include<stdlib.h>
typedef struct Polynomial
{
float coef;
int expn;
struct Polynomial *next;
}*Polyn;
void InitPolyn(Polyn *P);
void CreatPolyn(Polyn *P,int m);//创建一个多项式
void ShowPolyn(Polyn P);//打印出多项式
void SortPolyn(Polyn P);//按指数降序排列
void AddPolyn(Polyn Pa,Polyn Pb,Polyn Pc);//Pc = Pa + Pb
void SubPolyn(Polyn Pa,Polyn Pb,Polyn Pc);//Pc = Pa - Pb
void valPolyn(Polyn *P,int x);//求多项式当X为多少的值
void InitPolyn(Polyn *P)
{
*P = (Polyn )malloc(sizeof(Polyn));
(*P)->coef = 0.0;
(*P)->expn = 0;
(*P)->next = NULL;//初始化
}
void CreatPolyn(Polyn *P,int m)
{
float c;
int e,i;
Polyn s,q;
q = *P;
printf("请输入%d个项数\n",m);
for(i = 1;i<=m;i++)
{
printf("请输入第%d项的系数和指数:",i);
scanf("%f %d",&c,&e);
s = (Polyn )malloc(sizeof(Polyn));
s->coef = c;
s->expn = e;
s->next = NULL;
q->next = s;
q = s;
q->next = NULL;
}
SortPolyn(*P);
}
void SortPolyn(Polyn P)
{
Polyn p,q,r;
q=P->next;
P->next=NULL;
while(q!=NULL) //插入法排序
{
p=P;
while(p->next!=NULL&&p->next->expn>q->expn)
p=p->next;
r=q->next;
q->next=p->next;
p->next=q;
q=r;
}
}
void ShowPolyn(Polyn P)
{
Polyn pchar;
pchar = P->next;
while(pchar)
{
printf("%fX^%d ",pchar->coef,pchar->expn);
pchar = pchar->next;
}
printf("\n");
}
void AddPolyn(Polyn Pa,Polyn Pb,Polyn Pc)
{
float x;
Polyn anext,bnext,q,s;
anext = Pa->next;
bnext = Pb->next;
q = Pc;
while(anext&&bnext)
{
if(anext->expn == bnext->expn)//指数相同
{
x = anext->coef+bnext->coef;
if(x!=0)//系数相加不等于0,插入表中
{
s = (Polyn)malloc(sizeof(Polyn));
s->coef = x;
s->expn = anext->expn;
s->next = NULL;
q->next = s;
q = q->next;
}
else//系数等于0
{
anext = anext->next;
bnext = bnext->next;
}
}
else if(anext->expn < bnext->expn)//指数小的插入表中
{
s = (Polyn)malloc(sizeof(Polyn));
s->coef = anext->coef;
s->expn = anext->expn;
s->next = NULL;
q->next = s;
q = q->next;
anext = anext->next;
}
else
{
s = (Polyn)malloc(sizeof(Polyn));
s->coef = bnext->coef;
s->expn = bnext->expn;
s->next = NULL;
q->next = s;
q = q->next;
bnext = bnext->next;
}
}
while(anext == NULL&&bnext!=NULL)
{
s = (Polyn)malloc(sizeof(Polyn));
s->coef = bnext->coef;
s->expn = bnext->expn;
s->next = NULL;
q->next = s;
q = q->next;
bnext = bnext->next;
}
while(bnext == NULL&&bnext!=NULL)
{
s = (Polyn)malloc(sizeof(Polyn));
s->coef = anext->coef;
s->expn = anext->expn;
s->next = NULL;
q->next = s;
q = q->next;
anext = anext->next;
}
if(anext == NULL&&bnext == NULL)
{
printf("完成\n");
}
SortPolyn(q);
}
int main()
{
Polyn Pa,Pb,Pc;
InitPolyn(&Pa);
CreatPolyn(&Pa,3);
ShowPolyn(Pa);
InitPolyn(&Pb);
CreatPolyn(&Pb,2);
ShowPolyn(Pb);
InitPolyn(&Pc);
AddPolyn(Pa,Pb,Pc);
ShowPolyn(Pc);
}
为什么相加不了?出现不了“完成”这两个字 修正一点儿粗心小错误:#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct Polynomial
{
float coef;
int expn; //指数
struct Polynomial *next;
}*Polyn;
void InitPolyn(Polyn *P);
void CreatPolyn(Polyn *P,int m);//创建一个多项式
void ShowPolyn(Polyn P);//打印出多项式
void SortPolyn(Polyn P);//按指数降序排列
void AddPolyn(Polyn Pa,Polyn Pb,Polyn Pc);//Pc = Pa + Pb
void SubPolyn(Polyn Pa,Polyn Pb,Polyn Pc);//Pc = Pa - Pb
void valPolyn(Polyn *P,int x);//求多项式当X为多少的值
void InitPolyn(Polyn *P)
{
*P = (Polyn )malloc(sizeof(Polyn));
(*P)->coef = 0.0;
(*P)->expn = 0;
(*P)->next = NULL;//初始化
}
void CreatPolyn(Polyn *P,int m)
{
float c;
int e,i;
Polyn s,q;
q = *P;
printf("请输入%d个项数\n",m);
for(i = 1;i<=m;i++)
{
printf("请输入第%d项的系数和指数:",i);
scanf("%f %d",&c,&e);
s = (Polyn )malloc(sizeof(Polyn));
s->coef = c;
s->expn = e;
s->next = NULL;
q->next = s;
q = s;
q->next = NULL;
}
SortPolyn(*P);
}
void SortPolyn(Polyn P)
{
Polyn p,q,r;
q=P->next;
P->next=NULL;
while(q!=NULL) //插入法排序
{
p=P;
while(p->next!=NULL&&p->next->expn>q->expn)
p=p->next;
r=q->next;
q->next=p->next;
p->next=q;
q=r;
}
}
void ShowPolyn(Polyn P)
{
Polyn pchar;
pchar = P->next;
while(pchar)
{
printf("%fX^%d ",pchar->coef,pchar->expn);
pchar = pchar->next;
}
printf("\n");
}
void AddPolyn(Polyn Pa,Polyn Pb,Polyn Pc)
{
float x;
Polyn anext,bnext,q,s;
anext = Pa->next;
bnext = Pb->next;
q = Pc;
while(anext&&bnext)
{
if(anext->expn == bnext->expn)//指数相同
{
x = anext->coef+bnext->coef;
if(x!=0)//系数相加不等于0,插入表中
{
s = (Polyn)malloc(sizeof(Polyn));
s->coef = x;
s->expn = anext->expn;
s->next = NULL;
q->next = s;
q = q->next;
}
else//系数等于0
{
anext = anext->next;
bnext = bnext->next;
}
}
else if(anext->expn < bnext->expn)//指数小的插入表中
{
s = (Polyn)malloc(sizeof(Polyn));
s->coef = anext->coef;
s->expn = anext->expn;
s->next = NULL;
q->next = s;
q = q->next;
anext = anext->next;
}
else
{
s = (Polyn)malloc(sizeof(Polyn));
s->coef = bnext->coef;
s->expn = bnext->expn;
s->next = NULL;
q->next = s;
q = q->next;
bnext = bnext->next;
}
}
while(anext == NULL&&bnext!=NULL)
{
s = (Polyn)malloc(sizeof(Polyn));
s->coef = bnext->coef;
s->expn = bnext->expn;
s->next = NULL;
q->next = s;
q = q->next;
bnext = bnext->next;
}
while(bnext == NULL&&anext!=NULL)
{
s = (Polyn)malloc(sizeof(Polyn));
s->coef = anext->coef;
s->expn = anext->expn;
s->next = NULL;
q->next = s;
q = q->next;
anext = anext->next;
}
if(anext == NULL&&bnext == NULL)
{
printf("完成\n");
}
SortPolyn(q);
}
int main()
{
Polyn Pa,Pb,Pc;
InitPolyn(&Pa);
CreatPolyn(&Pa,3);
ShowPolyn(Pa);
InitPolyn(&Pb);
CreatPolyn(&Pb,2);
ShowPolyn(Pb);
InitPolyn(&Pc);
AddPolyn(Pa,Pb,Pc);
ShowPolyn(Pc);
}
小甲鱼 发表于 2014-4-15 19:56 static/image/common/back.gif
修正一点儿粗心小错误:
甲鱼哥,我仔细对照你改过的代码,功力不深,未发现改在哪。。 还有为什么我这个AddPolyn函数相加不了呢?C:\Users\HP\Desktop\1.jpg不知道图片出来没,,函数出来的不是相加的样子。感谢甲鱼哥百忙之中改错{:1_1:} 第二个:while(bnext == NULL&&anext!=NULL) 小甲鱼 发表于 2014-4-16 23:06 static/image/common/back.gif
第二个:while(bnext == NULL&&anext!=NULL)
:dizzy: 看到了,,,甲鱼哥当真好眼力。 不过看来相加的这个函数还是要自己去琢磨了。。:mad: a372187663 发表于 2014-4-16 23:16 static/image/common/back.gif
看到了,,,甲鱼哥当真好眼力。 不过看来相加的这个函数还是要自己去琢磨了。。
嗯,具体的逻辑你最好是用张纸分开写一下,这样不容易出错。 小甲鱼 发表于 2014-4-16 23:17 static/image/common/back.gif
嗯,具体的逻辑你最好是用张纸分开写一下,这样不容易出错。
好的,谢谢哈!{:1_1:}{:1_1:} 小甲鱼 发表于 2014-4-16 23:06 static/image/common/back.gif
第二个:while(bnext == NULL&&anext!=NULL)
void AddPolyn(Polyn Pa,Polyn Pb)
{
Polyn ahead,bhead,qa,qb;
float sum;
Polyn temp,temp1;
qa = Pa->next;
qb = Pb->next;
ahead = Pa;
bhead = Pb;
while(ahead->next&&bhead->next)
{
switch(cmp(ahead->expn,bhead->expn))//比较系数大小, <=>返回 -1, 0, 1。
{
case -1:
ahead = qa;
qa = qa->next;
break;
case 0:
sum = ahead->coef + bhead->coef;
if(sum!=0)
{
ahead->coef = sum;
temp = bhead;
bhead = qb;
qb = qb->next;
free(temp);
}
else
{
temp = ahead;
ahead = qa;
qa = qa->next;
free(temp);
temp1 = bhead;
bhead = qb;
qb = qb->next;
free(temp1);
}
break;
case 1:
temp = bhead;
bhead = qb;
qb = qb->next;
ahead = temp;
temp->next = qa;
qa = ahead;
ahead->next = qa;
}
}
if(bhead->next)
{
ahead->next = bhead;
free(bhead);
}
}
甲鱼哥,这是我在纸上画图想出来的算法。但是出现了下面这个问题。
Debug Error!
Program:...\myProgram.exe
DAMAGE:after Normal block(#76)at 0x004F2A70.
(Press Retry to debug the application)
终止 重试忽略
不知道怎么回事,还请帮忙解释一下。
页:
[1]