改变运算优先级
作者:
凡尘_4
,
2022-03-19 16:57:26
,
所有人可见
,
阅读 160
对于一个给定的含有加减乘的中缀表达式,可以任意加括号,排序输出所有得到的答案
#include<bits/stdc++.h>
using namespace std;
string s;
int n;
vector<int> work(string str){
vector<int>tmp;
int l = str.length();
bool flag = 0;
for(int i = 0; i < l; i++){
if(!(isdigit(s[i]))){
flag = 1;
auto t1 = work(str.substr(0, i));
auto t2 = work(str.substr(i + 1, l - i));
for(auto x: t1)
for(auto y: t2){
if(str[i] == '+') tmp.push_back(x + y);
if(str[i] == '-') tmp.push_back(x - y);
if(str[i] == '*') tmp.push_back(x * y);
}
}
}
if(!flag){
int now = 0;
for(int i = 0; i < l; i++){
now = now * 10 + str[i] - '0';
}
tmp.push_back(now);
}
return tmp;
}
int main(){
cin >> s;
// n = strlen(s);
vector<int>ans = work(s);
sort(ans.begin(), ans.end());
for(auto x: ans){
cout<<x<<endl;
}
// work(s);
}