?? 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 + -