#include<stack>
#include<string>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 返回表达式的值
* @param s string字符串 待计算的表达式
* @return int整型
*/
int solve(string s) {
// write code here
//将中缀表达式转换为后缀表达式 优先级大就压栈
stack<char>opers;
string houzhui;
for(int i=0;i<s.size();i++){
if(s[i]>='0'&&s[i]<='9'){
while(s[i]>='0'&&s[i]<='9'){
houzhui+=s[i];
i++;
}
houzhui+='#';
i--;
}
else if(s[i]=='('){
opers.push(s[i]);
}
else if(s[i]==')'){
while(opers.top()!='('){
houzhui+=opers.top();
opers.pop();
}
opers.pop();
}
else if(s[i]=='*'){
if(opers.empty()||opers.top()=='('||opers.top()=='+'||opers.top()=='-'){
opers.push(s[i]);
}
else if(!opers.empty()&&opers.top()=='*'){
while(!opers.empty()&&opers.top()=='*'){
houzhui+=opers.top();
opers.pop();
}
opers.push(s[i]);
}
}
else if(s[i]=='+'||s[i]=='-'){
if(opers.empty()||opers.top()=='('){
opers.push(s[i]);
}
else if(opers.top()=='+'||opers.top()=='-'||opers.top()=='*'){
while(!opers.empty()&&opers.top()!='('){
houzhui+=opers.top();
opers.pop();
}
opers.push(s[i]);
}
}
}
while(!opers.empty()){
houzhui+=opers.top();
opers.pop();
}
cout<<houzhui;
//将后缀表达式求值 323*4*+1- 100#100#+ 12#3#+15#* (12+3)*15 "12#3#+15#*"
string str=houzhui;
int pos=-1;
stack<int>num;
for(int i=0;i<str.size();i++){
if(str[i]=='#'){
string numstr;
for(int j=pos+1;j<i;j++){
if(str[j]>='0'&&str[j]<='9'){
numstr+=str[j];
}
}
int numint=stoi(numstr);
num.push(numint);
pos=i;
}
else if(str[i]=='+'||str[i]=='-'||str[i]=='*'){
int n1,n2;
n1=num.top();
num.pop();
n2=num.top();
num.pop();
if(str[i]=='+'){
num.push(n2+n1);
}
else if(str[i]=='-'){
num.push(n2-n1);
}
else if(str[i]=='*'){
num.push(n2*n1);
}
}
}
return num.top();
}
};
// 一、核心转换规则
// 操作数(数字 / 变量):直接加入后缀表达式列表。
// 左括号(:压入栈中,标记括号内运算的开始。
// 右括号):弹出栈顶运算符并加入后缀列表,直到遇到左括号((左括号弹出但不加入后缀)。
// 运算符(+、-、*、/等):
// 若栈为空或栈顶是左括号(,直接压栈;
// 若栈顶运算符优先级 **≥** 当前运算符,弹出栈顶运算符加入后缀列表,重复此过程;
// 若栈顶运算符优先级 **<** 当前运算符,直接压栈。
// 遍历结束后:将栈中剩余运算符依次弹出,加入后缀列表。
/////////////////草稿//////////////////////
// #include<stack>
// class Solution {
// public:
// /**
// * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
// *
// * 返回表达式的值
// * @param s string字符串 待计算的表达式
// * @return int整型
// */
// int solve(string s) {
// // write code here
// //将中缀表达式转换为后缀表达式 优先级大就压栈
// stack<char>opers;
// stack<char>houzhui;
// for(int i=0;i<s.size();i++){
// if(s[i]>='0'&&s[i]<='9'){
// houzhui.push(s[i]);
// }
// else if(s[i]=='('){
// opers.push(s[i]);
// }
// else if(s[i]==')'){
// while(opers.top()!='('){
// houzhui.push(opers.top());
// opers.pop();
// }
// opers.pop();
// }
// else if(s[i]=='+'||s[i]=='-'){
// if(opers.empty()){
// opers.push(s[i]);
// }
// else if(opers.top()=='('){
// opers.push(s[i]);
// }
// else if(opers.top()=='*'){
// do{
// houzhui.push(opers.top());
// opers.pop();
// }while(!opers.empty()&&opers.top()!='(');
// }
// }
// else if(s[i]=='*'){
// if(opers.empty()){
// opers.push(s[i]);
// }
// else if(opers.top()=='('){
// opers.push(s[i]);
// }
// else if(opers.top()=='+'||opers.top()=='-'){
// opers.push(s[i]);
// }
// else if(opers.top()=='*'){
// do{
// houzhui.push(opers.top());
// opers.pop();
// }while(opers.top()!='+'&&opers.top()!='-');
// }
// }
// }
// while(!opers.empty()){
// houzhui.push(opers.pop());
// opers.pop();
// }
// string Houzhui;
// while(!houzhui.empty()){
// Houzhui+=houzhui.pop();
// houzhui.pop();
// }
// Houzhui.reverse(Houzhui.begin(),Houzhui.end());
// cout<<Houzhui;
// }
// };
// #include<stack>
// #include<string>
// class Solution {
// public:
// /**
// * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
// *
// * 返回表达式的值
// * @param s string字符串 待计算的表达式
// * @return int整型
// */
// int solve(string s) {
// // write code here
// //将中缀表达式转换为后缀表达式 优先级大就压栈
// stack<char>opers;
// string houzhui;
// for(int i=0;i<s.size();i++){
// if(s[i]>='0'&&s[i]<='9'){
// houzhui+=s[i];
// }
// else if(s[i]=='('){
// opers.push(s[i]);
// }
// else if(s[i]==')'){
// while(opers.top()!='('){
// houzhui+=opers.top();
// opers.pop();
// }
// opers.pop();
// }
// else if(s[i]=='*'){
// if(opers.empty()||opers.top()=='('||opers.top()=='+'||opers.top()=='-'){
// opers.push(s[i]);
// }
// else if(!opers.empty()&&opers.top()=='*'){
// while(!opers.empty()&&opers.top()=='*'){
// houzhui+=opers.top();
// opers.pop();
// }
// opers.push(s[i]);
// }
// }
// else if(s[i]=='+'||s[i]=='-'){
// if(opers.empty()||opers.top()=='('){
// opers.push(s[i]);
// }
// else if(opers.top()=='+'||opers.top()=='-'||opers.top()=='*'){
// while(!opers.empty()&&opers.top()!='('){
// houzhui+=opers.top();
// opers.pop();
// }
// opers.push(s[i]);
// }
// }
// }
// while(!opers.empty()){
// houzhui+=opers.top();
// opers.pop();
// }
// cout<<houzhui;
// //将后缀表达式求值 323*4*+1-
// string str=houzhui;
// stack<int>num;
// for(int i=0;i<str.size();i++){
// if(str[i]>='0'&&str[i]<='9'){
// int number=str[i]-'0';
// num.push(number);
// }
// else if(str[i]=='+'||str[i]=='-'||str[i]=='*'){
// int n1,n2;
// n1=num.top();
// num.pop();
// n2=num.top();
// num.pop();
// if(str[i]=='+'){
// num.push(n2+n1);
// }
// else if(str[i]=='-'){
// num.push(n2-n1);
// }
// else if(str[i]=='*'){
// num.push(n2*n1);
// }
// }
// }
// return num.top();
// }
// };