| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
 编程论坛祝所有会员中秋、国庆双节快乐 金秋十月，喜迎中秋，欢度国庆

```#include<stdio.h>
#include<malloc.h>

struct node
{
int coef;
int expn;
struct node * next;
}
typedef struct node polyn;

polyn *createList()
{
int i,c,d;
p->coef=-1;
p->next=NULL;
for(i=1;;i++)
{    printf("请输入第%d项的系数以及指数\n",i);
scanf("%d,%d",&c,&d);
while((c!=0)||(d!=0))
{if(d<p->coef)
printf("输入错误\n");
if(c==0)
printf("系数不能为0!\n");
else
{q=(polyn *)malloc(sizeof(polyn));
q->coef=c;
q->expn=d;
q->next=NULL;
p->next=q;
p=q;
}
}
}

polyn *subpolyn(polyn *ha,polyn *hb)
{
int i;
polyn *p,*q,*s,*t;
p=ha;
q=ha->next;
s=hb->next;
while((q!=0)&&(s!=0))
{
if((q->expn)<(s->expn))
{
p=q;
q=q->next;
}
if((q->expn)>(s->expn))
{
t=(polyn *)malloc(sizeof(polyn));
t->coef=s->coef;
t->expn=s->expn;
t->next=q;
p->next=t;
p=t;
s=s->next;
}
else
{
i=(q->coef)-(s->coef);
if(i==0)
{
p->next=q->next;
free(q);
q=p->next;
s=s->next;
}
else
{
p->coef=i;
p=q;
q=q->next;
s=s->next;
}
}
}
if(s!=NULL)
{q->next=s;}
return(ha);
}

{
polyn *p;
while(p!=NULL)
{printf("--> <&d,&d>",p->coef,p->expn);
p=p->next;
}
}

main()
{
polyn *ha,*hb;
printf("intput ha:\n");
ha=createList();
printf("intput hb:\n");
hb=createList();
printf("output ha:\n");
printpolyn(ha);
printf("output hb:\n");
printpolyn(hb);
ha=subpolyn(ha,hb);
printf("多项式相减的结果\n");
printpolyn(ha);
}

```

#include<stdio.h>
#include<malloc.h>

struct node
{
int coef;
int expn;
struct node * next;
};       //此处少了分号
typedef struct node polyn;

polyn *createList()
{
int i,c,d;
p->coef=-1;
p->next=NULL;
for(i=1;;i++)
{    printf("请输入第%d项的系数以及指数\n",i);
scanf("%d,%d",&c,&d);
while((c!=0)||(d!=0))
{if(d<p->coef)
printf("输入错误\n");
if(c==0)
printf("系数不能为0!\n");
else
{q=(polyn *)malloc(sizeof(polyn));
q->coef=c;
q->expn=d;
q->next=NULL;
p->next=q;
p=q;
}
}
}    //此处少了大括号。
}

[此贴子已经被作者于2016-9-18 23:42编辑过]

```#include<stdio.h>
#include<malloc.h>

struct node
{
int coef;
int expn;
struct node * next;
};
typedef struct node polyn;

polyn *createList()
{
int i=1;
int j,k;
p->expn= -1;
p->next=NULL;
printf("请输入第%d项的系数以及指数\n",i);
scanf("%d,%d",&j,&k);
while((j!=0)||(k!=0))
{if(k<=p->expn)
printf("输入错误\n");
else
if(j==0)
printf("系数不能为0!\n");
else
{
q=(polyn *)malloc(sizeof(polyn));
q->coef=j;
q->expn=k;
q->next=NULL;
p->next=q;
p=q;
i++;
}
printf("请输入第%d项的系数以及指数\n",i);
scanf("%d,%d",&j,&k);
}
}

polyn *subpolyn(polyn *ha,polyn *hb)
{
int i;
polyn *p,*q,*s,*t;
p=ha;
q=ha->next;
s=hb->next;
while((q!=0)&&(s!=0))
{
if((q->expn)<(s->expn))
{
p=q;
q=q->next;
}
else
if((q->expn)>(s->expn))
{
t=(polyn *)malloc(sizeof(polyn));
t->coef=-(s->coef);
t->expn=s->expn;
t->next=q;
p->next=t;
p=t;
s=s->next;
}
else
{
i=q->coef-s->coef;
if(i==0)
{
p->next=q->next;
free(q);
q=p->next;
s=s->next;
}
else
{
q->coef=i;
p=q;
q=q->next;
s=s->next;
}
}
}
if(s!=NULL)
{p->next=s;}
return(ha);
}

{
polyn *p;
while(p!=NULL)
{printf("--> <%d,%d>",p->coef,p->expn);
p=p->next;
}
printf("\n");
}

main()
{
polyn *ha,*hb;
printf("intput ha:\n");
ha=createList();
printf("intput hb:\n");
hb=createList();
printf("output ha:\n");
printpolyn(ha);
printf("output hb:\n");
printpolyn(hb);
ha=subpolyn(ha,hb);
printf("多项式相减的结果:\n");
printpolyn(ha);
system("pause");
}

```

` scanf("%d,%d",&j,&k);`

φ(゜▽゜*)♪

scanf("%d,%d",&j,&k);
while((j!=0)||(k!=0))     //这里的条件是j!=0或k!=0。
{if(k<=p->expn)       // 也就是说当k是负数时，会执行这句。接下来会发生什么不用我多解释吧。
printf("输入错误\n");

[此贴子已经被作者于2016-9-19 15:40编辑过]

[此贴子已经被作者于2016-9-19 22:59编辑过]

[此贴子已经被作者于2016-9-19 23:48编辑过]

[此贴子已经被作者于2016-9-20 23:19编辑过]

ha：3+4x+5x&sup2;
hb: 4+3x+6x&sup3;

-->(-1,0)-->(1,1)-->(5，2)-->(-6,3)

-->(1,0)-->(1,1)-->(5，2)-->(6,3)

t->coef=-(s->coef)的

[此贴子已经被作者于2016-9-21 09:08编辑过]

• 12
• 1/2页
• 1
• 2