c语言实现加、减、乘、除混合的对错判断
时间: 2023-07-10 16:25:31 浏览: 36
以下是一个示例程序,可以实现加、减、乘、除混合的对错判断:
```c
#include <stdio.h>
int main()
{
int a, b, c, d, result;
char op1, op2;
printf("请输入一个四则运算表达式(如 1+2*3/4-5):");
scanf("%d%c%d%c%d%c%d", &a, &op1, &b, &op2, &c, &op2, &d);
if (op1 == '+') {
result = a + b;
} else if (op1 == '-') {
result = a - b;
} else {
printf("表达式错误!\n");
return 0;
}
if (op2 == '*') {
result = result * c;
} else if (op2 == '/') {
if (c == 0) {
printf("表达式错误!\n");
return 0;
}
result = result / c;
} else {
printf("表达式错误!\n");
return 0;
}
if (op2 == '+' || op2 == '-') {
printf("表达式错误!\n");
return 0;
}
if (op2 == '*') {
result = result * d;
} else if (op2 == '/') {
if (d == 0) {
printf("表达式错误!\n");
return 0;
}
result = result / d;
} else {
printf("表达式错误!\n");
return 0;
}
printf("计算结果为:%d\n", result);
return 0;
}
```
该程序首先读入一个四则运算表达式,然后根据运算符的优先级依次计算结果,并判断表达式是否合法。如果表达式合法,则输出计算结果,否则输出错误信息。
相关推荐
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)