逆波兰表达式又称作后缀表达式,是一种便于计算机运算的数学表达式,我们日常使用的中缀表达式需要用括号确定优先级,而后缀表达式是不需要括号的
中缀表达式转后缀表达式 手写实现转换 比如想把(3+5)*4+6/(8-5)
转为后缀表达式
为所有运算符加括号
(3+5)*4+6/(8-5)
—–>(((3+5)*4)+(6/(8-5)))
将运算符移至")"
之后
(((3+5)*4)+(6/(8-5)))
—–>(((35)+4)*(6(85)-)/)+
去除所有括号
(((35)+4)*(6(85)-)/)+
—–>35+4*685-/+
即最终结果为35+4*685-/+
C++实现转换 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 #include <iostream> #include <string> #include <stack> using namespace std;bool isBracket (char c) { return (c == '(' || c == ')' ) ? true : false ; } int getPri (char c) { switch (c) { case '+' : case '-' : return 0 ; break ; case '*' : case '/' : return 1 ; break ; case '(' : case ')' : return -1 ; break ; default : cout << "输入其他运算符\n" << endl; } } void check (char c, stack<char >& operator_s, string& suffix) { if (operator_s.empty ()) { operator_s.push (c); return ; } if (isBracket (c)) { if (c == '(' ) { operator_s.push (c); } else { while (operator_s.top () != '(' ) { suffix += operator_s.top (); operator_s.pop (); } operator_s.pop (); } } else { if (getPri (c) > getPri (operator_s.top ())) { operator_s.push (c); } else { while (!operator_s.empty () && getPri (operator_s.top ()) >= getPri (c)) { suffix += operator_s.top (); operator_s.pop (); } operator_s.push (c); } } } string infixToSuffix (string& infix) { stack<char > operator_s; string suffix; for (int i = 0 ; i < infix.size (); ++i) { if (infix[i] >= '0' && infix[i] <= '9' ) { suffix += infix[i]; } else { check (infix[i], operator_s, suffix); } } while (!operator_s.empty ()) { suffix += operator_s.top (); operator_s.pop (); } return suffix; } int main () { string s = "2*5+6*(7-8)+6" ; cout << s << endl; string suffix = infixToSuffix (s); cout << suffix << endl; }
后缀表达式求值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 #include <iostream> #include <string> #include <stack> using namespace std;int main () { string s = "35+4*685-/+" ; stack<char > st; int value = 0 ; cout << s << endl; for (int i = 0 ; i < s.size (); i++) { if ('0' <=s[i]&&'9' >=s[i]) { st.push (s[i]); } else { if ('+' ==s[i]) { int b = st.top () - '0' ; st.pop (); int a = st.top () - '0' ; st.pop (); char t = a + b + '0' ; st.push (t); } else if ('-' ==s[i]) { int b = st.top () - '0' ; st.pop (); int a = st.top () - '0' ; st.pop (); char t = a - b + '0' ; st.push (t); } else if ('*' ==s[i]) { int b = st.top () - '0' ; st.pop (); int a = st.top () - '0' ; st.pop (); char t = a * b + '0' ; st.push (t); } else if ('/' ==s[i]) { int b = st.top () - '0' ; st.pop (); int a = st.top () - '0' ; st.pop (); char t = a / b + '0' ; st.push (t); } } } value = st.top () - '0' ; st.pop (); cout << "value = " << value << endl; }