c语言函数做简单计算器
时间: 2024-02-15 08:59:13 浏览: 34
以下是一个简单的C语言函数实现计算器的例子:
```c
#include <stdio.h>
// 加法函数
int add(int x, int y) {
return x + y;
}
// 减法函数
int sub(int x, int y) {
return x - y;
}
// 乘法函数
int mul(int x, int y) {
return x * y;
}
// 除法函数
int div(int x, int y) {
return x / y;
}
int main() {
int x, y;
char op;
printf("请输入两个数字和运算符,以空格分隔:");
scanf("%d %d %c", &x, &y, &op);
switch (op) {
case '+':
printf("%d + %d = %d\n", x, y, add(x, y));
break;
case '-':
printf("%d - %d = %d\n", x, y, sub(x, y));
break;
case '*':
printf("%d * %d = %d\n", x, y, mul(x, y));
break;
case '/':
printf("%d / %d = %d\n", x, y, div(x, y));
break;
default:
printf("不支持的运算符\n");
break;
}
return 0;
}
```
用户输入两个数字和运算符,程序根据运算符调用相应的函数进行计算并输出结果。
相关推荐
![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)