C語言救急 先乘除後加減
這是一到先乘除後加減的編程這裡面少了一個除法 不知道哪為大大可以幫個忙幫我加上去
還有在案下'='鍵之前 如果按'ESC'鍵 會回到原問題
最後算出答案後案'ESC'鍵會終結程序
#include <stdio.h>
#include <stdlib.h>
char GetInteger(int* x);
int main(int argc, char *argv[])
{
char state;
int x, x1, x2, x3, result;
char op, op1;
int repeat;
printf("Welcome to the single-digit calculator ");
printf("with multiplication.\n\n");
state = 'F';
repeat = 'y';
while(repeat == 'y' || repeat == 'Y')
{
switch(state)
{
case 'F':
//state F: prompt user for input
//and leave for state A unconditional
printf("Please input the equation:\n");
state = 'A'; //update the state
break;
case 'B':
op = GetInteger(&x);
if (op == '+' || op == '-')
{
state = 'D';
op1 = op;
x1 = x;
}
else if (op == '*')
{
state = 'I';
x1 = x;
}
else
{
state = 'J';
result = x;
}
break;
case 'D':
op = GetInteger(&x);
if (op == '+' || op == '-')
{
state = 'D';
if (op1 == '+')
{
x1 = x1 + x;
}
else
{
x1 = x1 - x ;
}
op1 = op;
}
else if (op == '*')
{
state = 'G';
x2 = x;
}
else
{
state = 'J';
if (op1 == '+')
{
result = x1 + x;
}
else
{
result = x1 - x;
}
}
break;
case 'G':
op = GetInteger(&x);
if (op == '+' || op == '-')
{
state = 'D';
if (op1 == '+')
{
x1 = x1 + x2 * x;
}
else
{
x1 = x1 - x2 * x;
}
op1 = op;
}
else if (op == '*')
{
state = 'G';
x2 = x2 * x;
}
else
{
state = 'J';
if (op1 == '+')
{
result = x1 + x2 * x;
}
else
{
result = x1 - x2 * x;
}
}
break;
case 'I':
op = GetInteger(&x);
if (op == '+' || op == '-')
{
state = 'D';
x1 = x1 * x;
op1 = op;
}
else if (op == '*')
{
state = 'I';
x1 = x1 * x;
}
else
{
state = 'J';
result = x1 * x;
}
break;
case 'J':
printf(" %d\n", result);
state = 'K';
break;
default:
case 'K':
printf("Another calculation? (y/n) ");
repeat = getch(); //re-use op tempararily
printf("\n");
state = 'F'; //when repeat
break;
}
}
printf("\nBye!\n\n");
system("PAUSE");
return 0;
}
char GetInteger(int* x)
{
char input;
*x = 0;
while(1)
{
input = getch();
if (input >= '0' && input <= '9')
{
*x = 10 * (*x) + input - '0';
printf("%c", input);
continue;
}
if (input == '=' || input == '+'
|| input == '-' || input == '*')
{
printf(" %c ",input);
break; //to terminate the loop
}
}
return input;
}