ignore__ 2020-02-25 09:43 采纳率: 100%

# c语言,一元多项式相乘,相乘结果莫名少第一项

4 3 4 -5 2 6 1 -2 0

3 5 20 -7 4 3 1

``````#include<stdio.h>
#include<stdlib.h>
typedef struct Node *Poly;
struct Node{
int coef;
int expon;
};
void Attach(int c,int e,Poly *R);
Poly Sum(Poly p1,Poly p2);
Poly Mult(Poly p1,Poly p2);
void print(Poly p);
void freep(Poly p);
int main(void)
{
Poly p1,p2;
print(Mult(p1,p2));
print(Sum(p1,p2));
return 0;
}
{
int N,c,e;
Poly p,Rear,t;
scanf("%d",&N);
p = (Poly)malloc(sizeof(struct Node));
Rear = p;
while(N--)
{
scanf("%d %d",&c,&e);
Attach(c,e,&Rear);
}
t = p;
free(t);
return p;
}
void Attach(int c,int e,Poly *R)
{
Poly p;
p = (Poly)malloc(sizeof(struct Node));
p->coef = c;
p->expon = e;
*R = p;
}
Poly Sum(Poly p1,Poly p2)
{
t1 = p1;
t2 = p2;
while(t1&&t2)
{
if(t1->expon == t2->expon)
{
if(t1->coef + t2->coef)
{
Attach(t1->coef+t2->coef,t1->expon,&Rear);
}
}
else if(t1->expon > t2->expon)
{
Attach(t1->coef,t1->expon,&Rear);
}
else
{
Attach(t2->coef,t2->expon,&Rear);
}

}
while(t1)
{
Attach(t1->coef,t1->expon,&Rear);
}
while(t2)
{
Attach(t2->coef,t2->expon,&Rear);
}
free(t);
}
Poly Mult(Poly p1,Poly p2)
{
int c,e;
if(!p1||!p2)
{
return NULL;
}
t1 = p1;
t2 = p1;
while(t2)
{
Attach(t1->coef*t2->coef,t1->expon+t2->expon,&Rear);
}
while(t1)
{
t2 = p2;
while(t2)
{
c=t1->coef*t2->coef;
e=t1->expon+t2->expon;
{
}
{
{
}
else
{
free(t);
}
}
else
{
Attach(c,e,&Rear);
}
}
}
free(t);
}
void print(Poly p)
{
int flag = 0;
if(!p)
{
printf("0 0\n");
return ;
}
while(p)
{
if(!flag)
{
flag = 1;
}
else
{
printf(" ");
}
printf("%d %d",p->coef,p->expon);
}
printf("\n");
}

``````
• 写回答

#### 1条回答默认 最新

• dabocaiqq 2020-02-25 10:59
关注
本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 悬赏问题

• ¥15 关于用abap来解决动态规划的问题，但是要求输出索引值，这个是难点
• ¥15 在ISIS中什么是IP从地址
• ¥15 压测时，并发量过高时，响应时间出现尖刺
• ¥15 关于vmprotect3.8.4虚拟文件一项
• ¥15 在不用IT调试的情况下怎样能连外网
• ¥20 C#调用虚拟键盘TabTip.exe
• ¥15 Qt4代码实现下面的界面
• ¥15 CCS离散化传递函数与仿真不一致
• ¥15 prism提示我reinstall prism 如何解决
• ¥15 asp.core 权限控制怎么做，需要控制到每个方法