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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题