Java expression Calculation
Expression calculation refers(34+2)*4*2+7
The value of the expression, because it involves parentheses and operator priority, if it is difficult to calculate directly, follow(34+2)*4*2+7
The normal order isInfix expressionTo convert the expressionSuffix expressionConvenient,(34+2)*4*2+7
The suffix expression is34 2 + 4 * 2 * 7 +
.
There are two steps to solve the expression:
1. Convert the expression to a suffix,
2. Use the suffix to evaluate the expression value.
I. first introduce the algorithm for converting an infix expression into a suffix:
First, define the priority between operators. Each operator has two priorities: Intra-stack priority and off-stack priority, which are expressed by isp and icp respectively. The following figure shows the priority table of Yin renkun's data structure, which is very accurate.
The priority table can be simply summarized as: '(' into the stack, encounter ')' out of the stack, * and/priority, and the priority between + and/is left to right, */Priority greater than +-priority <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Release/e3 + release/dfW1rG908rks/release/e3 + 6Os1PK/release + 7bBtb21xNfWt/release + release/e3 + 7P21buyosrks/release/LV39W7zqq/ 1c6q1rmjrNfuuvO9q7bBtb21xLLZ1/e3 + samples/samples + samples/C0ru49tfWt/u1vWNoPGJyIC8 + DQoyoaLI9GNoysey2df3t/placement = "brush: java; "> 1) if icp (ch)> isp (op) is used, the ch is pushed to the stack and the next character ch.2 is read.) If icp (ch)
End. The output is a suffix expression.
2. Use the suffix to evaluate the expression value.
You also need to set a stack34 2 + 4 * 2 * 7 +
For example, in the case of a number, the two numbers at the top of the stack are taken out in the case of operators and corresponding operations are performed.+
Then, 34 + 2 is calculated, and the 34 + 2 result is re-placed to the top of the stack. The number at the top of the stack is the final calculation result.
Below is a java code implementation,middleToPost()
The function converts an infix to a suffix,calculate()
Evaluate a function using a suffix:
Package leetCode; import java. util. arrayList; import java. util. stack; public class Solution {// element class, which is used to prevent numbers from being greater than 10, such as 34 + 2 class Ele {public boolean isOp; public double num; public char op ;} // convert the infix to a suffix, including +-() */number // rule: 1. When it comes to Digital Output 2. When it comes to operators, consider whether to go to the stack/3. When it comes to "(", go to the stack, and encounter ")", the operator goes out of the stack, until "(" is encountered and the latter is removed from the stack public ArrayList MiddleToPost (String s) {Stack OpStack = new Stack (); S = s. replace ("", ""); ArrayList Res = new ArrayList (); For (int I = 0; I '9' | c <'0') {ele. isOp = true; ele. op = c;} else {String str = c + ""; while (I + 1 = '0') {str = str + s. charAt (I + 1); I ++;} ele. isOp = false; ele. num = Double. parseDouble (str);} // end if (! Ele. isOp) {res. add (ele);} else {switch (ele. op) {case' (': opStack. push (ele); break; case ')': while (opStack. peek (). op! = '(') {Res. add (opStack. pop ();} opStack. pop (); break; case '/': // '/' * 'the processing method is the same as case' * ': if (opStack. isEmpty () {opStack. push (ele);} else {while (! OpStack. isEmpty () & (opStack. peek (). op = '*' | opStack. peek (). op = '/') {res. add (opStack. pop ();} opStack. push (ele);} break; default: // processing +-if (opStack. isEmpty () {opStack. push (ele);} else {while (! OpStack. isEmpty () & opStack. peek (). op! = '(') {Res. add (opStack. pop () ;}opstack. push (ele) ;}}} while (! OpStack. isEmpty () {res. add (opStack. pop ();} return res;} // evaluate public int calculate (String s) {ArrayList using a suffix expression Eles = middleToPost (s); Stack Stack = new Stack (); Double top = 0.0; for (int I = 0; I List = so. middleToPost (s); // output suffix expression for (int I = 0; I <list. size (); I ++) {if (list. get (I ). isop) = "" system. out. print (list. get (I ). op + "="); = "" else = "" system. out. print (list. get (I ). num + "=" "} =" "system. out. println (); = "" output calculation result = "" system. out. println (so. calculate (s); = ""