• 【代码·Patten】theme: Calculator


    Polish Calculator

    Infix->Postfix Rules:

    • number -> postfix stack
    • ( -> opera stack
    • ) -> pop opera stack until (
    • +-*/ ->
      • pop greater or equal priority operators
      • push them to postfix stack
      • opera stack
    class Solution {
    public:
        int string2Int(const string s){
            int result=0;
            for(auto c:s){
                result=result*10-48+c;
            }
            return result;
        }
    
        vector<string> str2Vector(string s){
            vector<string> infix_tokens;
            string num;
            for(char c:s){
                if(c==' ') continue;
                if(c>47)    num=num+c;
                else{
                    infix_tokens.push_back(num);
                    num = c;
                    infix_tokens.push_back(num);
                    num = "";
                }
            }
            infix_tokens.push_back(num);    //last num
            for(int i=0;i<infix_tokens.size();i++){
                cout<<infix_tokens[i]<<" ";
            }
            cout<<endl;
            return infix_tokens;
        }
    
        stack<string> infix2Post(vector<string> infix_tokens){
            stack<string> postfix_tokens;
            stack<string> opera;
            for(auto token:infix_tokens){
                if(token =="(") opera.push("(");
                else if(token==")"){
                    while(!opera.empty()&&opera.top()!="("){
                        postfix_tokens.push(opera.top());
                        opera.pop();
                    }
                    opera.pop();    //"("
                }
                else if(token=="+"||token=="-"||token=="*"||token=="/"){
                    if(token=="+"||token=="-"){
                        while(!opera.empty()&&opera.top()!="("){
                            postfix_tokens.push(opera.top());
                            opera.pop();
                        }
                        opera.push(token);
                    }
                    else{
                        while(!opera.empty()&&(opera.top()=="*"||opera.top()=="/")){
                            postfix_tokens.push(opera.top());
                            opera.pop();
                        }
                        opera.push(token);
                    }
                }
                else
                    postfix_tokens.push(token); //num
            }
            while(!opera.empty()){      //leftover
                postfix_tokens.push(opera.top());
                opera.pop();
            }
            return postfix_tokens;
        }
    
        int evalPostfix(stack<string> &postfix_tokens){
            int value = 0;
            if(!postfix_tokens.empty()){
                string opera = postfix_tokens.top();
                cout<<"opera: "<<opera<<endl;
                if(opera == "+"){
                    postfix_tokens.pop();
                    int second = evalPostfix(postfix_tokens);
                    int first = evalPostfix(postfix_tokens);
                    cout<<first<<" + "<<second<<endl;       //test
                    return first + second;
                }
                else if(opera == "-"){
                    postfix_tokens.pop();
                    int second = evalPostfix(postfix_tokens);
                    int first = evalPostfix(postfix_tokens);
                    cout<<first<<" - "<<second<<endl;       //test
                    return first - second;
                }
                else if(opera == "*"){
                    postfix_tokens.pop();
                    int second = evalPostfix(postfix_tokens);
                    int first = evalPostfix(postfix_tokens);
                    cout<<first<<" * "<<second<<endl;       //test
                    return first * second;
                }
                else if(opera == "/"){
                    postfix_tokens.pop();
                    int second = evalPostfix(postfix_tokens);
                    int first = evalPostfix(postfix_tokens);
                    cout<<first<<" / "<<second<<endl;       //test
                    return first / second;
                }
                else//num
                {
                    int num = string2Int(opera);
                    postfix_tokens.pop();
                    cout<<"num: "<<num<<endl;   //test
                    return num;
                }
            }
            else
            {
                cout<<"error 2"<<endl;
            }
            return 0;
    
        }
        int calculate(string s) {
            vector<string> infix_tokens = str2Vector(s);
            stack<string> postfix_tokens = infix2Post(infix_tokens);
            int result = evalPostfix(postfix_tokens);
            return result;
        }
    };
    
    from Chu
  • 相关阅读:
    Python操作SQLServer示例
    T-SQL 谓词和运算符
    T-SQL 语句—— 游标
    SQL中的循环、for循环、游标
    web自动化快速入门
    接口自动化的总结
    jenkins知识
    SVN知识
    random.sample函数
    项目实战(六)
  • 原文地址:https://www.cnblogs.com/chubuyu/p/15119508.html
Copyright © 2020-2023  润新知