读书人

C 言先乘除後加 救急解决思路

发布时间: 2012-03-23 12:06:21 作者: rapoo

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;
}

读书人网 >C语言

热点推荐