c语言用switch语句实现菜单程序

2024-11-24 05:07:10
推荐回答(1个)
回答1:

#include

int count(a,b,sign){

int num;

switch (sign) {

case '+':

num=(a+b);

break;

case '-':

num=(a-b);

break;

case '*':

num=(a*b);

break;

case '/':

num=a/b;

break;

default:

break;

}

return num;

}

int main(void){

char sign;

int ch,a,b,num;

// double num;

printf("1. a,b,add //求两数之和\n");

printf("2. a,b,sub //求两数之差\n");

printf("3. a,b,pro //求两数之积\n");

printf("4. a,b,div //求两数之商\n");

printf("5. exit\n\n");

while (1) {

printf("Please select a menu:");

scanf("%d",&ch);

switch (ch) {

case 1:

sign='+';

break;

case 2:

sign='-';

break;

case 3:

sign='*';

break;

case 4:

sign='/';

break;

default:

return 0;

}

printf("please input 2 number:\n");

scanf("%d%d",&a,&b);

num=count(a,b,sign);

printf("num=%d\n",num);

}

return 0;

}
//没有考虑除法浮点计算;没有考虑错误输入;