coder的计算器
题目详情:
coder现在已经上初中,也会用计算器实现+ ,-,*,/和幂运算^了,但他觉得市场那些计算器太繁琐了,有很多他不认识的符号,所以他现在很想要能计算带括号的+ ,-,*,/和幂运算^的混合表达式就可以了,你能帮他实现这个愿望吗?还有coder希望这台计算器能告诉他每一步的计算结果,以便学习和检查。注意 2^2^2表示2^(2^2)。
输入格式:有T组数据,每一组都是一个表达式,表达式每个符号之间都会有一个空格,如1 + 2 / 3 =
输出格式:首先输出按照计算顺序的每一步的计算结果,而且要空行,最后输出计算结果,
第k组前要加上Case k : ,每个输出都要保留3位小数点。
答题说明:
输入样例:
1
1 - 0.5 ^ 2 ^ 0 + ( 2 - 1 ) =
输出样例:
2.000^0.000=1.000
0.500^1.000=0.500
1.000-0.500=0.500
2.000-1.000=1.000
0.500+1.000=1.500
Case 1: 1.500
错误的代码(由于对 ‘-’ 的二义性没有考虑到):
#include "stdio.h"
#include "string.h"
#include "math.h"
#include "ctype.h"
#include "stack"
#define maxn 1000
using namespace std;
char buf[maxn],length;stack<char> op;
/*在此犯了一个很严重的错误,错误的定义成了 stack<int> n,
这样的错误导致结果总是为 0,调试了好久很没有发现错误的根源在哪里*/
stack<double> n;int getPriority(char c)
{switch(c){case '(': return 1;case ')': return 1; case '+': return 2;case '-': return 2;case '*': return 3;case '/': return 3;case '^': return 4; default: return 0;}
}double calc(double a,double b,char c)
{double d; switch(c){case '+': d=(a+b); break;case '-': d=(a-b); break;case '*': d=(a*b); break;case '/': d=(a/b); break;case '^': d=pow(a,b); break;}printf("%.3lf%c%.3lf=%.3lf\n",a,c,b,d);return d;
}
void pull()
{double a,b;char c=op.top(); op.pop();if(getPriority(c)>1 && n.size()>1){b=n.top(); n.pop();a=n.top(); n.pop();n.push(calc(a,b,c));}
} int main()
{int T,count=0;scanf("%d",&T);while(T--){char c; double d; int i;length=0; count++;do{c=getchar();if(' '!=c) buf[length++]=c;}while('='!=c);i=-1;while(++i<length){ if(isalnum(buf[i])){//从左至右扫描表达式,数字读入 sscanf(buf+i,"%lf",&d); n.push(d);while(isalnum(buf[i+1]) || '.'==buf[i+1]) i++;}else{//从左至右扫描表达式,运算符读入c=buf[i];if(getPriority(c)){ //能被识别的操作符 if('('==c || '^'==c || op.empty() || getPriority(c)>getPriority(op.top()) ) op.push(c);else if(')'==c){while(!op.empty() && '('!=op.top()) pull();if(!op.empty()) op.pop();}else{while(!op.empty() && getPriority(c)<=getPriority(op.top())) pull(); //操作符计算 op.push(c);}} } }while(!op.empty()) pull();printf("Case %d: %.3lf\n",count,n.top());while(!n.empty()) n.pop(); //清空数据栈 }return 0;
}
修改后(AC):
#include "stdio.h"
#include "string.h"
#include "math.h"
#include "ctype.h"
#include "stack"
#define maxn 1000
using namespace std;
char buf[maxn],length;stack<char> op;
stack<double> n;int getPriority(char c)
{switch(c){case '(': return 1;case ')': return 1; case '+': return 2;case '-': return 2;case '*': return 3;case '/': return 3;case '^': return 4; default: return 0;}
}double calc(double a,double b,char c)
{double d; switch(c){case '+': d=(a+b); break;case '-': d=(a-b); break;case '*': d=(a*b); break;case '/': d=(a/b); break;case '^': d=pow(a,b); break;}printf("%.3lf%c%.3lf=%.3lf\n",a,c,b,d);return d;
}
void pull()
{double a,b;char c=op.top(); op.pop();if(getPriority(c)>1 && n.size()>1){b=n.top(); n.pop();a=n.top(); n.pop();n.push(calc(a,b,c));}
} int main()
{int T,count=0;scanf("%d",&T);while(T--){char c; double d; int i;length=0; count++;do{c=getchar();if(' '!=c && '\n'!=c) buf[length++]=c;}while('='!=c);i=-1;while(++i<length){ if( buf[i]=='-' ){ // '-' 可能出现二义性(符号或减号)因此特殊处理 int flag=0;if(i==0) flag=1;else if(i>0){int tmp=i;flag=1;while(tmp--){if(isalnum(buf[tmp])){flag=0; break;}else if(getPriority(buf[tmp])>1) break;}}if(flag){sscanf(buf+i,"%lf",&d); n.push(d);while(isalnum(buf[i+1]) || '.'==buf[i+1]) i++;continue;} }if(isalnum(buf[i])){//从左至右扫描表达式,数字读入 sscanf(buf+i,"%lf",&d); n.push(d);while(isalnum(buf[i+1]) || '.'==buf[i+1]) i++;}else{//从左至右扫描表达式,运算符读入c=buf[i];if(getPriority(c)){ //能被识别的操作符 if('('==c || '^'==c || op.empty() || getPriority(c)>getPriority(op.top()) ) op.push(c);else if(')'==c){//遇到有括号退栈计算,直到计算到左括号或栈空为止 while(!op.empty() && '('!=op.top()) pull();if(!op.empty()) op.pop();}else{while(!op.empty() && getPriority(c)<=getPriority(op.top())) pull(); //操作符计算 op.push(c);}} } }while(!op.empty()) pull();printf("Case %d: %.3lf\n",count,n.top());while(!n.empty()) n.pop(); //清空数据栈 }return 0;
}