calculator

时间:2023-12-26 17:59:01
#include <stdio.h>
#include <stdlib.h> typedef enum
{
FALSE = ,
TRUE
}BOOL; void calculator(void); int main(void)
{
char y = ' ';
y = 'y';
while('y' == y)
{
printf("start calculator:\n");
calculator();
printf("restart?,printf 'y' to continue\n");
scanf(" %c",&y);
printf("%c\n",y);
}
return ;
} void calculator(void)
{
char c;
char ucOperator = ' ';
int k = ;
int value = ;
int lastValue = ;
int result = ;
int lastk; printf("start calculator input:\n");
/* while((c = getchar()) == '\n'||c == ' ')
{
;
}
//this does work,the below way is more simple
*/
scanf(" %c",&c); //filter space and return
do{
if(c >= '' && c <= '')
{
if(!value)
{
value = c - '';
}
else
{
value = value* + c- '';
}
continue;
} if('+' == c)
{
lastk = k;
k = ;
}
else
{
if('-' == c)
{
lastk = k;
k = -;
c = '+';
}
} switch(c)
{
case '+':
switch(ucOperator)
{
case '*':
result += lastValue * value * lastk;
break;
case '/':
result += lastValue / value * lastk;
break;
case '+':
result += value * lastk;
break;
case ' ':
result += value;
break;
default:
break;
}
ucOperator = '+';
break; case '*':
lastValue = value; ucOperator = '*';
break;
case '/':
lastValue = value; ucOperator = '/';
break;
case '=':
if('+' == ucOperator)
{
result += value * k;
}
if('*' == ucOperator)
{
result += value * lastValue * k;
}
if('/' == ucOperator)
{
result += lastValue / value * k;
}
printf("%d\n",result);
result = ;
ucOperator = ' ';
break;
default:
break;
}
value = ;
}while((c = getchar()) != '\n');
}