zoukankan      html  css  js  c++  java
  • 中缀表达式转后缀表达式

    示例:9+(3-1)*3+10/2

    #include<map>
    #include<stack>
    #include<queue>
    #include<string>
    #include<iostream>
    using namespace std;
    map
    <char, int> p; struct Node { double num; char op; bool flag; }; stack<Node> s; queue<Node> q; void midtoback(string str) { Node temp; for (int i = 0; i < str.length();) { if (str[i] == '(') { temp.flag = false; temp.op = str[i]; s.push(temp); i++; } else if (str[i] == ')') { while (!s.empty() && s.top().op != '(') { q.push(s.top()); s.pop(); } s.pop(); i++; } else if(str[i]>='0'&&str[i]<='9') { temp.flag = true; temp.num = str[i] - '0'; i++; while (i < str.length() && str[i] >= '0' && str[i] <= '9') { temp.num = temp.num * 10 + (str[i] - '0'); i++; } q.push(temp); } else { temp.flag = false; while (!s.empty() && p[s.top().op] >= p[str[i]]) { q.push(s.top()); s.pop(); } temp.op = str[i]; s.push(temp); i++; } } while (!s.empty()) { q.push(s.top()); s.pop(); } } int main() { Node cur; string str; p['+'] = p['-'] = 1; p['*'] = p['/'] = 2; cin >> str; midtoback(str); while (!q.empty()) { cur = q.front(); if (cur.flag == true) cout << cur.num << " "; else cout << cur.op << " "; q.pop(); } return 0; }
  • 相关阅读:
    contextMenu,右键菜单
    hashchange
    web攻击日志分析之新手指南
    工匠人生
    数学有卵用之通信篇
    精英主义(一)
    flaskbb部署笔记
    深入分析一波,你们说的云安全到底是什么鬼?
    Gh0st与云安全
    困境与突破
  • 原文地址:https://www.cnblogs.com/single-dont/p/11684876.html
Copyright © 2011-2022 走看看