标签:char 表达 rect -name null 目的 标记 rpg red
需求分析:
功能设计:
1.在控制台输入题目的数量及生成数的范围
2.在文本中记录所有生成的题目
3.在文本中记录每道题对应的正确答案
4.把正确答案和做题者输入的答案进行对比,在文本中计算出正确和错误的题目数量并标记题号
实现:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.Reader; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Scanner; public class Test{ public static int zf = 0; public static int f = 0; public static int fm = 0; public static void main(String[] args) { String[] form= new String[10000]; form[0] = "4"; form[1] = "+"; form[2] = "13"; String result; result=result(form); System.out.print(result); } public static String result(String[] eq){ //算式计算 int eqnum=10; String res = new String(); String[] RPG = new String[10000]; boolean control = true; if (eqnum >= 1){ if (eq[1] != "/" && eq[1] != "*"){ for (int s = 3; s < arraylength(eq); s = s + 2){ if (eq[s] == "*" || eq[s] == "/"){ control = false; String[] RPG2 = new String[10000]; for (int i = 0; i < arraylength(eq) - 2; i++) { RPG2[i] = eq[s - 1 + i]; } for (int i = 0; i < arraylength(eq) - 2; i++){ if (i < s - 1){ RPG[i] = eq[i]; } else if (i == s - 1){ eqnum = 0; RPG[i] = result(RPG2); } else { RPG[i] = eq[i + 2]; } if (i == arraylength(eq) - 3){ eqnum = 0; res = result(RPG); } } } else { if (control == true){ String[] RPG2 = new String[10000]; for (int i = 0; i < arraylength(eq) - 2; i++){ RPG2[i] = eq[i]; } eqnum = 0; for (int i = 0; i < arraylength(eq) - 2; i++){ if (i == 0){ RPG[i] = result(RPG2); } else { RPG[i] = eq[i + 2]; } if (i == arraylength(eq) - 3){ res = result(RPG); } } } } } } else{ if (control == true){ String[] RPG2 = new String[10000]; for (int i = 0; i < 3; i++){ RPG2[i] = eq[i]; } eqnum = 0; for (int i = 0; i < arraylength(eq) - 2; i++){ if (i == 0){ RPG[i] = result(RPG2); } else { RPG[i] = eq[i + 2]; } if (i == arraylength(eq) - 3){ res = result(RPG); } } } } } else { int con0 = 0; int con2 = 0; for (int i = 0; i < eq[0].length(); i++){ if (eq[0].charAt(i) == ‘/‘){ con0 = 1; } } for (int i = 0; i < eq[2].length(); i++){ if (eq[2].charAt(i) == ‘/‘){ con2 = 1; } } if (con0 == 0 && con2 == 0){ switch (eq[1]) { case "+": res = Integer.toString(Integer.parseInt(eq[0]) + Integer.parseInt(eq[2])); break; case "-": res = Integer.toString(Integer.parseInt(eq[0]) - Integer.parseInt(eq[2])); break; case "*": res = Integer.toString(Integer.parseInt(eq[0]) * Integer.parseInt(eq[2])); break; case "/": zf = Integer.parseInt(eq[0]) / Integer.parseInt(eq[2]); f = Integer.parseInt(eq[0]) % Integer.parseInt(eq[2]); fm = Integer.parseInt(eq[2]); if (Integer.parseInt(eq[0]) >= Integer.parseInt(eq[2])){ if (f != 0){ int k = maxmath(f, Integer.parseInt(eq[2])); f = f / k; res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + Integer.parseInt(eq[2]) / k; } else { res = Integer.toString(zf); } } else { int k = maxmath(f, Integer.parseInt(eq[2])); f = f / k; res = Integer.toString(f) + "/" + Integer.parseInt(eq[2]) / k; } break; default: break; } } else if (con0 == 1 && con2 == 0){ int fz = zf * fm + f; switch (eq[1]) { case "+": fz = fz + Integer.parseInt(eq[2]) * fm; zf = fz / fm; f = fz % fm; if (zf == 0 && f != 0){ int k = maxmath(fz, fm); fz = fz / k; fm = fm / k; res = Integer.toString(fz) + "/" + fm; } else if (zf != 0 && f == 0){ res = Integer.toString(zf); } else if (zf != 0 && f != 0){ if (f > 0){ int k = maxmath(f, fm); f = f / k; fm = fm / k; res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fm; } else { int k = maxmath(f, fm); f = f / k; fm = fm / k; res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fm; } } break; case "-": fz = fz - Integer.parseInt(eq[2]) * fm; zf = fz / fm; f = fz % fm; if (fz == 0){ res = "0"; } else if (zf == 0 && f != 0){ int k = maxmath(fz, fm); fz = fz / k; fm = fm / k; res = Integer.toString(fz) + "/" + fm; } else if (zf != 0 && f == 0){ res = Integer.toString(zf); } else if (zf != 0 && f != 0){ if (f > 0){ int k = maxmath(f, fm); f = f / k; fm = fm / k; res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fm; } else { int k = maxmath(f, fm); f = f / k; fm = fm / k; res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fm; } } break; case "*": fz = fz * Integer.parseInt(eq[2]); zf = fz / fm; f = fz % fm; if (zf == 0 && f != 0){ int k = maxmath(fz, fm); fz = fz / k; fm = fm / k; res = Integer.toString(fz) + "/" + fm; } else if (zf != 0 && f == 0){ res = Integer.toString(zf); } else if (zf != 0 && f != 0){ if (f > 0){ int k = maxmath(f, fm); f = f / k; fm = fm / k; res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fm; } else { int k = maxmath(f, fm); f = f / k; fm = fm / k; res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fm; } } break; case "/": fm = Integer.parseInt(eq[2]) * fm; zf = fz / fm; f = fz % fm; if (zf == 0 && f != 0){ int k = maxmath(fz, fm); fz = fz / k; fm = fm / k; res = Integer.toString(fz) + "/" + fm; } else if (zf != 0 && f == 0){ res = Integer.toString(zf); } else if (zf != 0 && f != 0){ if (f > 0){ int k = maxmath(f, fm); f = f / k; fm = fm / k; res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fm; } else { int k = maxmath(f, fm); f = f / k; fm = fm / k; res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fm; } } break; default: break; } } else if (con0 == 0 && con2 == 1){ int fz = zf * fm + f; switch (eq[1]) { case "+": fz = Integer.parseInt(eq[0]) * fm +fz; zf = fz / fm; f = fz % fm; if (zf == 0 && f != 0){ int k = maxmath(fz, fm); fz = fz / k; fm = fm / k; res = Integer.toString(fz) + "/" + fm; } else if (zf != 0 && f == 0){ res = Integer.toString(zf); } else if (zf != 0 && f != 0){ if (f > 0){ int k = maxmath(f, fm); f = f / k; fm = fm / k; res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fm; } else { int k = maxmath(f, fm); f = f / k; fm = fm / k; res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fm; } } break; case "-": fz = Integer.parseInt(eq[0]) * fm - fz; zf = fz / fm; f = fz % fm; if (fz == 0){ res = "0"; } else if (zf == 0 && f != 0){ int k = maxmath(fz, fm); fz = fz / k; fm = fm / k; res = Integer.toString(fz) + "/" + fm; } else if (zf != 0 && f == 0){ res = Integer.toString(zf); } else if (zf != 0 && f != 0){ if (f > 0){ int k = maxmath(f, fm); f = f / k; fm = fm / k; res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fm; } else { int k = maxmath(f, fm); f = f / k; fm = fm / k; res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fm; } } break; case "*": fz = Integer.parseInt(eq[0]) * fz; zf = fz / fm; f = fz % fm; if (zf == 0 && f != 0){ int k = maxmath(fz, fm); fz = fz / k; fm = fm / k; res = Integer.toString(fz) + "/" + fm; } else if (zf != 0 && f == 0){ res = Integer.toString(zf); } else if (zf != 0 && f != 0){ if (f > 0){ int k = maxmath(f, fm); f = f / k; fm = fm / k; res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fm; } else { int k = maxmath(f, fm); f = f / k; fm = fm / k; res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fm; } } break; case "/": int fz1 = fz; fz = Integer.parseInt(eq[0]) * fm; zf = fz / fm; f = fz % fm; if (zf == 0 && f != 0){ int k = maxmath(fz, fm); fz = fz / k; fm = fm / k; res = Integer.toString(fz) + "/" + fm; } else if (zf != 0 && f == 0){ res = Integer.toString(zf); } else if (zf != 0 && f != 0){ if (f > 0){ int k = maxmath(f, fz1); f = f / k; fz1 = fz1 / k; res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fz1; } else { int k = maxmath(f, fz1); f = f / k; fz1 = fz1 / k; res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fz1; } } break; default: break; } } else { } } return res; } public static int arraylength(String[] array){ int j = 0; for (int i = 0; i < array.length; i++){ if (array[i] != null){ j++; } } return j; } public static int maxmath(int a,int b){ int k = 1; int math = 0; if (a < 0){ a = -a; } if (b < 0 ){ b = -b; } while (k <= a && k <= b){ if (a % k == 0 && b % k == 0){ math = k; } k++; } return math; } }
总结:
由于时间问题,目前一直在仔细研究关于算式的计算,还未完成,后续继续补
标签:char 表达 rect -name null 目的 标记 rpg red
原文地址:http://www.cnblogs.com/pjishu/p/7586662.html