[求助]帮帮忙!我做的一个关于多项式加减的程序出错了
程序如下
题目 一元多项式加减运算
运行的时候说 画红线的地方错了
请好心人帮我改改
要是你们有做好的发一个上来好吗 |
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct pnode
{
float coef;
int expn;
struct pnode * next;
} pnode;
pnode * creatpolyn(char ch)
{
pnode * p, *s,*r;
float x; int y;
p=(pnode *)malloc(sizeof(pnode));
p->next=NULL;
printf("shuru\n",ch);
scanf("%f %d",&x,&y);
while(x!=0)
{
s=(pnode *)malloc(sizeof(pnode));
s->coef=x;
s->expn=y;
s->next=NULL;
if(p->next==NULL)
{
p->next=s;
r=s;
}
else
{
r->next=s;
r=s;
}
scanf("%f %d",&x,&y);
}
return p;
}
pnode *addpolyn(pnode * f,pnode * g)
{
pnode * fg;
pnode *t,*q,*s,*r;
float m;
t=f->next;
q=g->next;
fg=r=(pnode*)malloc(sizeof(pnode));
fg->next=NULL;
while(t&&q)
{
if(t->expn==q->expn)
{
m=t->coef+q->coef;
if(m!=0)
{
s=(pnode *)malloc(sizeof(pnode));
s->coef=m;
s->expn=t->expn;
s->next=NULL;
}
t=t->next;
q=q->next;
}
else
if(t->expn<q->expn)
{
s=(pnode *)malloc(sizeof(pnode));
s->coef=t->coef;
s->expn=t->expn;
s->next=NULL;
t=t->next;
}
else
{
s=(pnode *)malloc(sizeof(pnode));
s->coef=q->coef;
s->expn=q->expn;
s->next=NULL;
q=q->next;
}
if(fg->next==NULL)
{
fg->next=s;
r=s;
}
else
{
r->next=s;
r=s;
}
}
r->next=t?t:q;
return fg;
}
void outpolyn(pnode * f)
{
pnode *t;
t=f->next;
if(!f->next){
printf("0\n"); return;
}
while(t)
{ if(t->coef>0&&f->next!=t) printf("+");
if(t->expn==0)
printf("%f",t->coef);
else
printf("%f*X^%d",t->coef,t->expn);
t=t->next;
}
printf("\n");
}
/*一元多项式系数取反*/
pnode * opposite (phone f)
pnode f ;
p=f.head;
while(p->next)
{
p=p->next;
p->data.coef*=-1;
}
}
/*多项式减法*/
pnode * subtractpolyn(pnode *f,pnode *g)
{
opposite(g)
addpolyn(f,g)
}
void main()
{
open();
pnode * f,* g,* fg;
int i=-1;
while(i!=0)
{
scanf("%d",&i);
getchar();
switch(i)
{
case 0:
return;
case 1:
printf("duoxiangshi xiangjia:\n");
f=createpolyn('A');
printf("A=");
outpolyn(f);
g=createpolyn('B');
printf("B=");
outpolyn(g);
printf("A+B=");
fg=addpolyn(f,g);
outpolyn(fg);
i=-1;
break;
case 2:
printf("duoxiangshi xiangjian:\n");
f=createpolyn('A');
printf("A=");
outpolyn(f);
g=createpolyn('B');
printf("B=");
outpolyn(g);
printf("A-B=");
fg=subtractpolyn(f,g);
outpolyn(fg);
i=-1;
break;
case 3:
i=-1;
break;
default:
printf("shuruyouwu,congxinshuru\n");
}
}
}
[此贴子已经被作者于2005-6-8 23:57:44编辑过]