标签:转化 names color pac turn nbsp 堆栈 技术分享 begin
这是堆栈那一节的选作实验,如下:
算法的核心规则
【显然,我们也可用树来实现这一过程】
c++实现转化代码如下
#include"pch.h" #include <iostream> #include <string> #include <stack> #include <map> using namespace std; int main() { string Midsort = "a+b*(c-d)-e/f"; string Behindsort = ""; stack<char> stk; map<char, int> op; op[‘(‘] = 0; op[‘)‘] = 0; op[‘+‘] = 1; op[‘-‘] = 1; op[‘*‘] = 2; op[‘/‘] = 2; string::iterator it = Midsort.begin();;//可以理解为指针哟 付凤涛桑 while (it != Midsort.end()) { if (op.count(*it)) { if (*it == ‘)‘) { while (stk.top() != ‘(‘) { Behindsort += stk.top(); stk.pop(); } stk.pop(); } else if (stk.empty() || *it == ‘(‘ || op[*it] > op[stk.top()]) { stk.push(*it); } else if (op[*it] <= op[stk.top()]) { while (op[*it] <= op[stk.top()] && (!stk.empty())) { Behindsort += stk.top(); stk.pop(); if (stk.empty()) break; } stk.push(*it); } } else { Behindsort += *it; } it++; if (it == Midsort.end()) { while (!stk.empty()) { Behindsort += stk.top(); stk.pop(); } break; } } cout << Behindsort << endl; return 0; }
@付风涛
标签:转化 names color pac turn nbsp 堆栈 技术分享 begin
原文地址:https://www.cnblogs.com/vk98/p/9989882.html