HF199
HF199
2017-10-11 12:31

我这个程序为什么实现不了一元多项式的运算

  • pc

#include
#include
#include

#define TRUE 1

#define FALSE 0

#define OK 1

#define ERROR 0

#define INFEASIBLE -1

#define OVERFLOW -2

#define LT(a,b) ((a)<(b))

#define N = 100

typedef int Status;

typedef int ElemType;

typedef struct polynode
{ int coef;

int expn;

struct polynode *next;
} polynode,*polylist;

void poly_create(polylist &L){

int i,a ;
scanf("%d",&a);
L=(polylist)malloc(sizeof(polynode)) ;

L->next=NULL;
polylist p,q;
q=L;
for(i=0;i {
p=(polylist)malloc(sizeof(polynode));
scanf("%d%d",&p->coef,&p->expn);
p->next=q->next;
q->next=p;
q=q->next;
}
}

void poly_display(polylist L)
{
polylist p;

p=L->next;
while(p)
{

printf("%d""X""(%d)",p->coef,p->expn);
if(p->next&&p->next->coef>0) printf("+") ;
p = p->next;

}

}
void poly_add(polylist La, polylist Lb, polylist &Lc)
{polylist pa,pb,pc,b;
pa=La->next;
pb=Lb->next;
Lc=(polylist)malloc(sizeof(polynode));
b=Lc;
while (pa&&pb)

{ if (pa->expnexpn)
{pc=(polylist)malloc(sizeof(polynode));
pc->expn=pa->expn;
pc->coef=pa->coef;
pc->next=b->next;
b->next=pc;
b=b->next;pa=pa->next;}

if (pa->expn>pb->expn)
{pc=(polylist)malloc(sizeof(polynode));
pc->expn=pb->expn;
pc->coef=pb->coef;
pc->next=b->next;
b->next=pc;b=b->next;pb=pb->next;}

if((pa->expn==pb->expn)&&(pa->coef+pb->coef!=0))
{pc=(polylist)malloc(sizeof(polynode));
pc->expn=pb->expn;
pc->coef=pb->coef+pa->coef; 
pc->next=b->next;
b->next=pc;b=b->next;pa=pa->next;
pb=pb->next;}
if(pa->expn==pb->expn&&pa->coef+pb->coef==0)
{
    pa=pa->next;
    pb=pb->next;
}

}
while (pa)
{
pc=(polylist)malloc(sizeof(polynode));
pc->expn=pa->expn;
pc->coef=pa->coef;
pc->next=b->next;
b->next=pc;
b=b->next;pa=pa->next;
}
while (pb)
{pc=(polylist)malloc(sizeof(polynode));
pc->expn=pb->expn;
pc->coef=pb->coef;
pc->next=b->next;
b->next=pc;b=b->next;pb=pb->next;

}

}

int main()
{ polylist La,Lb,Lc;
poly_create(La);
poly_display(La);
poly_create(Lb);
poly_display(Lb);
poly_add(La, Lb, Lc);
poly_display(Lc);

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答