【问题描述】
已知契比雪夫多项式的定义如下:
x (n=1)
2*x*x-1 (n=2)
4*x*x*x-3*x (n=3)
8*x*x*x*x-8*x*x+1 (n=4)
编写程序,通过键盘输入整数n和浮点数x,并计算多项式的值。
【参考代码】
#include <stdio.h>
float f1(float x) { return x; }
float f2(float x) { return 2 * x * x - 1; }
float f3(float x) { return 2 * x * x * x - 3 * x; }
float f4(float x) { return 8 * x * x * x * x - 8 * x * x + 1; }
int main()
{
float (*p)(float)=f1, x, result=1;
int n;
printf("请输入n和x:");
scanf("%d %f", &n, &x);
switch (n)
{
case 1:p = f1; break;
case 2:p = f2; break;
case 3:p = f3; break;
case 4:p = f4; break;
default: printf("n错误!");
}
result = (*p)(x);
printf("rusult=%.2f", result);
return 0;
}