C語言 如何案ESC鍵回原問題
這是一道先乘除後加減的編程(按下'='鍵會算出答案)題目:
在按下'='鍵之前 按下'ESC' 會回到原題目
在算出答案後 按任何建繼續 按'ESC'會結束程序
不知道哪為大大可以幫我改一下
以下是先乘除後加減的編程 要把它加上題目說的
#include <stdio.h>
#include <stdlib.h>
char GetInteger(int* x);
int main(int argc, char *argv[])
{
char state; //to the state of the state machine
int x, x1, x2, x3, result;
char op, op1;
int repeat;
//explain the program
printf("Welcome to the single-digit calculator ");
printf("with multiplication.\n\n");
state = 'F'; //enter state F initially
repeat = 'y'; //enter the loop initially
while(repeat == 'y' || repeat == 'Y')
{
switch(state)
{
case 'F':
printf("Please input the equation:\n");
state = 'A'; //update the state
break;
case 'A':
case 'B':
op = GetInteger(&x);
//make decision to update the state
if (op == '+' || op == '-')
{
state = 'C';
op1 = op;
x1 = x;
}
else if (op == '*')
{
state = 'H';
x1 = x;
}
else
{
state = 'J';
result = x;
}
break;
case 'C':
case 'D':
case 'E':
//op,x <- input
//and leave for state E on +,-; x1 <- x1(op1)x,op1=op
//leave for state G on *; x2=x
//leave for state J on =; result <- x1(op1)x
op = GetInteger(&x);
//make decision to update the state
if (op == '+' || op == '-')
{
state = 'E';
//update x1 based op1
if (op1 == '+')
{
x1 = x1 + x;
}
else
{
x1 = x1 - x ;
}
//update op1
op1 = op;
}
else if (op == '*')
{
state = 'G';
x2 = x;
}
else
{
state = 'J';
//update result based op1
if (op1 == '+')
{
result = x1 + x;
}
else
{
result = x1 - x;
}
}
break;
case 'G':
//op,x <- input
//leave for state D on +,-; x1=x1(op1)x2*x;op1=op
//leave for state G on *; x2=x2*x
//leave for state J on =; result=x1(op1)x2*x
op = GetInteger(&x);
//update the state based on op
if (op == '+' || op == '-')
{
state = 'D';
//update x1 based on op1
if (op1 == '+')
{
x1 = x1 + x2 * x;
}
else
{
x1 = x1 - x2 * x;
}
//update op1
op1 = op;
}
else if (op == '*')
{
state = 'G';
x2 = x2 * x;
}
else
{
state = 'J';
//update result based on op1
if (op1 == '+')
{
result = x1 + x2 * x;
}
else
{
result = x1 - x2 * x;
}
}
break;
case 'H':
case 'I':
//op,x <- input
//leave for state C on +,-; x1 <- x1*x; op1=op
//or leave for state H on *; x1 <- x1*x
//or leave for state J on =; result <- x1*x
op = GetInteger(&x);
//make decision to update the state
if (op == '+' || op == '-')
{
state = 'C';
//update x1 and op1
x1 = x1 * x;
op1 = op;
}
else if (op == '*')
{
state = 'H';
//update x1
x1 = x1 * x;
}
else
{
state = 'J';
result = x1 * x;
}
break;
case 'J':
//print the result
//leave for state K unconditional
printf(" %d\n", result);
//update state
state = 'K';
break;
default:
case 'K':
//prompt the user if repeat or not
//leave for state F on repeat
//otherwise break the loop and terminate the prog
printf("Another calculation? (y/n) ");
repeat = getch(); //re-use op tempararily
printf("\n");
state = 'F'; //when repeat
break;
}
}
//prompt the user for completion
printf("\nBye!\n\n");
system("PAUSE");
return 0;
}
//Function definitions
char GetInteger(int* x)
{
char input;
*x = 0; //initialize the variable pointed to by x
while(1)
{
input = getch();
if (input >= '0' && input <= '9')
{
*x = 10 * (*x) + input - '0'; //update *x
printf("%c", input); //print the digit
continue; //to go back the beginning of the loop
}
if (input == '=' || input == '+'
|| input == '-' || input == '*')
{
printf(" %c ",input);
break; //to terminate the loop
}
}
return input;
}