There are many stack applications. The function calls and Recursion that we are most familiar with are implemented by the operating principle of the stack for the compiler. There is also a backward option in our browser, which is a stack application (this is an example in <big talk Data Structure> ). There is also the expression to be evaluated in this article. We know that C/C ++ is an expression-based programming language. We can see the importance of expressions to languages. We will not discuss what expressions are and so on here. We can directly talk about three types of expressions: prefix expressions, infix expressions, and suffix expressions. We will only discuss the binary operators here, such as the single object and the third object. Infix representation: <operand> <operator> <operand>, for example, A + B prefix representation: <operator> <operand>, for example, + AB suffix representation: <operand>. For example, AB + uses the infix form to evaluate the expression value, which is also frequently used in various programming languages and calculators. Two stacks are required, one storage operand and one storage operator. Here we will only describe how to use the suffix to evaluate the expression value. Using the suffix form to evaluate the expression value requires only one stack, and the operation is convenient, but an algorithm and process are required to convert the expression from the infix form to the suffix form. First, an infix expression, A + B * (C-D)-E/F, is provided. Its suffix is in the form of ABCD-* + EF /-. How can I convert it into a suffix expression? This requires an algorithm implementation. Several questions should be considered before the algorithm is expanded. One is how to express the operator priority in the order of its suffix format, and the other is how to handle the brackets. To solve the first problem, we use a common binary operator as the sorting operator ch # (*,/, % + ,-) isp 0 1 6 3 6 icp 0 6 4 2 1 because we need to first press a symbol on the stack as a sign, so the # is defined as 0, meaning that all operators are higher than their priority. Isp and icp operators have different priorities outside the stack. isp (in stack prioroty) and icp (in coming priority ). Now the problem has been solved, that is, the implementation of an algorithm. See the algorithm below: <1> initialize the operator stack and press the terminator '#' into the stack. Then, read the first character ch of the infix expression. <2> repeat the following steps until ch = '#' and the operator at the top of the stack is '#' to stop the loop. A: If ch is output directly by the operator, it is read into the next character ch. B: If it is an operator, determine the priority of the ch. The icp value is the priority of the operator op at the top of the stack. isp value: @ if it is an icp (ch)> isp (op), and ch is pushed to the stack, read the next character. @ If icp (ch) <isp (op), exit the top operator of the stack and output this character. Note that the next character is not read here, but the elements in the stack are continuously compared. This step is specifically for the sake of brackets. @ If icp (ch) = isp (op), roll back the stack but do not output it. If the exit is '(', read the next character ch. <3> when the algorithm ends, the output sequence is the suffix expression. The following is the code: [cpp] # pragma once # include <iostream> // # include <stack> // The stack's own stack in STL can also achieve this effect # include "S_stack.h" # include <vector> # include <string> using namespace std; class Calculator {private: string expression_str; // string vector <char> char_elem; // The character container converted from the infix to the suffix dynamically increases the Stack <float> stk_float; // void Do_Operator (char opt_ch), the core variable of the floating point Stack used for the suffix calculation expression ); // calculate bool Get_operands (float & left _ Optnum, float & right_optnum); // retrieves the Left and Right operands from the stack for calculating bool is_numelem (char ch); // determines whether the operand float exchange_num (char num_ch ); // convert numeric characters to numeric void exchange_exp (); // The expression converted from infix to suffix is stored in the character array, so that int Get_isp (char opt_ch) can be computed with the suffix ); // return the int Get_icp (char opt_ch) Priority Level in the stack of the operator; // return the void Show_result () Priority Level of the operator to be added to the stack; // return the calculation result public: calculator (string str); // The Calculator accepts a string expression ~ Calculator (); void Running (); // external computing interface}; [cpp] # include "Calculator. h "# include <iostream> using namespace std; // initialize some data for later Calculation of Calculator: Calculator (string str) {expression_str = str;} Calculator ::~ Calculator () {}// return operator's intra-stack priority int Calculator: Get_isp (char opt_ch) {switch (opt_ch) {case '#': return 0; break; case '(': return 1; break; case '*': case '/': case '%': return 5; break; case '+': case '-': return 3; break; case ')': return 6; break ;}// return the priority level int Calculator: Get_icp (char opt_ch) of the forthcoming stack operator) {switch (opt_ch) {case '#': return 0; break; case '(': return 6; break; case '*': case '/': case '%': return 4; break; case '+': case '-': return 2; break; case ')': return 1; break ;}} // determine whether it is the operand bool Calculator: is_numelem (char ch) {if (48 <= ch & ch <= 57) // The limitation here is that only four arithmetic operations within 10 can be judged and later computed return true; else return false;} // The expression converted from infix to suffix is stored in the character array, void Calculator: exchange_exp () {Stack <char> stk_char; // Stack is used to store operators and process their priority stk_char.push ('#'); // first press the standard character to stack char isp_ch; // stack character Temporary Variable string: size_type I = 0; while (stk_char.isEmpty () = false) & (I <expression_str.length () {if (is_numelem (expression_str [I]) // if the operand is output to the character container {char_elem.push_back (expression_str [I]); I ++;} else {isp_ch = stk_char.Get_topelem (); // compare the top-stack operator with the operator to be added to the stack if (Get_icp (expression_str [I])> Get_isp (isp_ch )) {stk_char.push (expression_str [I]); // if the priority of the stack to be pushed is high, the stack I ++;} else if (Get_icp (expression_str [I]) <Get_isp (isp_ch) {char_elem.push_back (stk_char.pop ();} else {if (stk_char.pop () = '(') I ++ ;}}}} // convert numeric characters to float Calculator: exchange_num (char num_ch) {switch (num_ch) {case '0': return 0; break; case '1': return 1; break; case '2': return 2; break; case '3': return 3; break; case '4': return 4; break; case '5': return 5; break; case '6': return 6; break; case '7': return 7; break; case '8': return 8; break; case '9': return 9; break ;}/// calculation function and output result void Calculator: Running () {exchange_exp (); // conversion expression for (vector <char >:: size_type I = 0; I <char_elem.size (); I ++) {switch (char_elem [I]) {// when an operator is encountered, make a judgment case '+': case '-': case '*': case '/': {Do_Operator (char_elem [I]); // Based on the operator, calculate the two elements of the stack header, and press the result to stack continue ;} // when the operator is not used, default: {if (is_numelem (char_elem [I]) // determines whether it is a numeric character {stk_float.push (exchange_num (char_elem [I]); // convert to a digital post-pressure stack continue ;} else {// when the operation ends, it indicates that there are invalid characters or undefined characters in the program. As a result, the computation cannot continue with the cout operation. <"an invalid character may cause an operation error. We recommend that you re-enter and clear the stack. check the validity of the expression "<endl; exit (1) ;}}// parentheses after the switch statement} // Show_result () after the for structure (); // display result} // obtain the Left and Right operands from the stack for calculating bool Calculator: Get_operands (float & left_optnum, float & right_optnum) {if (stk_float.isEmpty ()) // judge stack null {cout <"missing right operand" <endl; return false;} right_optnum = stk_float.pop (); // obtain the right operand if (stk_float.isEmpty ()) // retrieve a right operand and make another judgment {cout <"left operand missing" <endl; return false;} left_optnum = stk_float.pop (); // retrieve the left operand return true; // return true} // calculate void Calculator: Do_Operator (char opt_ch) {float left, right according to the operator to get two elements in the stack, value; if (Get_operands (left, right) // extract the first two elements from the stack {switch (opt_ch) {case '+': {value = left + right; stk_float.push (value); // after adding, the result is the stack break;} case '-': {value = left-right; stk_float.push (value); // subtract the stack break ;} case '*': {value = left * right; stk_float.push (value); // multiply the pressure stack break;} case '/': if (right = 0) {cout <"/operator right operand is 0" <endl; exit (1);} else {value = left/right; stk_float.push (value ); // remove the pressure stack} break ;}}www.2cto.com//reserve and output the final result void Calculator: Show_result () {cout <"expression Calculation Result:" <endl; cout <expression_str <"=" <stk_float.Get_topelem (); // The top element of the stack is the computing result after the calculation ends}