I have made some improvements, and the operation of the stack is changed to the other local modifications. It has not debugged, and I will debug it next time. Now put the code out again! Power off quickly! ! ! !
/ ************************************************** ******** This algorithm uses the "Operation Symbol Priority" process to evaluate the given expression of 1.00! ------ Navybluestudio Welcome to my blog http://blog.9cbs.net/navyblue1982********************************** ****************************** / INT COMPARE (STRING); String Operate (String, String, String); Char Precede String, string;
// Implement part
// EvaluateExpression function Mainly completed expression request issues // expression is the value of the value of the value of the value INT EvaluateExpression (String * expression) {// to save the operator, OPND to save Stack Optr, Opnd; Push ( Optr, "#"); // # symbol indicates the bottom of the stack
/ / Define Temp to save temporary symbols or operands String Temp; int i = 0; Temp = Expression [i];
// Judgment If ## End the loop while (Gettop (OPTR)! = "#" || Temp! = "#") {// compare is used to compare the number of operands or operators if the operand is returned True If the operator returns false //, you don't have to pay attention to the return value of 0, because it is a 24-point game, the minimum value is 1, you can steal the lazy IF (Compare) {Push (Opnd, Temp); Temp = Expression [ i];} else // precede is used to compare the priority Switch (Precede (Gettop (Optr), Temp)) {CASE '<': Priority Low Push (Optr, Temp) of the top of the stack; Temp = Expression [ i]; Break; Case '=': // Remoded bracket process and accepts the next character String X; POP (Optr, X) Temp = expression [ i]; Break; Case '>': // Repends the arithmetic result to load Sign; String Num1, Num2; POP (OPTR, SIGN); POP (OPND, NUM1); POP (OPND, NUM2); Push (OPND, Operate (Num1, Sign, Num2)); // Find problems, convert digital to character form
Break;}} String ReturnValue = gettop (OPND); Return ATOI (RETURNVALUE): // Returns the elements of the bottom of the stack to the required results
} INT COMPARE (STRING isoperatornum) {// is compared to whether it is an operand. . The premise is that the operand is within 0 to 12 // If it is not in this range as an operator to see SELECT = ATOI (IsoPeratorNum); if (0 == SELECT) RETURN FALSE; Else Return True;}
String Operate (String Num1, String Sign, String Num2) {// Note To return to the character pattern // first convert to a numeric type and convert to a character line returns int Integer1, Integer2; Integer1 = ATOI (Num1); Integer2 = ATOI Num2); char SIGN = Sign [0]; // Define Return Return Return ReturnValue; if (' ' == SIGN) {ITOA ((Integer1 Integer2), ReturnValue, 10); Return ReturnValue;} Else IF (' - '== Sign) {ITOA (Integer1-Integer2), ReturnValue, 10); Return ReturnValue;} else if (' * '== sign) {ITOA (Integer1 * Integer2, ReturnValue, 10); Return ReturnValue; Else {ITOA (Integer1 / Integer2, ReturnValue, 10); Return ReturnValue;}} // The priority of the comparison operator This is more troubles of Char Precede (String Sign1, String Sign2) {char siGn1, Sign2; // Transferring string converted to characters SIGN1 = Sign1 [0]; SIGN2 = SIGN [0]; Switch (Sign1) {Case ' ': Case '-': Switch (Sign2) {CASE ' ': Case '- ': Case' # ': Return'> '; Case' * ': Case' / ': Case' (': Return' <';} Case' * ': Case' / ': Switch ( Sign2) {CASE ' ': Case '-': Case '*': Case '/': Case ')': Case '#': Return '>'; Case '(': Return '<';} CASE '(': Switch (Sign2) {CASE ')') ': Ret URN '='; default: return '<';} case ')': Return '>'; Case '#': switch (Sign2) {case '#': return '='; default: return '<'; }}}
INT main () {// Test Because this program uses a string form in a string, it is not possible to save a value of the two-digit value, so the result will be an error. / / Tomorrow to improve, use the form of characters or double strings to store element string temp [16] = {"12", "*", "56" "," ("," 34 ", "-", "5", ")", "#"} int m = evataExpression (TEMP); int J = m-48; std :: cout << "The value of your required expression is:" << J << std :: endl; return 0;}
Stack implementation
Class stack {public: stack (); string gettop (); bool push; bool pop (string); private: int top; int base; string str [16];}; implementation part stack :: stack () {TOP = Base = 0;} string stack :: gettop () {if (TOP == Base) Return False; Else Return Str [TOP-1];} Bool Stack :: Push (String S) {IF (TOP> 15) RETURN FALSE; ELSE IF (TOP == 15) STR [TOP] = S; Return True; Else Str [TOP ] = S; Return True;} Bool Stack :: Pop (String & S) {IF (TOP == Base) Return False; Else S = STR [TOP ---]; Return True;