假定输入都是整数且不考虑运算符优先级
#include<stdio.h>
#include<stdlib.h>
int main()
{
int sum, x;
char op;
scanf_s("%d%c", &sum, &op,3);
while (op != '=')
{
scanf_s("%d", &x);
switch (op)
{
case'+':
sum += x;
break;
case'-':
sum -= x;
break;
case'*':
sum *= x;
break;
case'/':
if (x == 0)
{
printf("除法分母为0!\n");
return 0;
}
else
{
sum /= x;
break;
}
default:
printf("非法输入!\n");
return 0;
}
scanf_s("%c", &op,1);
}
printf("%d\n", sum);
system("pause");
return 0;
}
输出.png
网友评论