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