| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 505 人关注过本帖
标题:C語言 如何案ESC鍵回原問題
只看楼主 加入收藏
cjw23529
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-3-22
收藏
 问题点数:0 回复次数:0 
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;
}


yiJQhZU7.txt (7.45 KB) C語言 如何案ESC鍵回原問題

搜索更多相关主题的帖子: ESC 何案 
2007-03-22 19:37
快速回复:C語言 如何案ESC鍵回原問題
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022186 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved