华为OJ上的题目:
请实现如下接口
/* 功能:四则运算
* 输入:strExpression:字符串格式的算术表达式,如: "3+2*{1+2*[-4/(8-6)+7]}"
* 返回:算术表达式的计算结果
*/
public static int calculate(String strExpression)
{
/* 请实现*/
return 0;
}
约束:
pucExpression字符串中的有效字符包括[‘0’-‘9’],‘+’,‘-’, ‘*’,‘/’ ,‘(’, ‘)’,‘[’, ‘]’,‘{’ ,‘}’。
pucExpression算术表达式的有效性由调用者保证;
自己想的一个解决方案:(有点代码其实还是可以优化的)#include <iostream> #include <stack> #include <string> #include <map> using namespace std; int calculate(string& s) { if(s.length()==0) return 0; map<char,int> symbol; symbol['+']=1; symbol['-']=1; symbol['*']=2; symbol['/']=2; symbol['(']=0; symbol[')']=0; symbol['[']=0; symbol[']']=0; symbol['{']=0; symbol['}']=0; stack<char> sym_stk; stack<int> num_stk; int result=0; int is_positive=1; bool judge_sym=false; for(int i=0;i<s.length();) { if(sym_stk.size()!=0&&(sym_stk.top()=='('||sym_stk.top()=='['||sym_stk.top()=='{')&&judge_sym) { if(s[i]=='-') { is_positive=-1; i++; } judge_sym=false; } if((s[i]-'0')>=0&&(s[i]-'0')<=9) { int right=0; while(i < s.length() && s[i] <= '9' && s[i] >= '0') { right=right*10+s[i]-'0'; i++; } right*=is_positive; num_stk.push(right); is_positive=1; } else { if(sym_stk.empty()) { sym_stk.push(s[i]); i++; } else { if(symbol[s[i]]-symbol[sym_stk.top()]>0||(s[i]=='(')||(s[i]=='[')||(s[i]=='{')) { sym_stk.push(s[i]); if((s[i]=='(')||(s[i]=='[')||(s[i]=='{')) judge_sym=true; i++; } else if((s[i]==')')||(s[i]==']')||(s[i]=='}')) { char special_char; switch(s[i]) { case ')': {special_char='(';break;} case ']': {special_char='[';break;} case '}': {special_char='{';break;} } while(sym_stk.top()!=special_char) { char sym_temp=sym_stk.top(); int temp_result; sym_stk.pop(); int num1=num_stk.top(); num_stk.pop(); int num2=num_stk.top(); num_stk.pop(); switch(sym_temp) { case '+': { temp_result=num2+num1; break; } case '-': { temp_result=num2-num1; break; } case '*': { temp_result=num2*num1; break; } case '/': { if(num1==0) return -1; temp_result=num2/num1; break; } } num_stk.push(temp_result); } sym_stk.pop(); i++; } else { while(sym_stk.size()!=0&&symbol[s[i]]<=symbol[sym_stk.top()]) { char sym_temp=sym_stk.top(); int temp_result; sym_stk.pop(); int num1=num_stk.top(); num_stk.pop(); int num2=num_stk.top(); num_stk.pop(); switch(sym_temp) { case '+': { temp_result=num2+num1; break; } case '-': { temp_result=num2-num1; break; } case '*': { temp_result=num2*num1; break; } case '/': { if(num1==0) return -1; temp_result=num2/num1; break; } } num_stk.push(temp_result); } sym_stk.push(s[i]); i++; } } } } while(!sym_stk.empty()) { char sym_temp=sym_stk.top(); int temp_result; sym_stk.pop(); int num1=num_stk.top(); num_stk.pop(); int num2=num_stk.top(); num_stk.pop(); switch(sym_temp) { case '+': { temp_result=num2+num1; break; } case '-': { temp_result=num2-num1; break; } case '*': { temp_result=num2*num1; break; } case '/': { if(num1==0) return -1; temp_result=num2/num1; break; } } num_stk.push(temp_result); } return num_stk.top(); } int main() { string s; cin>>s; //string s="3+2*{1+2*[-4/(8-6)+7]}"; int a= calculate(s); cout<<a; }