227. Basic Calculator II(medium)[后缀表达式 栈]

来源:互联网 发布:九院13所知乎 编辑:程序博客网 时间:2024/05/08 04:29

Implement a basic calculator to evaluate a simple expression string.

The expression string contains only non-negative integers, +-*/ operators and empty spaces . The integer division should truncate toward zero.

You may assume that the given expression is always valid.

Some examples:

"3+2*2" = 7" 3/2 " = 1" 3+5 / 2 " = 5

Note: Do not use the eval built-in library function.

与Basic Calculator实现方法相似,不过此时没有括号优先级,有*和/运算符



class Solution {public:    int compareSym(int a,int b)    {        //cout<<a<<" "<<b<<endl;        int flag = 0;        if(a>b)           flag = 1;        if(a<b)           flag = -1;        return flag;    }    int calculate(string s) {        map<char,int> lpri;         lpri['='] = 0;        lpri['+'] = 2;        lpri['-'] = 2;        lpri['*'] = 4;        lpri['/'] = 4;        map<char,int> rpri;        rpri['='] = 0;        rpri['+'] = 1;        rpri['-'] = 1;        rpri['*'] = 3;        rpri['/'] = 3;       //当当前符号在rpri中比栈顶运算符lpri的值大的时候才能进栈       //下面利用栈生成后缀表达式       string prefix;       stack<char> sym;       sym.push('=');       int i = 0;       while(s[i] != '\0')       {           if(isdigit(s[i]))           {                 prefix += s[i];               while(s[i+1] !='\0'&&isdigit(s[i+1]))                  prefix += s[++i];                prefix += '#';                i++;           }           else if(s[i] == ' ')           {               i++;           }           else           {               switch(compareSym(lpri[sym.top()],rpri[s[i]]))               {                   case 1:                       prefix += sym.top();                       sym.pop();                       break;                   case 0:                       sym.pop();                       ++i;                       break;                   case -1:                       sym.push(s[i++]);                       break;               }           }       }       //prefix += '#';       while(sym.top() != '=')       {           prefix += sym.top();           sym.pop();       }       //cout<<prefix<<endl;       stack<int> number;       int j=0;       while(prefix[j] != '\0')       {           //cout<<"j:"<<j<<endl;          if(isdigit(prefix[j]))             {                 string num;              num += prefix[j];              while(prefix[++j] != '#')                 num += prefix[j];              stringstream ss;              ss<<num;              int temp;              ss>>temp;              //cout<<"sh:"<<temp<<endl;              number.push(temp);          }else if(prefix[j] == '#')          {            ++j;  }else          {              int b = number.top();              number.pop();              int a = number.top();              number.pop();              //cout<<a<<" "<<b<<endl;              switch(prefix[j++])              {                  case '+':                    number.push(a+b);                    break;                  case '-':                    number.push(a-b);                    break;                  case '*':                    number.push(a*b);                    break;                  case '/':                    number.push(a/b);                    break;              }          }       }             return number.top();    }};


0 0