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



aK2oEUAz.txt (46 Bytes) C語言救急 先乘除後加減

搜索更多相关主题的帖子: 乘除 
2007-03-24 12:15
快速回复:C語言救急 先乘除後加減
数据加载中...
 
   



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

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