算法参考链接链接
#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
#include<unordered_map>
using namespace std;
stack<string> num;
stack<char> op;
string str;
int main()
{
unordered_map<char, int> pr{ {'(', 0}, {'+', 1}, {'-', 1}, {'*', 2},{'/', 2} };
cin >> str;
int n = str.size();
for (int i = 0; i < n; i++)
{
char c = str[i];
if (isdigit(c))
{
int x = 0, j = i;
while (j < n && isdigit(str[j]))
{
x = x * 10 + str[j++] - '0';
}
i = j - 1;
num.push(to_string(x));
}
else
{
if (c == '(') op.push(c);
else if (c == ')')
{
while (op.top() != '(')
{
string s(1, op.top());
num.push(s);
op.pop();
}
op.pop();
}
else if (op.empty() || pr[op.top()] < pr[c]) op.push(c);
else
{
while (op.size() && pr[op.top()] >= pr[c])
{
string s(1, op.top());
num.push(s);
op.pop();
}
op.push(c);
}
}
}
while (op.size())
{
string s(1, op.top());
num.push(s);
op.pop();
}
vector<string> v;
while (num.size())
{
v.push_back(num.top());
num.pop();
}
for (int i = v.size() - 1; i >= 0; i--) cout << v[i] << " ";
return 0;
}