James, who opened the hotel in the Software Park, was very depressed recently. He often encountered errors during account verification, and his computing results were always biased. After knowing this, Mr. Wang planned to solve this problem for James. After investigation, he found that the problem occurred on the calculator. The calculation method of the current calculator is mostly: input data, input operators, and then input data, the calculation result is provided immediately, and the loop continues. At the end of the calculation, if there is a deviation or data, you cannot review which error occurred. If you need to re-check the error, you need to enter it all again, which is very time-consuming. Mr. Wang intends to help the boss design a new calculator that supports expression calculation. because it is the first version, he only needs to support the integer "+,-, *" and "() ", of course, the operation Priority inside the brackets is higher than that outside the brackets, and" * "is higher than" +.
Note: the length of the input expression string is less than 20. The expression must be a valid expression.
- Required implementation functions:
Void calculate (char * input, int * rel)
[Input] char * input, the expression to be calculated
[Output] int * Rel, Calculation Result
[Return] None
1) input: input = 5 + 2-10*2 + 5
Output: rel =-8
Programming ideology:
In case of parentheses, the number in the brackets is calculated using stack2. For arithmetic operations, stack is used to find the number between two parentheses for calculation.
# Include <iostream> # include <stack> # include <sstream> # include <stack> using namespace STD; void calculate (char * input, int * rel) {unsigned Len = strlen (input); string S (input, input + Len); S. insert (Len, ")"); S. insert (0, "("); Len + = 2; stack <string> stack1, stack2; For (unsigned I = 0; I <Len ;) {If (s [I] = '(' | s [I] = '*' | s [I] = '-' | s [I] = '+ ') {string S1 (S, I, 1); stack1.push (S1); I ++ ;} else if (s [I]> = '0' & S [I] <= '9') // because the input number may be greater than 9, it is a two-digit or three-digit {unsigned start = I; I ++; while (s [I]> = '0' & S [I] <= '9') I ++; unsigned end = I; string S1 (S, start, end-Start); stack1.push (S1);} else if (s [I] = ') // pop the number in the brackets from stack1, use stack2 to calculate the result and push it to stack1 {I ++; // I points to the next character in S, while (stack1.top ()! = "(") // Extract the previous number in stack1 "(", and remove the "*" number in the process, put the retrieved number in stack2 {If (stack1.top ()! = "*") {Stack2.push (stack1.top (); stack1.pop ();} else // If "*" is encountered, convert string to int to calculate the result, convert it to string and put it in stack2 {stack1.pop (); // pop off "*" string B = stack2.top (); string a = stack1.top (); stack1.pop (); stack2.pop (); int D1, D2; istringstream is1 (A), is2 (B); is1> d1; is2> D2; int res = D1 * D2; ostringstream OS; OS <res; string Re = OS. STR (); stack2.push (re) ;}// end while stack1.pop (); // pop off "(" while (! Stack2.empty () // clear the number in stack2 and press the result into stack1 {If (stack2.size () = 1) {stack1.push (stack2.top ()); stack2.pop (); break;} string a = stack2.top (); // retrieve the first operand A stack2.pop (); string F = stack2.top (); // "+" or "-" stack2.pop (); string B = stack2.top (); // obtain the second operand B stack2.pop (); int D1, D2; istringstream is1 (A), is2 (B); is1> d1; is2> D2; int res; If (F = "+") RES = D1 + D2; else res = d1-d2; ostringstream OS; OS <res; string RESS = OS. STR (); stack2.push (RESS);} // end while} // end else if} // end for string resss = stack1.top (); istringstream ISSS (resss ); ISSS> (* rel);} void main () {char * P = "(50*(1 + 2*3-4) + 2) -8*(1 + 2) "; cout <(50*(1 + 2*3-4) + 2)-8*(1 + 2) <Endl; int * rel = new int; Calculate (p, rel); cout <* rel <Endl ;}