/*===============================================* ** 设计目的:简单计算器,计算形如10*(20.2-30.6)+5.0/2的表达式值 ** 简要原理:中缀式转逆波兰式(后缀式) ** IDE:Dev-Cpp 4.9.9.2 ** 注意事项:括号必须是英文状态的 ** 时间: 2014-6-17 *===============================================*/ #include <stdio.h> #include <string.h> #include <stdlib.h> #define maxn 1000 char buf[maxn], str[maxn], signStack[maxn], ch[2]; int len, id, idSign, idAns, i, n; double ans[maxn]; void checkSign(char sign){ if(sign == '(') signStack[idSign++] = sign; else if(sign == '*' || sign == '/'){ while(idSign && (signStack[idSign-1] == '*' || signStack[idSign-1] == '/')) str[id++] = signStack[--idSign]; signStack[idSign++] = sign; }else if(sign == ')'){ while(signStack[idSign-1] != '(') str[id++] = signStack[--idSign]; --idSign; }else{ while(idSign && signStack[idSign-1] != '(') str[id++] = signStack[--idSign]; signStack[idSign++] = sign; } str[id++] = ' '; } int check(double a, char sign){ if(a == 0 && sign == '/'){ printf("除数不能为0!,程序结束。 "); system("pause"); exit(EXIT_FAILURE); } return 1; } double cal(double a, double b, char sign){ switch(sign){ case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; } } int main(){ printf(" 简单计算器 "); printf(" ====================================== "); printf(" || 支持+ - * / ( ) 等符号和负数输入 || "); printf(" ====================================== "); printf(" 原创作者:邱正钢 "); printf(" 完毕时间:2014年6月17日 "); printf(" Email:2276479303@qq.com "); printf("请输入一个表达式,如 -10*(20.2-30.6)+5.0/2 以回车结束: "); do{ gets(buf); id = idSign = idAns = 0; if((len = strlen(buf)) == 0) continue; int flag = 0; //这个是用来推断'-'字符是负号还是减号的。1表示数字,0表示 +*/( /*推断原理:假设第一次读取就碰到-,那么一定是负号,若近期一次读取是+*-/(那么 也一定是负号,假设是右括号或数字那么是减号*/ for(i = 0; i < len; ++i){ if(buf[i] == ' ') continue; if(buf[i] >= '0' && buf[i] <= '9' || buf[i] == '.' || buf[i] == '-' && !flag){ str[id++] = buf[i]; flag = 1; } else { str[id++] = ' '; checkSign(buf[i]); if(buf[i] != ')') flag = 0; else flag = 1; } } while(idSign) str[id++] = signStack[--idSign]; for(i = 0, n = 0; i < id; ++i){ if(str[i] == ' ') continue; /*用添加空格的方式推断‘-’字符是负号还是减号,若‘-’后面挨着数字或小数点,那么一定是负号*/ if(str[i] >= '0' && str[i] <= '9' || str[i] == '.' || str[i] == '-' && (str[i+1] >= '0' && str[i+1] <= '9' || str[i+1] == '.')){ sscanf(str + i, "%lf%n", &ans[idAns++], &n); i += n - 1; continue; }else if(check(ans[idAns-1], str[i])){ ans[idAns-2] = cal(ans[idAns-2], ans[idAns-1], str[i]); --idAns; } } printf("结果是 %.2lf ", ans[0]); printf("输入Y 继续, N 退出: "); scanf("%s", ch); }while(ch[0] == 'Y' || ch[0] == 'y'); printf("感谢您的使用!再见 "); system("pause"); return 0; }