C语言实现三角函数表
人气:0下面是用函数指针实现的三角函数表,其中cos函数、sin函数和tan函数都已经在<math.h>中定义,函数tabulate中使用的函数ceil也属于<math.h>,当给定double型实参x时,函数ceil会返回大于或等于x的最小整数。当给定三角函数的起始值,终止值和步长以后,程序会打印出相对应的三角函数值。
#include <math.h> #include <stdio.h> // 传递函数指针f时,tabulate函数显示函数f的值 void tabulate(double (*f)(double), double first, double last, double incr); int main(void) { double initial, final, increment; printf("Enter initial value: "); scanf("%lf", &initial); printf("Enter final value: "); scanf("%lf", &final); printf("Enter increment: "); scanf("%lf", &increment); printf("\n x cos(x)" "\n ------- -------\n"); tabulate(cos, initial, final, increment); printf("\n x sin(x)" "\n ------- -------\n"); tabulate(sin, initial, final, increment); printf("\n x tan(x)" "\n ------- -------\n"); tabulate(tan, initial, final, increment); return 0; } void tabulate(double (*f)(double), double first, double last, double incr) { double x; int i, num_intervals; num_intervals = ceil((last - first) / incr); for (i = 0; i <= num_intervals; i++) { x = first + i * incr; printf("%10.5f %10.5f\n", x, (*f)(x)); } }
参考资料:
《C语言程序设计现代方法》
您可能感兴趣的文章:
加载全部内容