zoukankan      html  css  js  c++  java
  • LeetCode() Basic Calculator 不知道哪里错了

    class Solution {
    public:
        int calculate(string s) {
            stack<int> num;
            stack<char> symbol;
            for(int i=0;i<s.length();i++){
                if(s[i]==' ')   continue;
                else if(s[i]=='('||s[i]=='+'||s[i]=='-')  symbol.push(s[i]);
                else if(s[i]>='0'&&s[i]<='9'){
                    for(int j=i+1;j<s.length();j++){
                        if(s[j]<'0'||s[j]>'9'){
                            num.push(stoi(s.substr(i,j-i)));
                            i=j-1;
                            break;
                        }
                           
                    }
                }
                else if(s[i]==')'){
                    stack<int> tem_i;
                    stack<char> tem_c;
                    while(symbol.top()!='('){
                        tem_i.push(num.top());
                        num.pop();
                        tem_c.push(symbol.top());
                        symbol.pop();
                    }
                    symbol.pop();
                    while(!tem_c.empty()){
                        char c=tem_c.top();
                        tem_c.pop();
                        if(c=='+'){
                            int a=tem_i.top();
                            tem_i.pop();
                            int b=tem_i.top();
                            tem_i.pop();
                            tem_i.push(a+b);
                        }
                        if(c=='-'){
                            int a=tem_i.top();
                            tem_i.pop();
                            int b=tem_i.top();
                            tem_i.pop();
                            tem_i.push(a-b);
                        }
                    }
                    num.push(tem_i.top());
                    tem_i.pop();
                }
            }
            stack<int> tem_i;
            stack<char> tem_c;
            while(!symbol.empty()){
                tem_i.push(num.top());
                num.pop();
                tem_c.push(symbol.top());
                symbol.pop();
            }
            while(!tem_c.empty()){
                        char c=tem_c.top();
                        tem_c.pop();
                        if(c=='+'){
                            int a=tem_i.top();
                            tem_i.pop();
                            int b=tem_i.top();
                            tem_i.pop();
                            tem_i.push(a+b);
                        }
                        if(c=='-'){
                            int a=tem_i.top();
                            tem_i.pop();
                            int b=tem_i.top();
                            tem_i.pop();
                            tem_i.push(a-b);
                        }
            }
            return tem_i.top();
        }
        int stoi(string s){
            int res=0;
            for(int i=0;i<s.length();i++){
                res=res*10+(s[i]-'0');
            }
            return res;
        }
    };

  • 相关阅读:
    wode.
    python中迭代器和生成器。
    Embeded linux 之 UBIFS文件系统
    Windows下Git安装和使用
    套接字 之 windows与linux 差异
    Embeded linux之RTL8188EU/RTL8188ETV使用
    嵌入式Linux之“+”版本问题
    Uboot之net
    Embeded linux之reboot
    Embeded linux之cifs文件系统
  • 原文地址:https://www.cnblogs.com/yanqi110/p/5112842.html
Copyright © 2011-2022 走看看