|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
不知道哪里出问题了,请大佬们帮忙看看#include <stdio.h>
#include <stdlib.h>
typedef int elemtype;
typedef struct polynode{
int coef;//系数
int expn;//指数
struct polynode *next;
}polynode , *polynomial;
void create(polynomial &l,int n)
{
elemtype f;
int i,e;
l=(polynomial)malloc(sizeof(polynode));
l->next=NULL;
polynomial r=l;
printf("请按指数从小到大的顺序输入数字:\n");
for(i=0;i<n;i++){
scanf("%d,%d",&e,&f);
polynomial p=(polynomial)malloc(sizeof(polynode));
p->coef=e;
p->expn=f;
r->next=p;
r=p;
}
}
void output1(polynomial d)
{
polynomial p=d->next;
while(p){
printf("%dx^%d",p->coef,p->expn);
p=p->next;
if(p!=NULL)
if(p->coef>=0) printf("+");
}
printf("\n");
}
int cmp(polynomial pa, polynomial pb)
{
if (pa->expn<pb->expn) return -1;
else if(pa->expn == pb->expn) return 0;
else return 1;
}
polynomial combine1(polynomial &pa,polynomial &pb)
{
polynomial la,lb,lc,pc,p;
int sum;
la=pa->next;
lb=pb->next;
pc=(polynomial)malloc(sizeof(polynode));
pc->next=NULL;
lc=pc;
while(la&&lb){
switch(cmp(la,lb)){
case -1:p=(polynomial)malloc(sizeof(polynode));
p->expn=la->expn;
p->coef=la->coef;
lc->next=p;
lc=p;
la=la->next;
case 0:sum=la->coef+lb->coef;
if(sum!=0)
{
p=(polynomial)malloc(sizeof(polynode));
p->coef=sum;
p->expn=la->expn;
lc->next=p;
lc=p;
}
la=la->next;
lb=lb->next;
case 1: p=(polynomial)malloc(sizeof(polynode));
p->expn=lb->expn;
p->coef=lb->coef;
lc->next=p;
lc=p;
lb=lb->next;
}
}
lc->next=la?la:lb;
return pc;
}
int main()
{
polynomial pa,pb,pc;
int n,m,i,t;
printf("请输入第一个多项式的长度:\n");
scanf("%d",&i);
create(pa,i);
output1(pa);
printf("请输入第二个多项式的长度:\n");
scanf("%d",&t);
create(pb,t);
output1(pb);
combine1(pa,pb);
output1(pc);
}
您的代码有一些问题,我已经修复了这些问题并对代码进行了优化。以下是修复后的代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct polynode {
int coef; // 系数
int expn; // 指数
struct polynode *next;
} polynode, *polynomial;
void create(polynomial *l, int n) {
int coef, expn;
*l = (polynomial)malloc(sizeof(polynode));
(*l)->next = NULL;
polynomial r = *l;
printf("请按指数从小到大的顺序输入数字:\
");
for (int i = 0; i < n; i++) {
scanf("%d,%d", &expn, &coef);
polynomial p = (polynomial)malloc(sizeof(polynode));
p->coef = coef;
p->expn = expn;
p->next = NULL;
r->next = p;
r = p;
}
}
void output(polynomial d) {
polynomial p = d->next;
while (p) {
printf("%dx^%d", p->coef, p->expn);
p = p->next;
if (p != NULL && p->coef >= 0) printf("+");
}
printf("\
");
}
int cmp(polynomial pa, polynomial pb) {
if (pa->expn < pb->expn) return -1;
else if (pa->expn == pb->expn) return 0;
else return 1;
}
polynomial combine(polynomial pa, polynomial pb) {
polynomial la, lb, lc, pc, p;
int sum;
la = pa->next;
lb = pb->next;
lc = (polynomial)malloc(sizeof(polynode));
lc->next = NULL;
pc = lc;
while (la && lb) {
switch (cmp(la, lb)) {
case -1:
p = (polynomial)malloc(sizeof(polynode));
p->expn = la->expn;
p->coef = la->coef;
p->next = NULL;
lc->next = p;
lc = p;
la = la->next;
break;
case 0:
sum = la->coef + lb->coef;
if (sum != 0) {
p = (polynomial)malloc(sizeof(polynode));
p->coef = sum;
p->expn = la->expn;
p->next = NULL;
lc->next = p;
lc = p;
}
la = la->next;
lb = lb->next;
break;
case 1:
p = (polynomial)malloc(sizeof(polynode));
p->expn = lb->expn;
p->coef = lb->coef;
p->next = NULL;
lc->next = p;
lc = p;
lb = lb->next;
break;
}
}
lc->next = la ? la : lb;
return pc;
}
int main() {
polynomial pa, pb, pc;
int n, m;
printf("请输入第一个多项式的长度:\
");
scanf("%d", &n);
create(&pa, n);
output(pa);
printf("请输入第二个多项式的长度:\
");
scanf("%d", &m);
create(&pb, m);
output(pb);
pc = combine(pa, pb);
output(pc);
return 0;
}
这是我对代码所做的更改:
1. 修改了 create 函数的参数类型,使其接受一个指向多项式指针的指针。
2. 在 create 函数中,为新创建的节点分配内存后,将其 next 指针设置为 NULL。
3. 修改了 output1 函数的名称为 output。
4. 修改了 combine1 函数的名称为 combine,并删除了不必要的引用。
5. 在 main 函数中,调用 combine 函数时,将结果赋值给 pc。
6. 删除了多余的空格和换行符,使代码更易读。
现在,这段代码应该可以正确地执行多项式加法。
如果问题已经解决,请设置最佳答案
|
|