Simple calculator, simple calculator simulation work. Suppose only calculator arithmetic operations, operands and results are integers, the same four kinds of operator precedence is calculated from left to right.

Simple calculator
simulate simple arithmetic work. Suppose only calculator arithmetic operations, operands and results are integers, the same four kinds of operator precedence is calculated from left to right.

Input format:
input is given in a four row arithmetic expression, there is no space, and at least one operand. In case of equal sign "=" Enter the end of the description.
Output format:
outputting a calculation result of expression in a row, or if the denominator of the division is 0 or illegal operator outputs an error message "ERROR".
Sample input:
1 + 2 * 10-10 / 2 =
Output Sample:
10

# include<stdio.h>
int main()
{
	int a,b,c,flag=0;
	char ch;
	scanf("%d",&a);
	while((ch=getchar())!='=')
	{
		scanf("%d",&b);
		if((ch=='/')&&(b==0))
		{
			flag=1;break;
		}
		switch(ch)
		{
			case '+': a=a+b;break;
			case '-':a=a-b;break;
			case '*':a=a*b;break;
			case '/':a=a/b;break;
			default :
			flag=1;break;
		}
		if(flag) break;
	}
		if(flag)
		{
			printf("ERROR");
		}
	     else
	     {
	     	printf("%d",a);
		 }
	return 0;
}
Published 123 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/Du798566/article/details/104751177