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

时间:2019-02-17 21:22:55

标签: c

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 = 

1 个答案:

答案 0 :(得分:1)

您尝试使用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。