diff --git a/doc/#说明.md b/doc/#说明.md new file mode 100644 index 0000000..601d3fa --- /dev/null +++ b/doc/#说明.md @@ -0,0 +1,23 @@ +#说明 + +##Windows环境编译步骤 + +1.打开命令提示符(cmd) + +2.进入项目src文件夹 + +3.执行以下命令: + +chcp 65001 + +javac *.java + +##注意事项 + +1.chcp 65001 命令用于设置控制台编码为 UTF-8,确保中文正常显示 + +2.必须在执行编译命令前设置编码 + +##运行程序 + +java Main \ No newline at end of file diff --git a/src/AccountManager.java b/src/AccountManager.java index cb26a91..b6f4d72 100644 --- a/src/AccountManager.java +++ b/src/AccountManager.java @@ -1,37 +1,37 @@ -import java.util.HashMap; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; public class AccountManager { - private Map users; + private List users; public AccountManager() { initializeUsers(); } private void initializeUsers() { - users = new HashMap<>(); + users = new ArrayList<>(); - addUser("张三1", "123", UserType.PRIMARY); - addUser("张三2", "123", UserType.PRIMARY); - addUser("张三3", "123", UserType.PRIMARY); + users.add(new User("张三1", "123", UserType.PRIMARY)); + users.add(new User("张三2", "123", UserType.PRIMARY)); + users.add(new User("张三3", "123", UserType.PRIMARY)); - addUser("李四1", "123", UserType.JUNIOR); - addUser("李四2", "123", UserType.JUNIOR); - addUser("李四3", "123", UserType.JUNIOR); + users.add(new User("李四1", "123", UserType.JUNIOR)); + users.add(new User("李四2", "123", UserType.JUNIOR)); + users.add(new User("李四3", "123", UserType.JUNIOR)); - addUser("王五1", "123", UserType.SENIOR); - addUser("王五2", "123", UserType.SENIOR); - addUser("王五3", "123", UserType.SENIOR); - } - - private void addUser(String username, String password, UserType type) { - users.put(username + ":" + password, new User(username, password, type)); + users.add(new User("王五1", "123", UserType.SENIOR)); + users.add(new User("王五2", "123", UserType.SENIOR)); + users.add(new User("王五3", "123", UserType.SENIOR)); } public User authenticate(String username, String password) { - String key = username + ":" + password; - return users.get(key); + for (User user : users) { + if (user.getUsername().equals(username) && user.getPassword().equals(password)) { + return user; + } + } + return null; } public boolean isValidUserType(String typeName) { diff --git a/src/Filemanager.java b/src/Filemanager.java index c1e0461..f05aaac 100644 --- a/src/Filemanager.java +++ b/src/Filemanager.java @@ -60,27 +60,16 @@ public class Filemanager { for (File file : files) { try (BufferedReader reader = new BufferedReader(new FileReader(file))) { String line; - StringBuilder question = new StringBuilder(); - boolean inQuestion = false; - while ((line = reader.readLine()) != null) { + line = line.trim(); + if (line.isEmpty()) continue; + + // 提取题目内容(去除题号) if (line.matches("^\\d+\\.\\s.*")) { - if (inQuestion) { - allQuestions.add(question.toString().trim()); - question.setLength(0); - } - String questionContent = line.substring(line.indexOf(". ") + 2); - question.append(questionContent); - inQuestion = true; - } else if (inQuestion && !line.trim().isEmpty()) { - question.append(" ").append(line.trim()); + String question = line.substring(line.indexOf(". ") + 2).trim(); + allQuestions.add(question); } } - - if (inQuestion && question.length() > 0) { - allQuestions.add(question.toString().trim()); - } - } catch (IOException e) { System.err.println("读取文件失败: " + file.getName()); } @@ -88,4 +77,25 @@ public class Filemanager { return allQuestions; } + + + public void clearUserHistory(String username) { + String userDirPath = BASE_DIR + File.separator + username; + File userDir = new File(userDirPath); + + if (userDir.exists() && userDir.isDirectory()) { + File[] files = userDir.listFiles(); + if (files != null) { + int count = 0; + for (File file : files) { + if (file.delete()) { + count++; + } + } + System.out.println("已删除 " + count + " 个历史文件"); + } + } else { + System.out.println("没有找到历史文件"); + } + } } \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index bad3bb0..a42fa04 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,5 @@ import java.util.Scanner; - public class Main { private AccountManager accountManager; private PaperGenerator paperGenerator; @@ -84,8 +83,10 @@ public class Main { continue; } + System.out.println("正在生成试卷,请稍候..."); String[] questions = paperGenerator.generatePaper(questionCount, fileManager); fileManager.savePaper(currentUser.getUsername(), questions); + System.out.println("试卷生成成功!"); } catch (NumberFormatException e) { System.out.println("请输入有效的数字(10-30或-1)"); @@ -118,7 +119,6 @@ public class Main { app.run(); } catch (Exception e) { System.err.println("程序出现异常: " + e.getMessage()); - e.printStackTrace(); } } } \ No newline at end of file diff --git a/src/PaperGenerator.java b/src/PaperGenerator.java index 604c6da..71803ab 100644 --- a/src/PaperGenerator.java +++ b/src/PaperGenerator.java @@ -1,16 +1,17 @@ import java.util.HashSet; +import java.util.Random; import java.util.Set; public class PaperGenerator { private IQuestionGenerator questionGenerator; - private Set generatedQuestions; private User currentUser; + private Random random; public PaperGenerator(User user) { this.currentUser = user; this.questionGenerator = QuestionGeneratorFactory.createGenerator(user.getUserType()); - this.generatedQuestions = new HashSet<>(); + this.random = new Random(); } public void setUserType(UserType userType) { @@ -22,27 +23,99 @@ public class PaperGenerator { throw new IllegalArgumentException("题目数量必须在10-30之间"); } - String[] questions = new String[questionCount]; Set allHistoryQuestions = fileManager.loadAllQuestions(currentUser.getUsername()); - generatedQuestions.addAll(allHistoryQuestions); + System.out.println("历史题目数量: " + allHistoryQuestions.size()); + + String[] questions = new String[questionCount]; + Set usedQuestions = new HashSet<>(); for (int i = 0; i < questionCount; i++) { String question; int attempt = 0; - int maxAttempts = 100; + int maxAttempts = 500; do { question = questionGenerator.generateQuestion(); attempt++; + + if (attempt > 100) { + question = generateSimpleQuestion(); + } + if (attempt >= maxAttempts) { - throw new RuntimeException("无法生成不重复的题目,请尝试清理历史文件"); + System.out.println("警告:第 " + (i+1) + " 题尝试次数过多,使用备用题目"); + question = generateFallbackQuestion(); + break; } - } while (generatedQuestions.contains(question) || !questionGenerator.isValidQuestion(question)); + + } while (allHistoryQuestions.contains(question) || + usedQuestions.contains(question) || + !questionGenerator.isValidQuestion(question)); - generatedQuestions.add(question); + usedQuestions.add(question); questions[i] = (i + 1) + ". " + question; + System.out.println("成功生成第 " + (i+1) + " 题"); } return questions; } + + + private String generateSimpleQuestion() { + switch (currentUser.getUserType()) { + case PRIMARY: + return generateSimplePrimaryQuestion(); + case JUNIOR: + return generateSimpleJuniorQuestion(); + case SENIOR: + return generateSimpleSeniorQuestion(); + default: + return generateSimplePrimaryQuestion(); + } + } + + private String generateSimplePrimaryQuestion() { + int a = random.nextInt(100) + 1; + int b = random.nextInt(100) + 1; + String[] ops = {"+", "-", "*", "/"}; + String op = ops[random.nextInt(ops.length)]; + return a + " " + op + " " + b + " = "; + } + + private String generateSimpleJuniorQuestion() { + int a = random.nextInt(100) + 1; + int b = random.nextInt(100) + 1; + String[] ops = {"+", "-", "*", "/"}; + String op = ops[random.nextInt(ops.length)]; + + if (random.nextBoolean()) { + return "√" + a + " " + op + " " + b + " = "; + } else { + return a + "² " + op + " " + b + " = "; + } + } + + private String generateSimpleSeniorQuestion() { + int a = random.nextInt(100) + 1; + int angle = random.nextInt(360) + 1; + String[] ops = {"+", "-", "*", "/"}; + String op = ops[random.nextInt(ops.length)]; + String[] trig = {"sin", "cos", "tan"}; + String trigFunc = trig[random.nextInt(trig.length)]; + + return trigFunc + "(" + angle + "°) " + op + " " + a + " = "; + } + + private String generateFallbackQuestion() { + switch (currentUser.getUserType()) { + case PRIMARY: + return (random.nextInt(100) + 1) + " + " + (random.nextInt(100) + 1) + " = "; + case JUNIOR: + return "√" + (random.nextInt(100) + 1) + " + " + (random.nextInt(100) + 1) + " = "; + case SENIOR: + return "sin(" + (random.nextInt(360) + 1) + "°) + " + (random.nextInt(100) + 1) + " = "; + default: + return (random.nextInt(100) + 1) + " + " + (random.nextInt(100) + 1) + " = "; + } + } } \ No newline at end of file diff --git a/src/QuestionGenerator.java b/src/QuestionGenerator.java index b24762d..9579bc6 100644 --- a/src/QuestionGenerator.java +++ b/src/QuestionGenerator.java @@ -1,123 +1,102 @@ import java.util.Random; - interface IQuestionGenerator { String generateQuestion(); boolean isValidQuestion(String question); } - class PrimaryQuestionGenerator implements IQuestionGenerator { private Random random = new Random(); private String[] operators = {"+", "-", "*", "/"}; @Override public String generateQuestion() { - int operandCount = random.nextInt(3) + 2; - StringBuilder question = new StringBuilder(); - - question.append(random.nextInt(100) + 1); - - for (int i = 1; i < operandCount; i++) { - String operator = operators[random.nextInt(operators.length)]; - int operand = random.nextInt(100) + 1; + try { + int operandCount = random.nextInt(3) + 2; + StringBuilder question = new StringBuilder(); - if (random.nextBoolean() && i < operandCount - 1) { - question.append(" ").append(operator).append(" (").append(operand); - if (random.nextBoolean()) { - question.append(" ").append(operators[random.nextInt(operators.length)]) - .append(" ").append(random.nextInt(100) + 1).append(")"); - i++; - } - } else { + question.append(random.nextInt(100) + 1); + + for (int i = 1; i < operandCount; i++) { + String operator = operators[random.nextInt(operators.length)]; + int operand = random.nextInt(100) + 1; question.append(" ").append(operator).append(" ").append(operand); } + + return question.toString() + " = "; + } catch (Exception e) { + return (random.nextInt(100) + 1) + " + " + (random.nextInt(100) + 1) + " = "; } - - String result = question.toString(); - int openCount = countChar(result, '('); - int closeCount = countChar(result, ')'); - while (openCount > closeCount) { - result += ")"; - closeCount++; - } - - return result + " = "; } @Override public boolean isValidQuestion(String question) { - return question.matches("^[0-9+\\-*/().\\s]+=$") && - !question.contains("²") && !question.contains("√") && - !question.contains("sin") && !question.contains("cos") && !question.contains("tan"); - } - - private int countChar(String str, char ch) { - int count = 0; - for (char c : str.toCharArray()) { - if (c == ch) count++; + try { + return question != null && + question.contains("=") && + !question.contains("²") && + !question.contains("√") && + !question.contains("sin") && + !question.contains("cos") && + !question.contains("tan"); + } catch (Exception e) { + return false; } - return count; } } - class JuniorQuestionGenerator implements IQuestionGenerator { private Random random = new Random(); private String[] operators = {"+", "-", "*", "/"}; @Override public String generateQuestion() { - StringBuilder question = new StringBuilder(); - int operandCount = random.nextInt(3) + 2; - boolean hasSpecialOperator = false; - - question.append(random.nextInt(100) + 1); - - for (int i = 1; i < operandCount; i++) { - String operator; - if (!hasSpecialOperator && random.nextDouble() < 0.4) { - if (random.nextBoolean()) { - operator = "²"; - hasSpecialOperator = true; - } else { - operator = "√"; - hasSpecialOperator = true; - } - } else { - operator = operators[random.nextInt(operators.length)]; - } + try { + StringBuilder question = new StringBuilder(); - int operand = random.nextInt(100) + 1; - - if (operator.equals("²")) { - question.append("²"); - } else if (operator.equals("√")) { - question.append(" ").append(operator).append(operand); - } else { - question.append(" ").append(operator).append(" ").append(operand); - } - } - - if (!hasSpecialOperator) { if (random.nextBoolean()) { - question.append("²"); + question.append("√").append(random.nextInt(100) + 1); } else { - question.append(" √").append(random.nextInt(100) + 1); + question.append(random.nextInt(100) + 1).append("²"); } + + int extraOps = random.nextInt(2) + 1; + for (int i = 0; i < extraOps; i++) { + String operator = operators[random.nextInt(operators.length)]; + int operand = random.nextInt(100) + 1; + + if (random.nextDouble() < 0.3) { + if (random.nextBoolean()) { + question.append(" ").append(operator).append(" √").append(operand); + } else { + question.append(" ").append(operator).append(" ").append(operand).append("²"); + } + } else { + question.append(" ").append(operator).append(" ").append(operand); + } + } + + return question.toString() + " = "; + } catch (Exception e) { + return "√1 + 1 = "; } - - return question.toString() + " = "; } @Override public boolean isValidQuestion(String question) { - return (question.contains("²") || question.contains("√")) && - !question.contains("sin") && !question.contains("cos") && !question.contains("tan"); + try { + return question != null && + question.contains("=") && + (question.contains("²") || question.contains("√")) && + !question.contains("sin") && + !question.contains("cos") && + !question.contains("tan"); + } catch (Exception e) { + return false; + } } } - class SeniorQuestionGenerator implements IQuestionGenerator { private Random random = new Random(); private String[] operators = {"+", "-", "*", "/"}; @@ -125,50 +104,44 @@ class SeniorQuestionGenerator implements IQuestionGenerator { @Override public String generateQuestion() { - StringBuilder question = new StringBuilder(); - int operandCount = random.nextInt(3) + 2; - boolean hasTrigFunction = false; - - if (random.nextDouble() < 0.6) { + try { + StringBuilder question = new StringBuilder(); + String trigFunc = trigFunctions[random.nextInt(trigFunctions.length)]; - int angle = random.nextInt(360) + 1; - question.append(trigFunc).append("(").append(angle).append("°)"); - hasTrigFunction = true; - } else { - question.append(random.nextInt(100) + 1); - } - - for (int i = 1; i < operandCount; i++) { - String operator = operators[random.nextInt(operators.length)]; + question.append(trigFunc).append("(").append(random.nextInt(360) + 1).append("°)"); - if (!hasTrigFunction && random.nextDouble() < 0.5) { - String trigFunc = trigFunctions[random.nextInt(trigFunctions.length)]; - int angle = random.nextInt(360) + 1; - question.append(" ").append(operator).append(" ").append(trigFunc).append("(").append(angle).append("°)"); - hasTrigFunction = true; - } else { - int operand = random.nextInt(100) + 1; - question.append(" ").append(operator).append(" ").append(operand); + int extraOps = random.nextInt(2) + 1; + for (int i = 0; i < extraOps; i++) { + String operator = operators[random.nextInt(operators.length)]; + + if (random.nextDouble() < 0.4) { + trigFunc = trigFunctions[random.nextInt(trigFunctions.length)]; + question.append(" ").append(operator).append(" ").append(trigFunc) + .append("(").append(random.nextInt(360) + 1).append("°)"); + } else { + int operand = random.nextInt(100) + 1; + question.append(" ").append(operator).append(" ").append(operand); + } } + + return question.toString() + " = "; + } catch (Exception e) { + return "sin(45°) + 1 = "; } - - if (!hasTrigFunction) { - String trigFunc = trigFunctions[random.nextInt(trigFunctions.length)]; - int angle = random.nextInt(360) + 1; - question.append(" ").append(operators[random.nextInt(operators.length)]) - .append(" ").append(trigFunc).append("(").append(angle).append("°)"); - } - - return question.toString() + " = "; } @Override public boolean isValidQuestion(String question) { - return question.contains("sin") || question.contains("cos") || question.contains("tan"); + try { + return question != null && + question.contains("=") && + (question.contains("sin") || question.contains("cos") || question.contains("tan")); + } catch (Exception e) { + return false; + } } } - class QuestionGeneratorFactory { public static IQuestionGenerator createGenerator(UserType userType) { switch (userType) { diff --git a/src/数学试卷/张三1/2025-09-29-09-37-51.txt b/src/数学试卷/张三1/2025-09-29-09-37-51.txt new file mode 100644 index 0000000..f479d3c --- /dev/null +++ b/src/数学试卷/张三1/2025-09-29-09-37-51.txt @@ -0,0 +1,19 @@ +1. 19 - 21 = + +2. 38 * 82 - 34 - 22 = + +3. 46 * 34 * 47 = + +4. 23 + 38 / 49 - 19 = + +5. 76 - 62 / 48 + 52 = + +6. 40 * 78 * 27 * 10 = + +7. 17 / 44 - 89 = + +8. 53 + 68 = + +9. 54 / 41 = + +10. 63 / 98 = diff --git a/src/数学试卷/张三1/2025-09-29-09-38-13.txt b/src/数学试卷/张三1/2025-09-29-09-38-13.txt new file mode 100644 index 0000000..d40b855 --- /dev/null +++ b/src/数学试卷/张三1/2025-09-29-09-38-13.txt @@ -0,0 +1,59 @@ +1. 10 + 100 - 35 / 52 = + +2. 11 * 4 / 55 = + +3. 84 * 63 / 76 = + +4. 55 - 51 = + +5. 28 * 94 / 8 - 53 = + +6. 25 - 3 + 97 * 84 = + +7. 76 - 7 + 30 = + +8. 58 - 80 = + +9. 60 + 69 / 33 - 70 = + +10. 69 * 59 / 70 + 80 = + +11. 24 + 90 + 1 = + +12. 95 / 18 = + +13. 17 * 5 / 70 = + +14. 87 + 16 - 74 * 10 = + +15. 80 + 71 = + +16. 100 * 72 = + +17. 95 / 100 * 30 + 98 = + +18. 51 + 60 = + +19. 78 + 35 - 63 / 36 = + +20. 89 / 22 = + +21. 25 / 9 = + +22. 13 * 87 + 92 * 29 = + +23. 92 / 3 = + +24. 20 * 21 - 72 = + +25. 18 + 6 = + +26. 56 / 23 = + +27. 24 * 51 * 60 = + +28. 73 + 34 / 92 = + +29. 65 - 47 = + +30. 48 / 52 + 51 / 20 = diff --git a/src/数学试卷/张三1/2025-09-29-09-39-05.txt b/src/数学试卷/张三1/2025-09-29-09-39-05.txt new file mode 100644 index 0000000..e137ff3 --- /dev/null +++ b/src/数学试卷/张三1/2025-09-29-09-39-05.txt @@ -0,0 +1,39 @@ +1. tan(100°) / 14 - 87 = + +2. cos(231°) - 55 * 14 = + +3. sin(283°) / cos(56°) / 94 = + +4. sin(6°) - 2 = + +5. tan(69°) / 56 = + +6. cos(272°) + 25 = + +7. cos(211°) * 73 - 54 = + +8. cos(337°) / 57 = + +9. cos(282°) * cos(357°) = + +10. tan(126°) + 84 = + +11. cos(307°) - 7 = + +12. cos(210°) / 60 = + +13. sin(281°) / 44 = + +14. sin(296°) + 23 = + +15. cos(82°) * 96 = + +16. tan(353°) - 12 * 84 = + +17. tan(144°) * 26 = + +18. tan(354°) - 83 * 51 = + +19. tan(225°) * 63 = + +20. sin(128°) + sin(124°) + tan(79°) = diff --git a/src/数学试卷/张三1/2025-09-29-15-27-37.txt b/src/数学试卷/张三1/2025-09-29-15-27-37.txt new file mode 100644 index 0000000..38ed8ba --- /dev/null +++ b/src/数学试卷/张三1/2025-09-29-15-27-37.txt @@ -0,0 +1,19 @@ +1. 16 - 55 * 15 - 100 = + +2. 34 / 75 = + +3. 62 + 10 = + +4. 98 + 83 = + +5. 96 / 92 + 41 + 67 = + +6. 92 / 98 * 61 / 2 = + +7. 51 - 25 / 54 + 61 = + +8. 79 + 97 = + +9. 28 - 27 + 96 = + +10. 24 + 73 = diff --git a/src/数学试卷/张三1/2025-09-29-15-28-00.txt b/src/数学试卷/张三1/2025-09-29-15-28-00.txt new file mode 100644 index 0000000..123fa64 --- /dev/null +++ b/src/数学试卷/张三1/2025-09-29-15-28-00.txt @@ -0,0 +1,39 @@ +1. √76 + √53 = + +2. √19 / 17 + 85 = + +3. 3² + 98 = + +4. 100² / 6² + 2 = + +5. 19² + 99 = + +6. √75 + √60 = + +7. √22 * 20 = + +8. √42 * 22 = + +9. 84² - 51² / 87 = + +10. √62 * 18² + 40 = + +11. √73 + 65 = + +12. 55² * 75 = + +13. √56 + 34² = + +14. 63² + 94 / 27 = + +15. 77² / √78 * 16 = + +16. 14² * 55² = + +17. 82² * 11 = + +18. √69 * 98 = + +19. √47 / 24 / 72 = + +20. 54² - 84 =