zoukankan      html  css  js  c++  java
  • Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation.

    Valid operators are +-*/. Each operand may be an integer or another expression.

    Some examples:

      ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
      ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
    

    注意:一定要使用后出栈的操作数作为第一操作数,否则,对于没有对称操作的操作符会出错。

     C++实现代码:
    #include<iostream>
    #include<stack>
    #include<vector>
    #include<string>
    #include<cstdlib>
    using namespace std;
    
    class Solution
    {
    public:
        int evalRPN(vector<string> &tokens)
        {
            stack<int> operand;
            int operand1;
            int operand2;
            if(tokens.empty())
                return 0;
            int i;
            for(i=0; i<(int)tokens.size(); i++)
            {
                if(tokens[i]=="+")
                {
                    if(!operand.empty())
                    {
                        operand1=operand.top();
                        operand.pop();
                    }
                    if(!operand.empty())
                    {
                        operand2=operand.top();
                        operand.pop();
                    }
                    operand2+=operand1;
                    operand.push(operand2);
                }
                else if(tokens[i]=="-")
                {
                    if(!operand.empty())
                    {
                        operand1=operand.top();
                        operand.pop();
                    }
                    if(!operand.empty())
                    {
                        operand2=operand.top();
                        operand.pop();
                    }
                    operand2-=operand1;
                    operand.push(operand2);
                }
                else if(tokens[i]=="*")
                 {
                    if(!operand.empty())
                    {
                        operand1=operand.top();
                        operand.pop();
                    }
                    if(!operand.empty())
                    {
                        operand2=operand.top();
                        operand.pop();
                    }
                    operand2*=operand1;
                    operand.push(operand2);
                }
                else if(tokens[i]=="/")
                 {
                    if(!operand.empty())
                    {
                        operand1=operand.top();
                        operand.pop();
                    }
                    if(!operand.empty())
                    {
                        operand2=operand.top();
                        operand.pop();
                    }
                    operand2/=operand1;
                    operand.push(operand2);
                }
                else
                {
                    operand1=atoi(tokens[i].c_str());
                    operand.push(operand1);
                }
            }
            return operand.top();
        }
    };
    
    int main()
    {
        Solution s;
        vector<string> vec={"4","13","5","/","+"};
        cout<<s.evalRPN(vec)<<endl;
    }
  • 相关阅读:
    css3-8 内外边距中的注意要点有哪些
    php实现 统计输入中各种字符的个数
    Java设计模式偷跑系列(十八)建模和责任链模式的实现
    Delphi 3D Glscene安装
    五通信算法:五种编码增益比较matlab模拟
    OpenGL于MFC使用汇总(三)——离屏渲染
    设计模式--模板方法 And State模式
    EXCEL 两人的建立Y轴
    LeetCode Median of Two Sorted Arrays
    wordpress常见的问题
  • 原文地址:https://www.cnblogs.com/wuchanming/p/4112271.html
Copyright © 2011-2022 走看看