打印菜单,输入对应的序号后执行打印提示后按任意键返回菜单,继续选择。输入0后退出系统。
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <termios.h> 4 #include <unistd.h> 5 int getch(); 6 void menu(); 7 void result(); 8 int main() 9 { 10 int a; 11 menu(); 12 scanf("%d",&a); 13 while(a!=0) 14 { 15 switch (a) 16 { 17 case 1:result();break; 18 case 2:result();break; 19 case 3:result();break; 20 case 4:result();break; 21 default:printf("Wrong\n");getch();getch();break; 22 } 23 menu(); 24 scanf("%d",&a); 25 } 26 return 0; 27 } 28 void menu() 29 { 30 printf(" 选择菜单\n\n"); 31 printf("1.求两个数的和\n"); 32 printf("2.求两个数的差\n"); 33 printf("3.求两个数的商和余数\n"); 34 printf("4.求两个数的倒数之和\n\n"); 35 printf("输入0退出系统\n"); 36 } 37 void result() 38 {printf("请输入两个数:\n");getch();getch();} 39 int getch() 40 { 41 struct termios tm, tm_old; 42 int fd = STDIN_FILENO,c; 43 if (tcgetattr(fd, &tm) < 0) 44 { 45 return -1; 46 } 47 tm_old = tm; 48 cfmakeraw(&tm); 49 if (tcsetattr(fd, TCSANOW, &tm) < 0) 50 { 51 return -1; 52 } 53 c = fgetc(stdin); 54 if (tcsetattr(fd,TCSANOW,&tm_old) < 0) 55 { 56 return -1; 57 } 58 return c; 59 }