我希望这段代码说得通...我创建了两个多项式并尝试将它们相乘。问题是我不知道该怎么做才能正确地将它们相乘。这个程序相乘了多项式,将结果存储到另一个多项式中,但它没有将幂相同的系数相加。
为了实现这个功能,我应该怎么做?此外,在这个程序中如何使用
free()
?
#include <stdio.h>
#include <stdlib.h>
typedef struct poly {
int coef;
int exp;
struct poly *next;
} poly;
int main(void) {
poly * po1, *head, *po2, *head2, *po3, *head3 = NULL;
int sel, c = 1;
head = NULL;
printf(
"\nInsert elements for the first polynomial from the biggest to the smallest power of x. (Enter a power of zero (0) to stop)\n");
while (1) {
po1 = (poly *) malloc(sizeof(poly));
printf("Give number: ");
scanf("%d", &po1->coef);
printf("Give power of x: ");
scanf("%d", &po1->exp);
po1->next = head;
head = po1;
if (po1->exp == 0) break;
}
head2 = NULL;
printf(
"\nInsert elements for the second polynomial from the biggest to the smallest power of x. (Enter a power of zero (0) to stop)\n");
while (1) {
po2 = (poly *) malloc(sizeof(poly));
printf("Give number: ");
scanf("%d", &po2->coef);
printf("Give power of x: ");
scanf("%d", &po2->exp);
po2->next = head2;
head2 = po2;
if (po2->exp == 0) break;
}
po1 = head;
po2 = head2;
printf("Multiplying********\n");
po1 = head;
po2 = head2;
while (po1 || po2) {
po2 = head2;
while (po1 && po2) {
po3 = (poly *) malloc(sizeof(poly));
po3->coef = (po1->coef) * (po2->coef);
po3->exp = (po1->exp) + (po2->exp);
po3->next = head3;
head3 = po3;
po2 = po2->next;
}
po1 = po1->next;
}
while (po3) {
printf("%+d(x^%d)", po3->coef, po3->exp);
po3 = po3->next;
}
printf("\n");
}
}
3x² + 2x - 1
可以表示为double c[3] = {-1.0, 2.0, 3.0}
),其中数组位置表示对应的指数,那么计算会更加便捷。您的乘积可以在一个嵌套循环中计算,如r[i+j] += x[i] * y[j]
。 - John Bode