1 /* 2 xuejineng2016@163.com 3 2020年5月6日 4 */ 5 #include<stdio.h> 6 int main(void) 7 { 8 int num, ret, error = 0; 9 char ch; 10 11 scanf_s("%d", &num); 12 //至少有一个操作数的情况下,结果就是这个操作数 13 ret = num; 14 15 //当运算符为等于号时,结束循环 16 while ((ch = getchar()) != '=') 17 { 18 scanf_s("%d", &num); 19 if (ch == '+') 20 { 21 ret = ret + num; 22 } 23 else if (ch == '-') 24 { 25 ret = ret - num; 26 } 27 else if (ch == '*') 28 { 29 ret = ret * num; 30 } 31 else if (ch == '/') 32 { 33 if (num == 0) 34 { 35 printf("ERROR "); 36 error = 1; //错误标志 37 break; 38 } 39 else 40 { 41 ret = ret / num; 42 } 43 } 44 else 45 { 46 47 printf("ERROR "); 48 error = 1; 49 break; 50 } 51 } 52 53 if (error == 0) 54 { 55 printf("%d ", ret); 56 } 57 58 return 0; 59 }