Related processes
Key code Explanation
- Code that separates expressions
String[] result = num.split("\\s");
The input num is separated by a space spacing symbol, and each separated character is stored in an array named result.
-
if ("+". Equals (a) | | "-". Equals (a)) {if ("+"). Equals (b) | | "-". Equals (b) | | ")". Equals (b) | | "#". Equals (b)) {c = Optr.pop (); z = Opnd.pop (); y = Opnd.pop ();//str + = ""; str + = y; str + = ""; str + = Z; str + = ""; str + = C; str + = ""; Opnd.push (str); str = ""; if ("(". Equals (Optr.peek ()) && ")". Equals (Result[x])) {Optr.pop ();}
Here is a case where a (top element of the stack) has a precedence greater than B (input operator), the stack top of the operator stack and the operand stack header are presented, re-stitched into a new whole character into the operand stack, and the value of the equation is emptied (to prevent the new formula from being added to the previous one), The current operator is pressed into the operator stack, but when a matches B, the top element A is removed directly (preventing the suffix from being replaced with the "(") "number). (here basically implemented when a > B, a out of the stack, from OPND to pop up two operands y,x,z = x y a, and press A into the stack opnd,c optr operation)
if ("=".equals(result[x])) { while (OPND.size() != 0) { number.push(OPND.pop()); } suf += " "; suf += number.pop(); while (number.size() != 0){ suf += " "; suf += number.pop(); } while (OPTR.peek() != "#"){ suf += " "; suf += OPTR.pop(); } suf += " "; suf += "="; } }
Here I set "=" to a stop flag, when the program recognizes the "=" number, the program will be two stacks of elements to clean up, eventually stitching into a suffix. In this program, I re-pressed the elements in the stack into a new stack, which reverses the order of the elements in the stack, which makes it easy to splice later.
Difficulties encountered and their solutions
- 1: The beginning of the idea is not particularly clear, not the teacher in class to provide the method of understanding, almost in their own thinking in writing. At the beginning of the idea is that the gradual editing, first written only to convert the pure plus minus or pure multiplication number of the formula, and then expand on this basis for the subtraction mixed operation of the formula and realize the conversion, and finally with parentheses, I would like to achieve a three-step plan.
- Q: How can i separate each entry into a single character, especially the X/Y score, so that the program will have a three-element fraction grouped into one without affecting the other characters that contain only one element?
A:string There is a split method, which can be the specified character as a delimited mark, and the separated characters into an array, where I can use a space as a separator mark, and the number of true fractions and the operator is not space between the interval, so it can be achieved.
Q: The idea of pure plus or minus or pure multiplication is relatively simple, it is equivalent to the original expression of the operand and operator separately, and then re-splicing the process, which is to dig a big hole in the back, that is, upgrade the subtraction mixed operation of the code implementation, because the operator of the relative position of the different , the process of turning the suffix is obviously somewhat difficult if the coder alone anticipates all the circumstances, so I have written the extremely miscellaneous If-else statement here to judge the situation for stitching. Attach a suffix that can only be used for pure plus and minus operations
- A: I thought of the solution is, if it is such a string of expressions
1 + 2 * 3 - 4/5
, it is first through a method, the multiplication concatenated with a new character B to represent, then the expression is converted to 1 + b - 4/5
such a call to the first step of the pure plus minus operation, it can be solved.
- Q: But as the solution presented above seems feasible, it is still difficult to implement, because the first and last operators of the operator are still considered, for example, when I encounter the first multiplication sign or division sign, I use the substring method to present a whole set of equations, but when I encounter the latter multiplication sign or division sign, Make a lot of judgments, so the entire code is still very voluminous.
A: After the members of the reminder, I just re-read the teacher's lectures, found in the PPT gave a clear idea, so before it is to pay their own tuition bar.
- 2:
- Q: How do I prioritize an operator?
A: At first I want to write a compare class that defines the precedence relationships of all operators in the class, determining the relationship between the top element and the current input operator when the suffix is turned, the output "<", ">", "=" (because the problem is generated without errors, so there is no further consideration of the error). and greater than or equal to the relationship I want to use the wrapper to the top of the stack and input operations assigned int values 0 and 1, so it is easier to compare, but finally it seems more difficult to implement, and then I think of the fifth chapter of the programming Project Pp5-7 "Scissors and stone cloth" writing seems to be similar to this comparison, Then read the previous code, a clear idea, no longer write a separate class to implement, directly using the If-else statement to all the case list ( Yes, it feels step-by-point toward the beginning of the train of thought is marching. other questions about this section are explained in the explanation of the key code.
Write the part of the project that you are responsible for
Implement infix expression to postfix expression
Division of individual contribution (percentage contribution to each member of the group, contribution of each member to 100%) evaluation of small partners in pairs give a link to a small partner blog (focus on where to improve)
Give group pair programming photos
2017-2018-2 20172323 "Java Programming" course pair programming Exercises _ Arithmetic 2