【3.10】 编程实现,利用switch语句模拟简单的计算器,计算任意两个整数的加、减、乘、除运算。如输入5+2,输出5+2=7;如输入5/2,输出2.50。
时间: 2023-04-09 09:04:15 浏览: 139
好的,我可以回答这个问题。以下是利用switch语句模拟简单计算器的代码:
```
#include <stdio.h>
int main() {
char op;
int num1, num2;
float result;
printf("请输入两个整数和运算符,格式为:num1 运算符 num2\n");
scanf("%d %c %d", &num1, &op, &num2);
switch (op) {
case '+':
result = num1 + num2;
printf("%d %c %d = %.2f", num1, op, num2, result);
break;
case '-':
result = num1 - num2;
printf("%d %c %d = %.2f", num1, op, num2, result);
break;
case '*':
result = num1 * num2;
printf("%d %c %d = %.2f", num1, op, num2, result);
break;
case '/':
if (num2 == 0) {
printf("除数不能为0");
} else {
result = (float)num1 / num2;
printf("%d %c %d = %.2f", num1, op, num2, result);
}
break;
default:
printf("输入的运算符不正确");
break;
}
return 0;
}
```
如果输入5 2,则输出:5 + 2 = 7.00
如果输入5 / 2,则输出:5 / 2 = 2.50
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)