The calculator implemented in Java, the principle is read as code comments, the specific content is as follows
public class MyCalculator { public static void main(String[] args) { String obj = "6+(8/2)+6/3+1*8 + 30"; ArrayList<String> arrayList = strFormat(obj); System.out.println(obj + "=" + calculate(arrayList)); } /** * Use subsequent expressions to calculate the result* 1. When the current string is a number, directly enter the stack* 2. When the current string is a calculator, take out the first two calculations on the stack* 3. Put the calculation result into the stack, and the last remaining element in the stack is the required result*/ private static int calculate(ArrayList<String> obj) { ArrayList<String> result = transform(obj); System.out.println(result); Stack<Integer> stack = new Stack<>(); for (int i = 0; i < result.size(); i++) { String symbol = result.get(i); if (isDigital(symbol)) { //The number is directly stack.push(Integer.parseInt(symbol)); } else { //The operator int num1, num2; num1 = stack.pop(); //Take out two numbers num2 = stack.pop(); switch (symbol) { case "+": stack.push(num2 + num1); break; case "-": stack.push(num2 - num1); break; case "*": stack.push(num2 * num1); break; case "/": stack.push(num2 / num1); break; default: break; } } } return stack.pop(); } /** * In-order traversal is changed to subsequent traversal*/ private static ArrayList<String> transform(ArrayList<String> arrayList) { Stack<String> stack = new Stack<>(); ArrayList<String> result = new ArrayList<>(); for (int index = 0; index < arrayList.size(); index++) { String symbol = arrayList.get(index); if (isDigital(symbol)) { //If it is a number, it is output directly.add(symbol); } else if (symbol.equals(")")) { String tmp; while (!(tmp = stack.pop()).equals("(")) { // Stop after the match is successful result.add(tmp); } } else { if (stack.isEmpty()) { stack.push(symbol); continue; } String tmp = stack.peek(); while (outPriority(symbol) <= inPriority(tmp)) { //The priority is less than the priority in the stack, and the stack is always released result.add(tmp); stack.pop(); if (stack.isEmpty()) { break; } tmp = stack.peek(); } stack.push(symbol); } } //Put the remaining stack while (!stack.isEmpty()) { result.add(stack.pop()); } return result; } /** * First format the String into ArrayList * @param src 3*5+8; * @return ArrayList 3 * 5 + 8 */ private static ArrayList<String> strFormat(String src) { if (src == null || src.equals("")) { return null; } ArrayList<String> arrayList = new ArrayList<>(); StringBuilder comChar = new StringBuilder(); for (int i = 0; i <src.length(); i++) { char ch = src.charAt(i); if (ch == ' ') { continue; //Remove spaces} if (!Char.isDigit(ch)) { if (!comChar.toString().trim().equals(")) { arrayList.add(comChar.toString().trim()); comChar.delete(0, comChar.length()); } arrayList.add(ch + ""); continue; } comChar.append(ch); } if (!comChar.toString().trim().equals("")) { arrayList.add(comChar.toString().trim()); } return arrayList; } /** * Determine whether it is a number* @param symbol 782 or + - * / * @return true or false */ private static boolean isDigital(String symbol) { return !symbol.equals("+") && !symbol.equals("-") && !symbol.equals("*") && !symbol.equals("/") && !symbol.equals("(") && !symbol.equals(")"); } private static int inPriority(String ch) { switch (ch) { case "+": case "-": return 2; case "*": case "/": return 4; case ")": return 7; case "(": return 1; default: return 0; } } private static int outPriority(String ch) { switch (ch) { case "+": case "-": return 3; case "*": case "/": return 5; case ")": return 1; case "(": return 7; default: return 0; } }}All the above are the entire content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.