Java逆ポーランド計算機(中置式から後置式)



Java Reverse Polish Calculator



import java.util.ArrayList import java.util.List import java.util.Stack /** * @author Drug * @create 2020-04-18 17:39 */ public class PolandNotion { public static void main(String[] args) { String expression = '1+((2+3)*4)-5' //Convert the operation string into a collection of infix expressions List list = toInfixExpression(expression) //Traverse the set of infix expressions System.out.println(list) //Convert the infix expression set to the postfix expression set list = parseInfixExpression(list) //Traverse the set of suffix expressions System.out.println(list) //Calculation int res = calc(list) //Show results System.out.println(res) } /** * Convert infix expression to postfix expression * * @param infixExpression * @return */ public static List<String> parseInfixExpression(List<String> infixExpression) { //Symbol stack Stack<String> stack = new Stack() //Result set List<String> list = new ArrayList<>() //Traverse infixExpression, put the result //Traverse infixExpression for (String item : infixExpression) { if (item.matches('\d+')) { //This is a number list.add(item) } else if (stack.isEmpty()) { //Empty stack, directly into the stack stack.push(item) } else if ('('.equals(item)) { //'('Into the stack directly stack.push(item) } else if (')'.equals(item)) { //')' pops symbols from the stack to the list, until they encounter '(', and discard while (!'('.equals(stack.peek())) { list.add(stack.pop()) } //throw away'(' stack.pop() } else { //At this time item is the four arithmetic fu while (!stack.isEmpty() && Operation.getValue(stack.peek()) >= Operation.getValue(item)) { //The top element of the stack is greater than or equal to the item priority list.add(stack.pop()) } stack.push(item) } } //Pop the remaining symbols in the stack to the list while(!stack.isEmpty()){ list.add(stack.pop()) } return list } /** * Convert the operation string into a set of infix expressions * * @param infixExpression * @return */ public static List<String> toInfixExpression(String infixExpression) { int index = 0 char ch = ' ' String keepNum = '' List<String> list = new ArrayList<>() int length = infixExpression.length() while (index < length) { if (infixExpression.charAt(index) < 48 || infixExpression.charAt(index) > 57) { //This is the symbol list.add('' + infixExpression.charAt(index)) //index plus one index++ } else { //At this time, it is a number, first set keepNum blank keepNum = '' while (index < length) { //Take the index character of the string ch = infixExpression.charAt(index) //The index character is still a number, continue to splice if (ch >= 48 && ch <= 57) { keepNum += ch index++ } else { //The index character is not a number, jump out break } } //Add the spliced ​​keepNum to the array list.add(keepNum) } } return list } /** * Get the string collection according to the postfix expression * * @param strings * @return */ public static List<String> getList(String[] strings) { List list = new ArrayList() for (String item : strings) { list.add(item) } return list } /** * Postfix expression operation * * @param list * @return */ public static int calc(List<String> list) { Stack<String> strings = new Stack<>() int num1 = 0 int num2 = 0 int res = 0 for (String item : list) { if (item.matches('\d+')) { //item is a number, directly into the stack strings.push(item) } else { //item is a symbol, pop the top two numbers of the stack, and then the result of the operation is pushed onto the stack num2 = Integer.parseInt(strings.pop()) num1 = Integer.parseInt(strings.pop()) //Four arithmetic operations according to item if ('+'.equals(item)) { res = num1 + num2 } else if ('-'.equals(item)) { res = num1 - num2 } else if ('*'.equals(item)) { res = num1 * num2 } else if ('/'.equals(item)) { res = num1 / num2 } else { throw new RuntimeException('Incorrect operation symbol') } //The result is pushed into the stack strings.push('' + res) } } //Return result return res } } class Operation { private static int ADD = 1 private static int SUB = 1 private static int MUL = 2 private static int DIV = 2 /** * Get the priority of the operator * * @param value * @return */ public static int getValue(String value) { int res = 0 switch (value) { case ('+'): res = ADD case ('-'): res = SUB case ('*'): res = MUL case ('/'): res = DIV default: break } return res } }