多项式的加法问题
不知道哪里出问题了,请大佬们帮忙看看#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) return0;
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);
} 这段代码的问题在于combine1函数没有正确返回结果。在函数结尾处,应该将lc->next置为NULL,然后返回pc。同时,在main函数中应该将combine1的返回值赋给pc。修改后的代码如下:
#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) return0;
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;
break;
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;
break;
case 1:
p=(polynomial)malloc(sizeof(polynode));
p->expn=lb->expn;
p->coef=lb->coef;
lc->next=p;
lc=p;
lb=lb->next;
break;
}
}
lc->next=la?la:lb;
lc=pc->next;
pc->next=NULL;
free(pc);
return lc;
}
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);
pc = combine1(pa,pb);
output1(pc);
return 0;
}
问题:多项式的加法问题 数据结构与算法
在您提供的代码中,我发现了一些问题。以下是我对代码的修改和解释:
1. create 函数中,您需要将新节点的 next 指针设置为 NULL。
2. main 函数中,combine1 函数的返回值没有被赋值给 pc。
3. combine1 函数中,switch 语句缺少 break 语句。
修改后的代码如下:
#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(\"请按指数从小到大的顺序输入数字:\
\");
for (i = 0; i < n; i++) {
scanf(\"%d,%d\", &e, &f);
polynomial p = (polynomial)malloc(sizeof(polynode));
p->coef = e;
p->expn = f;
p->next = NULL; // 设置新节点的 next 指针为 NULL
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(\"\
\");
}
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;
break;
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;
break;
case 1:
p = (polynomial)malloc(sizeof(polynode));
p->expn = lb->expn;
p->coef = lb->coef;
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, i, t;
printf(\"请输入第一个多项式的长度:\
\");
scanf(\"%d\", &i);
create(pa, i);
output1(pa);
printf(\"请输入第二个多项式的长度:\
\");
scanf(\"%d\", &t);
create(pb, t);
output1(pb);
pc = combine1(pa, pb); // 将 combine1 函数的返回值赋值给 pc
output1(pc);
}
现在,这段代码应该可以正确地实现多项式的加法。如果您在运行修改后的代码时遇到任何问题,请随时告诉我。
球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:}
本段回答来自Chat GPT-4,如有错误,多多包涵! 您的代码有一些问题,我已经修复了这些问题并对代码进行了优化。以下是修复后的代码:
#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. 删除了多余的空格和换行符,使代码更易读。
现在,这段代码应该可以正确地执行多项式加法。
如果问题已经解决,请设置最佳答案
页:
[1]