📄 14-10.txt
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -