zoukankan      html  css  js  c++  java
  • leetcode227

    class Solution {
    public:
        stack<int> OPD;
        stack<char> OPR;
        int calculate(int num1, int num2, char opr){
            switch (opr){
            case '+':{
                         return num1 + num2;
            }
            case '-':{
                         return num1 - num2;
            }
            case '*':{
                         return num1*num2;
            }
            case '/':{
                         return num1 / num2;
            }
            }
        }
        int calculate(string s) {
            int len = s.size();
            int i=0;
            while(i<len){
                if (s[i] == ' '){
                    i++;
                }
                else if (s[i] == '+' || s[i] == '-' || s[i]=='*' || s[i]=='/'){
                    OPR.push(s[i]);
                    i++;
                }
                else{
                    int num = 0;
                    while (i < len && s[i] >= '0' && s[i] <= '9'){
                        num = num * 10 + (s[i] - '0');
                        i++;
                    }
                    if (!OPR.empty() && (OPR.top()=='/' || OPR.top()=='*')){
                        num = calculate(OPD.top(),num,OPR.top());
                        OPR.pop();
                        OPD.pop();
                    }
                    OPD.push(num);
                }
            }
            int res=0;
            while (!OPR.empty()){
                int tmp=OPD.top();
                char ch=OPR.top();
                if(ch=='-'){
                    tmp=-tmp;
                }
                res+=tmp;
                OPD.pop();
                OPR.pop();
            }
            res+=OPD.top();
            return res;
        }
    };
  • 相关阅读:
    express4.x socket
    validator
    服务管理,Dll查看
    复制程序,获取系统信息
    TCP HelloWord
    UDP HelloWord
    [置顶] 一个小马
    注册表编辑
    服务的启动与停止
    自下载运行
  • 原文地址:https://www.cnblogs.com/asenyang/p/9815235.html
Copyright © 2011-2022 走看看