循环5.1 while() Linux 输入任意键继续

打印菜单,输入对应的序号后执行打印提示后按任意键返回菜单,继续选择。输入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 }

 

转载于:https://www.cnblogs.com/Bird-Xu/p/6501586.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值