c语言 编写可以支持加减乘除取模的计算器
时间: 2023-10-08 15:03:48 浏览: 95
好的,我可以为您提供一个简单的c语言计算器示例程序,支持加减乘除取模运算。请注意,此示例程序仅支持整数运算,不支持浮点数运算。
```c
#include<stdio.h>
int main()
{
char operator;
int num1, num2, result;
printf("请输入运算符(+ - * / %%):\n");
scanf("%c", &operator);
printf("请输入第一个数字:\n");
scanf("%d", &num1);
printf("请输入第二个数字:\n");
scanf("%d", &num2);
switch(operator)
{
case '+':
result = num1 + num2;
printf("%d + %d = %d", num1, num2, result);
break;
case '-':
result = num1 - num2;
printf("%d - %d = %d", num1, num2, result);
break;
case '*':
result = num1 * num2;
printf("%d * %d = %d", num1, num2, result);
break;
case '/':
if(num2 == 0)
{
printf("除数不能为0");
}
else
{
result = num1 / num2;
printf("%d / %d = %d", num1, num2, result);
}
break;
case '%':
result = num1 % num2;
printf("%d %% %d = %d", num1, num2, result);
break;
default:
printf("无效的运算符");
}
return 0;
}
```
您可以将此代码复制到您的c语言开发环境中进行编译和测试。希望对您有所帮助!
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)