weixin_38517428 2018-02-13 15:04 采纳率: 57.1%
浏览 1177
已采纳

请问下题C语言实现多项式乘法的代码中存在的错误,加法是正确的输出,乘法为何结果总是错误,本人不胜感激

#include
#include
typedef int elemType;

typedef struct Node{
elemType Coefficient;
elemType Exponent;
struct Node *next;
}Node,*LinkedList;

int empty(LinkedList L);
LinkedList LinkedCreatT(int n);
LinkedList add_list(LinkedList a,LinkedList b);
LinkedList multi_list(LinkedList a,LinkedList b);
void show(LinkedList L);

int main()
{
LinkedList m,n,ans;
printf("输入第一个多项式\n");
m=LinkedCreatT(2);
show(m);
printf("\n");
printf("输入第二个多项式\n");
n=LinkedCreatT(2);
show(n);
printf("\n");
printf("他们的积是\n");
ans= multi_list(m,n);
show(ans);
return 0;
}

int empty(LinkedList L)
{
return L->next==NULL;
}

LinkedList LinkedCreatT(int n)
{
Node *L;
L=(Node *)malloc(sizeof(Node));
L->next=NULL;
Node *r;
r=L;
int k=n;
while(n--)
{
printf("请输入第%d项\n",k-n);
elemType coe,exp;
scanf("%d%d",&coe,&exp);
Node *p;
p=(Node *)malloc(sizeof(Node));
p->Coefficient=coe;
p->Exponent=exp;
r->next=p;
r=p;
}
r->next=NULL;
return L->next;
}

LinkedList add_list(LinkedList a,LinkedList b)
{
Node *L;
L=(Node *)malloc(sizeof(Node));
L->next=NULL;
Node *r;
r=L;
while(a&&b)
{
Node *p;
p=(Node *)malloc(sizeof(Node));
if(a->Exponent>b->Exponent)
{
p->Exponent=a->Exponent;
p->Coefficient=a->Coefficient;
a=a->next;
r->next=p;
r=p;
}
else if(a->ExponentExponent)
{
p->Exponent=b->Exponent;
p->Coefficient=b->Coefficient;
b=b->next;
r->next=p;
r=p;
}
else
{
if((a->Coefficient+b->Coefficient)!=0)
{
p->Coefficient=a->Coefficient+b->Coefficient;
p->Exponent=b->Exponent;
r->next=p;
r=p;
}
a=a->next;
b=b->next;
}
}
if(a)
r->next=a;
else
r->next=b;
return L->next;
}

LinkedList multi_list(LinkedList a,LinkedList b)
{
Node *L;
L=(Node *)malloc(sizeof(Node));
L->next=NULL;
while(a)
{
Node *t;
t=(Node *)malloc(sizeof(Node));
t->next=NULL;
Node *r;
r=t;
while(b)
{
Node *p;
p=(Node *)malloc(sizeof(Node));
p->Coefficient=b->Coefficient*a->Coefficient;
p->Exponent=b->Exponent+a->Exponent;
r->next=p;
r=p;
b=b->next;
}
r->next=NULL;

    a=a->next;
    printf("多项式当前是\n");
    show(L->next);
    printf("新链是\n");
    show(t->next);

    L->next=add_list(L->next,t->next);
    printf("多项式变为\n");
    show(L->next);
}
return L->next;

}
void show(LinkedList L)
{
int i=1;
while(L)
{
printf("该多项式是%d 乘x的%d次方\n",L->Coefficient,L->Exponent);
L=L->next;
}
}

本人的问题主要是在例如乘法中(a+b)*(c+d),表示c乘(a+b)的链表能正常返回,但永远只能返回这一项,之后的项数不能返回,通过查看打印结果发现除第一次以外之后的t链表永远为空。谢谢各位大神的赐教

  • 写回答

4条回答

  • rabbit_hog 2018-02-13 16:36
    关注

    乘法的问题出在这里:
    当你拿多项式a的第一项和b相乘后,b指向的位置已经变成了多项式b的末项。你可以这样改动while(a)循环里的内容(保留b原本指向的位置不动,定义一个新指针s遍历b的每一项):
    ...//前面的省略
    while(a)
    {
    Node *t;
    t=(Node *)malloc(sizeof(Node));
    t->next=NULL;
    Node *r;
    r=t;
    Node *s;
    s=b;
    while(s)
    {
    Node *p;
    p=(Node *)malloc(sizeof(Node));
    p->Coefficient=s->Coefficient*a->Coefficient;
    p->Exponent=s->Exponent+a->Exponent;
    r->next=p;
    r=p;
    s=s->next;
    }
    r->next=NULL;
    a=a->next;
    L->next=add_list(L->next,t->next);
    }
    ...//后面省略

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试