二柱子与他的计算题
代码:
public class Caculate03 { private static int grade; private static int number = 30; private static int range; private static String[] operator = new String[100]; private static int operatorNumber; private static int[] randomOperator = new int[100]; private static int[] randomNumber = new int[100]; private static String[] sum = new String[100]; private static String[] formula = new String[100]; private static String[] formula2 = new String[100]; private static String[] errorQuestionKey = new String[100]; private static int error = 0;//错题数量 private static int correct = 0;//正确数量 public static void Initialize() { Scanner sc = new Scanner(System.in); System.out.print("请输入出题个数:"); number = sc.nextInt(); System.out.print("请输入操作数取值范围:"); range = sc.nextInt(); } //打印 public static void print() { for (int i = 0; i < number; i++) { System.out.println(formula[i]); } } //生成随机数 public static void generateRandomNumber() { long seed = System.currentTimeMillis(); Random r = new Random(seed); for (int i = 0; i < number*operatorNumber; i++) { randomNumber[i] = r.nextInt(range); } } //生成四则运算 public static void generateOperation() { Random r = new Random(); for (int i = 0; i < (operatorNumber-1)*number; i++) { randomOperator[i] = r.nextInt(4); switch (randomOperator[i]) { case 0: operator[i] = "+"; break; case 1: operator[i] = "-"; break; case 2: operator[i] = "*"; break; case 3: operator[i] = "/"; break; default: System.out.println("Error1!"); break; } } } //组合 public static void combination() { int n = 0, k =0; Random r = new Random(); for (int i = 0; i < operatorNumber * number; i = i+operatorNumber){ for (int j = i; j < i+operatorNumber; j++) { if (j != (i+operatorNumber-1)) { while (true) { if ("/".equals(operator[n])) { if (randomNumber[j + 1] == 0 || randomNumber[j] % randomNumber[j + 1] != 0) { randomNumber[j + 1] = r.nextInt(range); randomNumber[j] = r.nextInt(range); } else { break; } } else { break; } } if (j == i){ formula[k] = String.valueOf(randomNumber[j])+operator[n]; formula2[k] = String.valueOf(randomNumber[j])+operator[n]; } else { formula[k] += String.valueOf(randomNumber[j])+operator[n]; formula2[k] += String.valueOf(randomNumber[j])+operator[n]; } n++; } else if(j == i+operatorNumber-1) { formula[k] += String.valueOf(randomNumber[j]); formula2[k] += String.valueOf(randomNumber[j]); } } if (grade == 4){ formula[k] = bracket(formula[k]); } k++; } } public static String bracket(String formula) { long seed = System.currentTimeMillis(); Random r = new Random(seed); int leftBracket, rightBracket; int a, b; String formula2=""; while(true){ a = r.nextInt(operatorNumber-2)+1; b = r.nextInt(operatorNumber-2)+1; if (a>b){ leftBracket = b; rightBracket = a; break; } else if (a<b){ leftBracket = a; rightBracket = b; break; } else { continue; } } int k=0; String temp=""; String[] numberStack = new String[operatorNumber]; String[] operatorStack = new String[operatorNumber]; operatorStack[0] = "#"; for (int i = 0; i < formula.length(); i++) { if (formula.charAt(i) != '+' && formula.charAt(i) != '-' && formula.charAt(i) != '*' && formula.charAt(i) != '/' && i != formula.length() - 1) { temp += formula.charAt(i); continue; } else if (i == formula.length() - 1) { temp += formula.charAt(i); numberStack[k] = temp; } else { numberStack[k] = temp; operatorStack[k + 1] = String.valueOf(formula.charAt(i)); temp = ""; k++; } } formula2 += numberStack[0]; for (int i = 1; i < operatorNumber; i++) { if (i == leftBracket){ formula2 += operatorStack[i]+"("+numberStack[i]; } else if (i == rightBracket){ formula2 += operatorStack[i]+numberStack[i]+")"; } else { formula2 += operatorStack[i]+numberStack[i]; } } return formula2; } //计算 public static void caculate() { if (grade == 4){ for (int i = 0; i < number; i++) { sum[i] = caculateString(formula[i]); } } else{ for (int i = 0; i < number; i++) { sum[i] = caculateString(formula[i]); } } } //计算一个字符串算式 @NotNull public static String caculateString(@NotNull String formula) { int k=0; int n=1; String temp=""; //给定两个栈,一个存放数字,一个存放运算符 int[] numberStack = new int[operatorNumber]; int[] numberStack2 = new int[operatorNumber]; String[] operatorStack = new String[operatorNumber]; String[] operatorStack2 = new String[operatorNumber]; operatorStack[0] = "#"; for (int i = 0; i < formula.length(); i++) { if (formula.charAt(i) != '+' && formula.charAt(i) != '-' && formula.charAt(i) != '*' && formula.charAt(i) != '/' && i != formula.length() - 1 &&formula.charAt(i)!='('&&formula.charAt(i)!=')') { temp += formula.charAt(i); continue; } else if (i == formula.length() - 1) { temp += formula.charAt(i); numberStack[k] = Integer.parseInt(temp); } else { numberStack[k] = Integer.parseInt(temp); operatorStack[k + 1] = String.valueOf(formula.charAt(i)); temp = ""; k++; } } operatorStack2[0] = operatorStack[0]; numberStack2[0] = numberStack[0]; for (int i = 1; i < operatorNumber; i++) { if ("*".equals(operatorStack[i])||"/".equals(operatorStack[i])) { switch (operatorStack[i]) { case "*": numberStack2[n-1] = numberStack[i]*numberStack2[n-1]; break; case "/": numberStack2[n-1] = numberStack2[n-1]/numberStack[i]; break; default: System.out.println("Error2!"); break; } } else { operatorStack2[n] = operatorStack[i]; numberStack2[n] = numberStack[i]; n++; } } int i; for (i = 1; i < n; i++) { switch (operatorStack2[i]) { case "+": numberStack2[i] = numberStack2[i]+numberStack2[i-1]; break; case "-": numberStack2[i] = numberStack2[i-1]-numberStack2[i]; break; default: System.out.println("Error3!"); break; } } return String.valueOf(numberStack2[n-1]); } public static String caculateString02(String formula){ String formula2=""; String temp=""; String result; for (int i = 0; i < formula.length(); i++) { if (formula.charAt(i) == '('){ for (int j = i+1; j < formula.length(); j++) { if (formula.charAt(j) != ')'){ formula2 += String.valueOf(formula.charAt(j)); } else { i = j; break; } } System.out.println(formula2); formula2 = caculateString(formula2); temp += formula2; }else { temp += String.valueOf(formula.charAt(i)); } } result = caculateString(temp); return result; } //在线答题 public static void answerQuesetion() throws IOException { String result; Scanner sc = new Scanner(System.in); for (int i = 0; i < number; i++) { System.out.print((i+1)+". "+formula[i]+"="); result = sc.nextLine(); if (result.equals(sum[i])){ correct++; } else { /* 如果用户输入答案不正确,则将题目录入至错题库 */ generateWrongQuestion(formula[i]); errorQuestionKey[error] = sum[i]; error++; } } System.out.println("正确"+correct+",错误"+error); System.out.println("------------------------------------"); System.out.println(" 正确答案"); for (int i = 0; i < number; i++) { System.out.println(" "+(i+1)+". "+sum[i]); } System.out.println("------------------------------------"); } //讲题目录入至错题库 public static void generateWrongQuestion(String formula) throws IOException { OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\WrongTopicSet.txt")); osw.write(formula); osw.flush(); osw.close(); } /* 功能: 1.将存入粗题库中的错题进行再次显示 2.重做 */ public static void printWrongQuestion() throws IOException { String result; String line; int i = 0; Scanner sc = new Scanner(System.in); BufferedReader br = new BufferedReader(new FileReader("E:\\WrongTopicSet.txt")); while((line = br.readLine()) != null) { System.out.println(line); result = sc.nextLine(); if (result.equals(errorQuestionKey[i])) { System.out.println("回答正确,你真的太棒了"); } else { System.out.println("回答错误,要继续努力哦"); } i++; } } public static void inputOperatorNumber(int n) { Scanner sc = new Scanner(System.in); Random r = new Random(); if (n == 2){ operatorNumber = 2; } else { System.out.print("请选择操作数个数:"); operatorNumber = sc.nextInt(); } } public static void main(String[] args) throws IOException { char choice = 'Y'; boolean bool = true; Scanner sc = new Scanner(System.in); Initialize(); System.out.print("请输入年级:"); grade = sc.nextInt(); //选择操作数量 inputOperatorNumber(grade); //生成计算题 generateRandomNumber(); generateOperation(); combination(); caculate();//生成计算题后计算答案 //System.out.println("这里有没有问题呢?"); while(true){ if (choice == 'Y' || choice == 'y') { generateRandomNumber(); generateOperation(); combination(); caculate(); answerQuesetion(); } else if(choice == 'n' || choice == 'N') { printWrongQuestion(); } else { System.exit(0); } System.out.println("------------------------------------"); System.out.println(" Y or y 进行下一套题训练"); System.out.println(" N or n 进行错题训练"); System.out.println(" 其他任意键 退出"); System.out.println("------------------------------------"); System.out.print("请选择:"); choice = sc.next().charAt(0); System.out.println("------------------------------------"); } } }