标签:form dex cas private i++ pac append 包括 public
ackage chapter;
import java.util.ArrayList;import java.util.List;import java.util.Stack;public class Master { private static String[] op = { "+", "-", "*", "/" }; public static List<String> returnUserInNum(int a){ List<String> ac=new ArrayList(); int i=1; while(i<=a){ String question=MakeFormula(); ac.add(question); i++; } return ac; } //算式 public static String MakeFormula(){ StringBuilder build = new StringBuilder(); int count = (int) (Math.random() * 2) + 1; // 生成1-3之间的随机整数(不包括3) int start = 0; int number1 = (int) (Math.random() * 99) + 1;//生成1-100之间的随机整数(不包括100) build.append(number1); while (start <= count){ int operation = (int) (Math.random() * 3); //生成0-3之间的随机整数(不包括3) int number2 = (int) (Math.random() * 99) + 1;//生成1-100之间的随机整数(不包括100) build.append(op[operation]).append(number2); start ++; } return build.toString(); } public static String Solve(String formula){ Stack<String> tempStack = new Stack<>(); Stack<Character> operatorStack = new Stack<>(); int len = formula.length(); int k = 0; for(int j = -1; j < len - 1; j++){ char formulaChar = formula.charAt(j + 1); if(j == len - 2 || formulaChar == ‘+‘ || formulaChar == ‘-‘ || formulaChar == ‘/‘ || formulaChar == ‘*‘) { if (j == len - 2) { tempStack.push(formula.substring(k)); } else { if(k < j){ tempStack.push(formula.substring(k, j + 1)); } if(operatorStack.empty()){ operatorStack.push(formulaChar); }else{ char stackChar = operatorStack.peek(); if ((stackChar == ‘+‘ || stackChar == ‘-‘) && (formulaChar == ‘*‘ || formulaChar == ‘/‘)){ operatorStack.push(formulaChar); }else { tempStack.push(operatorStack.pop().toString()); operatorStack.push(formulaChar); } } } k = j + 2; } } while (!operatorStack.empty()){ tempStack.push(operatorStack.pop().toString()); } Stack<String> calcStack = new Stack<>(); for(String peekChar : tempStack){ if(!peekChar.equals("+") && !peekChar.equals("-") && !peekChar.equals("/") && !peekChar.equals("*")) { calcStack.push(peekChar); }else{ int a1 = 0; int b1 = 0; if(!calcStack.empty()){ b1 = Integer.parseInt(calcStack.pop()); } if(!calcStack.empty()){ a1 = Integer.parseInt(calcStack.pop()); } switch (peekChar) { case "+": calcStack.push(String.valueOf(a1 + b1)); break; case "-": calcStack.push(String.valueOf(a1 - b1)); break; case "*": calcStack.push(String.valueOf(a1 * b1)); break; default: calcStack.push(String.valueOf(a1 / b1)); break; } } } return formula + "=" + calcStack.pop();标签:form dex cas private i++ pac append 包括 public
原文地址:https://www.cnblogs.com/wangxinyuo/p/14644925.html