创作灵感: 多项式乘法是数学中的基本操作,也常常在计算机科学和工程中得到应用。本文将介绍一个用C语言编写的程序,演示如何实现多项式的乘法运算。通过这个程序,我们可以学习如何使用链表数据结构来表示和操作多项式,以及如何进行多项式的乘法计算。
技术笔记要点: 在这个C语言程序中,我们首先定义了一个多项式的每一项的结构体Node,包括系数(coefficient)、指数(exponent)和指向下一个项的指针(next)。
主要函数和步骤如下:
createTerm
函数用于创建一个新的多项式项,分配内存并初始化其成员。addTerm
函数用于向多项式中添加一个项,将新项插入到链表中。combineLikeTerms
函数用于合并同类项,即将具有相同指数的项的系数相加,从而简化多项式。printPolynomial
函数用于打印多项式,格式化输出多项式的表达式。freePolynomial
函数用于释放多项式链表的内存。multiplyPolynomials
函数用于计算两个多项式的乘积,将结果保存在一个新的多项式链表中。
在main
函数中,我们首先定义了两个多项式poly1和poly2,并从标准输入中读取用户输入的多项式系数和指数。然后,我们打印输入的两个多项式,并调用multiplyPolynomials
函数计算它们的乘积。最后,我们打印乘积的结果,并释放内存以避免内存泄漏。
修正版主要解决了输入读取问题,确保了程序能够正确读取多项式的系数和指数,以及正确地处理多项式中的零项。
#include <stdio.h>
#include <stdlib.h>
// 定义多项式的每一项的结构体
typedef struct Node {
double coefficient;
int exponent;
struct Node *next;
} Node;
// 创建一个新的多项式项
Node *createTerm(double coefficient, int exponent) {
Node *term = (Node *)malloc(sizeof(Node));
term->coefficient = coefficient;
term->exponent = exponent;
term->next = NULL;
return term;
}
// 向多项式中添加一个项
void addTerm(Node **poly, double coefficient, int exponent) {
Node *newTerm = createTerm(coefficient, exponent);
if (*poly == NULL) {
*poly = newTerm;
} else {
Node *current = *poly;
while (current->next != NULL) {
current = current->next;
}
current->next = newTerm;
}
}
// 合并同类项
void combineLikeTerms(Node *poly) {
if (poly == NULL) {
return;
}
Node *current = poly;
while (current != NULL) {
Node *runner = current;
while (runner->next != NULL) {
if (runner->next->exponent == current->exponent) {
current->coefficient += runner->next->coefficient;
Node *temp = runner->next;
runner->next = runner->next->next;
free(temp);
} else {
runner = runner->next;
}
}
current = current->next;
}
}
// 打印多项式
void printPolynomial(const char *name, Node *poly) {
printf("%s=", name);
if (poly == NULL) {
printf("0.0\n");
return;
}
int firstTerm = 1;
while (poly != NULL) {
if (firstTerm) {
firstTerm = 0;
} else {
printf(poly->coefficient >= 0 ? "+" : "");
}
if (poly->exponent == 0) {
printf("%.1f", poly->coefficient);
} else if (poly->exponent == 1) {
if (poly->coefficient == 1) {
printf("X");
} else if (poly->coefficient == -1) {
printf("-X");
} else {
printf("%.1fX", poly->coefficient);
}
} else {
if (poly->coefficient == 1) {
printf("X^%d", poly->exponent);
} else if (poly->coefficient == -1) {
printf("-X^%d", poly->exponent);
} else {
printf("%.1fX^%d", poly->coefficient, poly->exponent);
}
}
poly = poly->next;
}
printf("\n");
}
// 释放多项式链表
void freePolynomial(Node *poly) {
while (poly != NULL) {
Node *temp = poly;
poly = poly->next;
free(temp);
}
}
// 计算两个多项式的乘积
Node *multiplyPolynomials(Node *poly1, Node *poly2) {
Node *result = NULL;
for (Node *term1 = poly1; term1 != NULL; term1 = term1->next) {
for (Node *term2 = poly2; term2 != NULL; term2 = term2->next) {
double coefficient = term1->coefficient * term2->coefficient;
int exponent = term1->exponent + term2->exponent;
addTerm(&result, coefficient, exponent);
}
}
// 合并同类项
combineLikeTerms(result);
return result;
}
int main() {
Node *poly1 = NULL;
Node *poly2 = NULL;
// 读取第一个多项式
double coefficient;
int exponent;
while (1) {
scanf("%lf,%d", &coefficient, &exponent);
if (coefficient == 0.0 && exponent == 0) {
break;
}
addTerm(&poly1, coefficient, exponent);
}
// 读取第二个多项式
while (1) {
scanf("%lf,%d", &coefficient, &exponent);
if (coefficient == 0.0 && exponent == 0) {
break;
}
addTerm(&poly2, coefficient, exponent);
}
// 打印两个多项式
printPolynomial("f(X)", poly1);
printPolynomial("g(X)", poly2);
// 计算并打印两个多项式的乘积
Node *result = multiplyPolynomials(poly1, poly2);
printPolynomial("f(X)*g(X)", result);
// 释放内存
freePolynomial(poly1);
freePolynomial(poly2);
freePolynomial(result);
return 0;
}