14-10.txt
来自「c primer 部分习题答案」· 文本 代码 · 共 50 行
TXT
50 行
PE 14-10
/* pe14-10.c */
/* the tricky part is declaring an array of pointers to functions */
#include <stdio.h>
#include <math.h>
double twice(double x);
double half(double x);
double thrice(double x);
void showmenu(void);
#define NUM 4
int main(void)
{
double (*pf[NUM])(double) = {twice, half, thrice, sqrt};
double val;
double ans;
int sel;
printf("Enter a number (negative to quit): ");
while (scanf("%lf", &val) && val >= 0)
{
showmenu();
while (scanf("%d", &sel) && sel >= 0 && sel <= 3)
{
ans = (*pf[sel])(val);
printf("answer = %f\n", ans);
showmenu();
}
printf("Enter next number (negative to quit): ");
}
puts("bye");
return 0;
}
void showmenu(void)
{
puts("Enter one of the following choices:");
puts("0) double the value 1) halve the value");
puts("2) triple the value 3) squareroot the value");
puts("4) next number");
}
double twice(double x) {return 2.0 * x;}
double half(double x) {return x / 2.0;};
double thrice(double x) {return 3.0 * x;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?