四则运算

华为OJ上的题目:

请实现如下接口

    /* 功能:四则运算

     * 输入:strExpression:字符串格式的算术表达式,如: "3+2*{1+2*[-4/(8-6)+7]}"

         * 返回:算术表达式的计算结果

     */

    public static int calculate(String strExpression)

    {

        /* 请实现*/

        return 0;

    } 

约束:

  1. pucExpression字符串中的有效字符包括[‘0’-‘9’],‘+’,‘-’, ‘*’,‘/’ ,‘(’, ‘)’,‘[’, ‘]’,‘{’ ,‘}’。

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值