简介
在本实验中,你将学习如何在 C 编程中计算多项式表达式的值。本实验涵盖以下步骤:从用户输入中读取多项式系数和变量 x,并使用霍纳法则高效地计算多项式表达式的值。完成本实验后,你将对在 C 语言中处理代数表达式以及实现有效的多项式求值算法有更深入的理解。
读取系数和变量 x
在这一步中,你将学习如何在 C 程序中从用户输入读取多项式系数和变量 x。这是实现多项式求值算法的第一步。
首先,为多项式求值程序创建一个新的 C 文件:
cd ~/project
nano polynomial_eval.c
现在,添加以下代码来读取系数和变量 x:
#include <stdio.h>
#define MAX_DEGREE 10
int main() {
int degree;
double coefficients[MAX_DEGREE + 1];
double x;
// 读取多项式的次数
printf("Enter the degree of the polynomial (0-10): ");
scanf("%d", °ree);
// 读取系数
printf("Enter coefficients from highest degree to constant term:\n");
for (int i = degree; i >= 0; i--) {
printf("Coefficient for x^%d: ", i);
scanf("%lf", &coefficients[i]);
}
// 读取 x 的值
printf("Enter the value of x: ");
scanf("%lf", &x);
return 0;
}
示例输出:
Enter the degree of the polynomial (0-10): 3
Enter coefficients from highest degree to constant term:
Coefficient for x^3: 2
Coefficient for x^2: -3
Coefficient for x^1: 0
Coefficient for x^0: 5
Enter the value of x: 2
下面我们来分析一下这段代码:
- 我们定义了多项式的最大次数,以防止缓冲区溢出
degree存储多项式的次数coefficients数组存储从最高次项到常数项的系数x存储我们将用于计算多项式的值- 我们使用
scanf()读取用户输入的次数、系数和 x 的值
编译并运行程序以测试输入:
gcc polynomial_eval.c -o polynomial_eval
./polynomial_eval
使用霍纳法则进行求值
在这一步中,你将实现霍纳法则,以便高效地计算多项式表达式的值。霍纳法则减少了计算多项式值所需的乘法次数。
打开之前的 C 文件,并修改它以包含霍纳法则求值:
cd ~/project
nano polynomial_eval.c
用霍纳法则的实现更新代码:
#include <stdio.h>
#define MAX_DEGREE 10
double hornerMethod(int degree, double coefficients[], double x) {
double result = coefficients[degree];
for (int i = degree - 1; i >= 0; i--) {
result = result * x + coefficients[i];
}
return result;
}
int main() {
int degree;
double coefficients[MAX_DEGREE + 1];
double x, result;
// 之前的输入代码保持不变
printf("Enter the degree of the polynomial (0-10): ");
scanf("%d", °ree);
printf("Enter coefficients from highest degree to constant term:\n");
for (int i = degree; i >= 0; i--) {
printf("Coefficient for x^%d: ", i);
scanf("%lf", &coefficients[i]);
}
printf("Enter the value of x: ");
scanf("%lf", &x);
// 使用霍纳法则计算多项式
result = hornerMethod(degree, coefficients, x);
printf("Polynomial value at x = %.2f is: %.2f\n", x, result);
return 0;
}
编译并运行更新后的程序:
gcc polynomial_eval.c -o polynomial_eval
./polynomial_eval
示例输出:
Enter the degree of the polynomial (0-10): 3
Enter coefficients from highest degree to constant term:
Coefficient for x^3: 2
Coefficient for x^2: -3
Coefficient for x^1: 0
Coefficient for x^0: 5
Enter the value of x: 2
Polynomial value at x = 2.00 is: 11.00
下面我们来分析一下霍纳法则:
- 函数
hornerMethod()将次数、系数和 x 作为参数 - 它从最高次项系数开始
- 迭代地将当前结果乘以 x 并加上下一个系数
- 将计算复杂度从 O(n²) 降低到 O(n)
该方法能高效地计算多项式,比如在 x = 2 时计算 2x³ - 3x² + 0x + 5 的值。
打印结果
在这最后一步中,你将通过添加格式化输出和错误处理来增强多项式求值程序,以改善用户体验。
打开之前的 C 文件,并用改进后的结果打印功能对其进行更新:
cd ~/project
nano polynomial_eval.c
用格式化的结果打印和输入验证功能更新代码:
#include <stdio.h>
#define MAX_DEGREE 10
double hornerMethod(int degree, double coefficients[], double x) {
double result = coefficients[degree];
for (int i = degree - 1; i >= 0; i--) {
result = result * x + coefficients[i];
}
return result;
}
void printPolynomial(int degree, double coefficients[]) {
printf("Polynomial: ");
for (int i = degree; i >= 0; i--) {
if (coefficients[i]!= 0) {
if (i == degree) {
printf("%.2fx^%d ", coefficients[i], i);
} else if (i > 1) {
printf("%+.2fx^%d ", coefficients[i], i);
} else if (i == 1) {
printf("%+.2fx ", coefficients[i]);
} else {
printf("%+.2f", coefficients[i]);
}
}
}
printf("\n");
}
int main() {
int degree;
double coefficients[MAX_DEGREE + 1];
double x, result;
// 输入验证
do {
printf("Enter the degree of the polynomial (0-10): ");
scanf("%d", °ree);
} while (degree < 0 || degree > MAX_DEGREE);
printf("Enter coefficients from highest degree to constant term:\n");
for (int i = degree; i >= 0; i--) {
printf("Coefficient for x^%d: ", i);
scanf("%lf", &coefficients[i]);
}
printf("Enter the value of x: ");
scanf("%lf", &x);
// 打印多项式细节
printPolynomial(degree, coefficients);
// 使用霍纳法则计算多项式
result = hornerMethod(degree, coefficients, x);
// 格式化结果输出
printf("Polynomial Evaluation:\n");
printf("P(x) = f(%.2f) = %.2f\n", x, result);
return 0;
}
编译并运行更新后的程序:
gcc polynomial_eval.c -o polynomial_eval
./polynomial_eval
示例输出:
Enter the degree of the polynomial (0-10): 3
Enter coefficients from highest degree to constant term:
Coefficient for x^3: 2
Coefficient for x^2: -3
Coefficient for x^1: 0
Coefficient for x^0: 5
Enter the value of x: 2
Polynomial: 2.00x^3 -3.00x^2 +5.00
Polynomial Evaluation:
P(x) = f(2.00) = 11.00
主要改进:
- 添加了
printPolynomial()函数来显示多项式 - 对多项式次数实现了输入验证
- 通过格式化输出增强了结果打印
- 同时显示多项式及其求值结果
总结
在本实验中,你首先学习了如何在 C 程序中从用户输入读取多项式系数和变量 x。这包括为多项式定义最大次数、创建一个数组来存储系数,以及使用scanf()读取用户输入。然后,你实现了霍纳法则以高效地计算多项式表达式的值,该法则减少了计算多项式值所需的乘法次数。通过遵循这些步骤,你可以在 C 语言中实现一个强大的多项式求值算法。



