中缀表达式转逆波兰表达式
需要数据结构栈——ops
遍历中缀表达式;
- 若为数字:则直接输出到队列
- 若为
(:直接输出至队列 - 若为
):将栈中的op输出至队列,直到栈顶字符为( - 若为
+-*/:判断当前操作符和栈顶操作符之间的优先级
遍历逆波兰表达式:
- 若为数字直接入栈
- 若为操作符
opint rval=nums.top();nums.pop();nums.top()+=nums.pop();
直接输出计算后的栈顶元素nums.top()。
中缀表达式转逆波兰计算时,顺便计算
当操作符栈
ops出栈时,更新数字栈的栈顶!! 便可顺便进行计算
class Solution {//优先级列表unordered_map<char,int> priority={{'(',0},{'+',1},{'-',1},{'*',2},{'/',2},};void ignore_whitespace(const char* &p){while(*p==' ')++p;}inline bool is_op(char ch){return ch == '+' || ch == '-'|| ch == '*' || ch == '/';}//转换为intint to_int(const char* &p){ignore_whitespace(p);assert(isdigit(*p));int ret=0;while(isdigit(*p)){ret=ret*10+static_cast<int>(*p-'0');++p;}ignore_whitespace(p);return ret;}//根据操作符和数字栈,更新数字栈void _calcu(stack<int>& nums,stack<char>& ops){int rval=nums.top();nums.pop();char op=ops.top();ops.pop();switch(op){case '+':nums.top()+=rval;break;case '-':nums.top()-=rval;break;case '*':nums.top()*=rval;break;case '/':nums.top()/=rval;break;}}int convert_to_reverse_poland(string& s){auto p=s.c_str();stack<int> nums;stack<char> ops;while(*p!='\0'){ignore_whitespace(p);if(isdigit(*p)){nums.push(to_int(p));}else if(*p=='('){ops.push(*p);++p;}else if(*p==')'){++p;while(!ops.empty() && ops.top()!='('){_calcu(nums,ops);}ops.pop(); //!!!! 删除'('}else{// + - * /char op=*p;++p;while(!ops.empty() && priority[op]<=priority[ops.top()]){_calcu(nums,ops);}ops.push(op);}}while(!ops.empty()){_calcu(nums,ops);}return nums.top();}public:int calculate(string s) {return convert_to_reverse_poland(s);}};
