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;
}
------解决方案--------------------
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(&x0);
x=1.0*x0;
if(op==27){state= 'F ';printf( "\n ");break;}
/* //make decision to update the state*/
if (op == '+ ' || op == '- ')
{
state = 'C ';
/* //update x1 and op1*/
if(op2== '* ')
x1 = x1 * x;
else
x1=x1/x;
op1 = op;
}
else if (op == '* ' || op== '/ ')
{
state = 'H ';
/* //update x1*/
if(op2== '* ')
x1 = x1 * x;
else
x1=x1/x;
op2=op;
}
else
{
state = 'J ';
if(op2== '* ')
result = x1 * x;
else
result=x1/x;
}
break;
case 'J ':
/* //print the result
//leave for state K unconditional*/
printf( " %f\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;
while(1)
{
input = getch();
if (input > = '0 ' && input <= '9 ')
{
*x = 10 * (*x) + input - '0 ';
printf( "%c ", input);
continue; /* //to go back the beginning of the loop*/
}
if (input == '= ' || input == '+ ' || input== '/ '||input==27
|| input == '- ' || input == '* ')
{
printf( " %c ",input);
break;
}
}
return input;
}