-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path6-5.c
56 lines (44 loc) · 1.18 KB
/
6-5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
int main() {
char op;
int operand1, operand2, res;
// 输入第一个操作数
scanf("%d", &operand1);
// 结果暂存为operand1, 例如"1="这种表达式,没有输入第二个操作数
res = operand1;
// 输入运算符
op = getchar();
while (op != '=') {
// 输入第二个操作数
scanf("%d", &operand2);
// 判断运算符
switch (op) {
case '+':
res = operand1 + operand2;
break; // break别忘了
case '-':
res = operand1 - operand2;
break;
case '*':
res = operand1 * operand2;
break;
case '/':
if (operand2 == 0) {
printf("ERROR");
return 0;
} else
res = operand1 / operand2;
break;
default:
printf("ERROR");
return 0;
}
// 更新操作数1
operand1 = res;
// 更新运算符
op = getchar();
}
// 输出运算结果
printf("%d\n", res);
return 0;
}