📄 p9-2.c
字号:
#include <stdio.h>char *menu[]={ /*命令菜单*/ "a - add new record", "d - delete record", "q - quit", NULL};int getchoice(char *greet,char *choices[]);FILE *input,*output;int main(void){ int choice = 0; input = fopen("/dev/tty","r"); output = fopen("/dev/tty","w"); if(!input||!output) { fprintf(stderr, "unable to open /dev/tty\n"); exit(1);} do { choice = getchoice("Please select a action", menu); printf ("You have chosen: %c\n", choice); } while (choice != 'q') ; exit (0);}int getchoice(char *greet, char *choices[]){ int chosen = 0; int selected; char **option; do { fprintf(output, "Choice: %s\n", greet); option = choices; /*打印选择菜单*/ while(*option) { fprintf (output,"%s\n", *option); option++; } /*读入命令字符,抛弃该行其它字符 */ selected = fgetc(input); while (fgetc(input)!= '\n' ); /* 检查命令字符,正确置chosen=1*/ option = choices; while(*option){ if(selected == *option[0]) { chosen = 1 ; break; } option++; } /*命令不正确,提示重新输入命令*/ if(!chosen) fprintf(output,"Incorrect choice,select agian\n"); } while(!chosen); /*直至得到正确的命令*/ return selected;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -