gpt4 book ai didi

c - 如何从 C 函数中的三个输入创建多项式函数?

转载 作者:行者123 更新时间:2023-11-30 14:41:48 25 4
gpt4 key购买 nike

Evaluate 函数计算给定 x 值处的多项式并返回结果。 coeff[] 数组包含它应该评估的多项式的系数(其中还有 x-min 和 x-max ,并且 terms 参数告诉它多项式有多少项(coeff[] 中要使用多少个元素) . coeff[] 数组具有图形的最小和最大范围,存储在数组的前两个空间中。

我真的不知道该怎么做。

0.0 6.0
25.00 -47.50 25.17 -5.00 0.33

所以这将是 25 - 47.5x + 25.17x^2 - 5x^3 + 0.33x^4

#include "poly.h"

// Then, anything else we need in the implementation file.
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int readPoly( int capacity, double coeff[] )
{
int i;

for (i = 0; i < capacity; i++) {
for (;;) {
int c;

if ((c = getchar()) == EOF)
return i;
if (!isspace(c)) {
ungetc(c, stdin);
break;
}
if (scanf("%lf", &coeff[i]) != 1) {
fprintf(stderr, "Invalid input");
exit(INVALID_POLYNOMAIL_STATUS);
}
}

return i;
}

double evaluate( double x, int terms, double coeff[] )
{
for(int i = 2; i < terms; i++) {

double equation =

最佳答案

输入函数中的无限循环似乎被设计为在尝试使用 scanf("%lf", &coeff[i]) 读取数字之前跳过空格。 。这是毫无意义的——数字格式都会自动跳过前导空格,包括换行符。只有三个格式说明符不跳过空格;他们是%c , %[…] (扫描集)和 %n .

您可能需要捕获 scanf() 的返回值这样您就可以区分 EOF 和虚假输入:

int rc;
int i = 0;
while (i < capacity && (rc = scanf("%lf", &coeff[i])) == 1)
i++;

if (i == capacity)
{
/* Too many values; remainder ignored */
}
else if (rc == 0)
{
/* Report format error */
}
else if (rc == EOF && i < 4)
{
/* Insufficient valid data */
/* Needed x-min, x-max, coeff[0] (constant term) and coeff[1] (linear term */
}
return i;

evaluate()函数在给定值 x 处计算多项式。最好的进程被称为Horner's Rule或霍纳方法。给定输入格式,其中常数项在 x 项和 x² 项之前读取,您需要从最高系数开始向后计算。

double evaluate(double x, int terms, double coeff[])
{
double r = coeff[terms - 1];
int i = terms - 1;
while (i > 0)
r = (r * x) + coeff[--i];
return r;
}

该函数调用 evaluate()函数需要参数:

x_min   — starting value
x_max — ending value
x_steps — number of values to print (101 in this case)
n_coeff — number of coefficients
coeff — the array of coefficients

它可能会变成:

static void print_values(double x_min, double x_max, int x_steps, int n_coeff, double coeff[n_coeff])
{
const char *pad = "";
for (int i = 0; i < x_steps; i++)
{
double x = (x_max - x_min) * i / (x_steps - 1) + x_min;
double r = evaluate(x, n_coeff, coeff);
printf("%s%3d: P(%5.3f) = %10.6f", pad, i, x, r);
if (i % 3 == 2)
pad = "\n";
else
pad = "; ";
}
putchar('\n');
}

还有main()函数可以变成:

enum { MAX_COEFF = 10 };

int main(void)
{
double coeff[MAX_COEFF];

int n_coeff = readPoly(MAX_COEFF, coeff);
double x_min = coeff[0];
double x_max = coeff[1];
int n_values = 101;
print_values(x_min, x_max, n_values, n_coeff - 2, &coeff[2]);
return 0;
}

并且,对于给定的数据:

0.0 6.0
25.00 -47.50 25.17 -5.00 0.33

生成的输出是:

  0: P(0.000) =  25.000000;   1: P(0.060) =  22.239536;   2: P(0.120) =  19.653876
3: P(0.180) = 17.236694; 4: P(0.240) = 14.981767; 5: P(0.300) = 12.882973
6: P(0.360) = 10.934295; 7: P(0.420) = 9.129817; 8: P(0.480) = 7.463726
9: P(0.540) = 5.930312; 10: P(0.600) = 4.523968; 11: P(0.660) = 3.239189
12: P(0.720) = 2.070572; 13: P(0.780) = 1.012818; 14: P(0.840) = 0.060730
15: P(0.900) = -0.790787; 16: P(0.960) = -1.546724; 17: P(1.020) = -2.211969
18: P(1.080) = -2.791311; 19: P(1.140) = -3.289431; 20: P(1.200) = -3.710912
21: P(1.260) = -4.060232; 22: P(1.320) = -4.341766; 23: P(1.380) = -4.559788
24: P(1.440) = -4.718468; 25: P(1.500) = -4.821875; 26: P(1.560) = -4.873973
27: P(1.620) = -4.878625; 28: P(1.680) = -4.839591; 29: P(1.740) = -4.760529
30: P(1.800) = -4.644992; 31: P(1.860) = -4.496433; 32: P(1.920) = -4.318202
33: P(1.980) = -4.113545; 34: P(2.040) = -3.885606; 35: P(2.100) = -3.637427
36: P(2.160) = -3.371946; 37: P(2.220) = -3.092000; 38: P(2.280) = -2.800322
39: P(2.340) = -2.499544; 40: P(2.400) = -2.192192; 41: P(2.460) = -1.880693
42: P(2.520) = -1.567371; 43: P(2.580) = -1.254444; 44: P(2.640) = -0.944031
45: P(2.700) = -0.638147; 46: P(2.760) = -0.338704; 47: P(2.820) = -0.047512
48: P(2.880) = 0.233722; 49: P(2.940) = 0.503393; 50: P(3.000) = 0.760000
51: P(3.060) = 1.002144; 52: P(3.120) = 1.228527; 53: P(3.180) = 1.437957
54: P(3.240) = 1.629342; 55: P(3.300) = 1.801693; 56: P(3.360) = 1.954124
57: P(3.420) = 2.085853; 58: P(3.480) = 2.196198; 59: P(3.540) = 2.284582
60: P(3.600) = 2.350528; 61: P(3.660) = 2.393665; 62: P(3.720) = 2.413722
63: P(3.780) = 2.410532; 64: P(3.840) = 2.384029; 65: P(3.900) = 2.334253
66: P(3.960) = 2.261343; 67: P(4.020) = 2.165542; 68: P(4.080) = 2.047197
69: P(4.140) = 1.906755; 70: P(4.200) = 1.744768; 71: P(4.260) = 1.561889
72: P(4.320) = 1.358875; 73: P(4.380) = 1.136585; 74: P(4.440) = 0.895980
75: P(4.500) = 0.638125; 76: P(4.560) = 0.364186; 77: P(4.620) = 0.075434
78: P(4.680) = -0.226760; 79: P(4.740) = -0.540922; 80: P(4.800) = -0.865472
81: P(4.860) = -1.198732; 82: P(4.920) = -1.538918; 83: P(4.980) = -1.884145
84: P(5.040) = -2.232425; 85: P(5.100) = -2.581667; 86: P(5.160) = -2.929678
87: P(5.220) = -3.274162; 88: P(5.280) = -3.612720; 89: P(5.340) = -3.942852
90: P(5.400) = -4.261952; 91: P(5.460) = -4.567315; 92: P(5.520) = -4.856131
93: P(5.580) = -5.125488; 94: P(5.640) = -5.372373; 95: P(5.700) = -5.593667
96: P(5.760) = -5.786151; 97: P(5.820) = -5.946503; 98: P(5.880) = -6.071297
99: P(5.940) = -6.157006; 100: P(6.000) = -6.200000

代码不会打印多项式进行验证;它应该。显然,当x时输出是 0 应该是 25确实如此,这让人感到安心。在极限情况下,0.33x^4 项应占主导地位,因此随着 x 向无穷大增加,结果趋于无穷大。给定多项式的最后 0 位于 x值略小于 7。

关于c - 如何从 C 函数中的三个输入创建多项式函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54737814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com