将表达式转换为逆波兰表达式-LintCode

给定一个表达式字符串数组,返回该表达式的逆波兰表达式(即去掉括号)。

示例:
对于 [3 - 4 + 5]的表达式(该表达式可表示为[“3”, “-“, “4”, “+”, “5”]),返回 [3 4 - 5 +](该表达式可表示为 [“3”, “4”, “-“, “5”, “+”])。

思想:
对于数字时,加入后缀表达式;
对于运算符:
a. 若为 ‘(‘,入栈;
b. 若为 ‘)’,则依次把栈中的的运算符加入后缀表达式中,直到出现’(‘,从栈中删除’(’ ;
c. 若为 除括号外的其他运算符, 当其优先级高于除’(‘以外的栈顶运算符时,直接入栈。否则从栈顶开始,依次弹出比当前处理的运算符优先级高和优先级相等的运算符,直到一个比它优先级低的或者遇到了一个左括号为止。
当扫描的中缀表达式结束时,栈中的的所有运算符出栈。

#ifndef C370_H
#define C370_H
#include<iostream>
#include<vector>
#include<string>
#include<stack>
using namespace std;
class Solution {
public:
    /*
    * @param expression: A string array
    * @return: The Reverse Polish notation of this expression
    */
    vector<string> convertToRPN(vector<string> &expression) {
        // write your code here
        vector<string> post;
        stack<string> sk;
        for (auto c : expression)
        {
            if (c == "(")
                sk.push(c);
            else if (c == ")")
            {
                while (sk.top() != "(")
                {
                    post.push_back(sk.top());
                    sk.pop();
                }
                sk.pop();
            }
            else
            {
                if (string("+-*/").find(c) == string::npos)
                    sk.push(c);
                else
                {
                    while (!sk.empty() && getPriority(sk.top()) >= getPriority(c))
                    {
                        post.push_back(sk.top());
                        sk.pop();
                    }
                    sk.push(c);
                }
            }
        }
        while (!sk.empty())
        {
            post.push_back(sk.top());
            sk.pop();
        }
        return post;
    }
    int getPriority(string s)
    {
        if (s == "(")
            return 0;
        else if (s == "+" || s == "-")
            return 1;
        else if (s == "*" || s == "/")
            return 2;
        else
            return 3;
    }
};
#endif
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是将中缀算数表达式转换为逆波兰式的C++代码: ```c++ #include <iostream> #include <stack> #include <string> #include <sstream> #include <vector> using namespace std; bool is_operator(const string& token) { return token == "+" || token == "-" || token == "*" || token == "/"; } int precedence(const string& token) { if (token == "+" || token == "-") { return 1; } else if (token == "*" || token == "/") { return 2; } else { return 0; } } vector<string> infix_to_postfix(const vector<string>& infix) { stack<string> s; vector<string> postfix; for (const auto& token : infix) { if (is_operator(token)) { while (!s.empty() && is_operator(s.top()) && precedence(token) <= precedence(s.top())) { postfix.push_back(s.top()); s.pop(); } s.push(token); } else if (token == "(") { s.push(token); } else if (token == ")") { while (!s.empty() && s.top() != "(") { postfix.push_back(s.top()); s.pop(); } s.pop(); } else { postfix.push_back(token); } } while (!s.empty()) { postfix.push_back(s.top()); s.pop(); } return postfix; } int evaluate_postfix(const vector<string>& postfix) { stack<int> s; for (const auto& token : postfix) { if (is_operator(token)) { int b = s.top(); s.pop(); int a = s.top(); s.pop(); if (token == "+") { s.push(a + b); } else if (token == "-") { s.push(a - b); } else if (token == "*") { s.push(a * b); } else { s.push(a / b); } } else { stringstream ss(token); int x; ss >> x; s.push(x); } } return s.top(); } int main() { // 中缀表达式: (3+4)*5-6/2 vector<string> infix = {"(", "3", "+", "4", ")", "*", "5", "-", "6", "/", "2"}; vector<string> postfix = infix_to_postfix(infix); for (const auto& token : postfix) { cout << token << " "; } cout << endl; int result = evaluate_postfix(postfix); cout << "Result: " << result << endl; return 0; } ``` 这个代码使用了 `stack` 来实现算法,其中 `infix_to_postfix` 函数将中缀表达式转换为后缀表达式, `evaluate_postfix` 函数计算后缀表达式的值。在 `main` 函数中,我们使用了 `(3+4)*5-6/2` 这个表达式来测试我们的代码,输出结果为: ``` 3 4 + 5 * 6 2 / - Result: 29 ``` 这表明我们的代码成功地将中缀表达式转换为了后缀表达式,并正确地计算了表达式的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值