From 8e52c179c7c05252e9eb493952acc04ccd15287d Mon Sep 17 00:00:00 2001 From: jzb <2386255140@qq.com> Date: Fri, 26 Sep 2025 15:40:49 +0800 Subject: [PATCH] first --- .gitignore | 30 +++++++++ .idea/.gitignore | 8 +++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 +++ exams/张三1/2025-09-26-14-58-39.txt | 19 ++++++ exams/张三1/2025-09-26-14-58-46.txt | 29 ++++++++ math_question/README.md | 2 + src/DifficultyLevel.java | 24 +++++++ src/FileManager.java | 66 ++++++++++++++++++ src/LoginSystem.java | 62 +++++++++++++++++ src/Main.java | 74 +++++++++++++++++++++ src/Question.java | 4 ++ src/QuestionGenerator.java | 96 +++++++++++++++++++++++++++ src/User.java | 17 +++++ untitled.iml | 11 +++ 15 files changed, 456 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 exams/张三1/2025-09-26-14-58-39.txt create mode 100644 exams/张三1/2025-09-26-14-58-46.txt create mode 100644 math_question/README.md create mode 100644 src/DifficultyLevel.java create mode 100644 src/FileManager.java create mode 100644 src/LoginSystem.java create mode 100644 src/Main.java create mode 100644 src/Question.java create mode 100644 src/QuestionGenerator.java create mode 100644 src/User.java create mode 100644 untitled.iml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..13275f1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,30 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ +.kotlin + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..a7cdac7 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3372485 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a8db667 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/exams/张三1/2025-09-26-14-58-39.txt b/exams/张三1/2025-09-26-14-58-39.txt new file mode 100644 index 0000000..141bfc4 --- /dev/null +++ b/exams/张三1/2025-09-26-14-58-39.txt @@ -0,0 +1,19 @@ +1. 34 / 30 = + +2. ((44) - 97 - 68) * 15 = + +3. 63 / 81 = + +4. 96 = + +5. ((36) - 18 + 35) = + +6. 76 - 52 = + +7. (34 * 99 / 50) - 77 = + +8. (57) - 51 + 90 = + +9. (((4) * 9) + 15) / 6 = + +10. ((20 - 11) + 95 * 39) = diff --git a/exams/张三1/2025-09-26-14-58-46.txt b/exams/张三1/2025-09-26-14-58-46.txt new file mode 100644 index 0000000..1b94675 --- /dev/null +++ b/exams/张三1/2025-09-26-14-58-46.txt @@ -0,0 +1,29 @@ +1. 21 = + +2. 61 / 51 = + +3. ((98) / 56 - 89) + 50 = + +4. (58) - 1 + 74 = + +5. 62 = + +6. 44 - 62 - 47 = + +7. (63 / 26 + 92) = + +8. 21 = + +9. ((54) * 43 - 14) = + +10. (44 / 75) / 9 / 71 / 43 = + +11. 76 / 45 = + +12. ((((61) + 17 - 94) - 19) / 33) = + +13. 38 = + +14. (94 / 22 / 27 + 70 - 40) = + +15. 81 - 48 = diff --git a/math_question/README.md b/math_question/README.md new file mode 100644 index 0000000..2c014ba --- /dev/null +++ b/math_question/README.md @@ -0,0 +1,2 @@ +# math_question + diff --git a/src/DifficultyLevel.java b/src/DifficultyLevel.java new file mode 100644 index 0000000..b549d7f --- /dev/null +++ b/src/DifficultyLevel.java @@ -0,0 +1,24 @@ +public enum DifficultyLevel { + PRIMARY("小学"), + JUNIOR("初中"), + SENIOR("高中"); + + private final String displayName; + + DifficultyLevel(String displayName) { + this.displayName = displayName; + } + + public String getDisplayName() { + return displayName; + } + + public static DifficultyLevel fromString(String text) { + for (DifficultyLevel level : DifficultyLevel.values()) { + if (level.displayName.equals(text)) { + return level; + } + } + return null; + } +} \ No newline at end of file diff --git a/src/FileManager.java b/src/FileManager.java new file mode 100644 index 0000000..9900915 --- /dev/null +++ b/src/FileManager.java @@ -0,0 +1,66 @@ +import java.io.*; +import java.nio.file.*; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashSet; +import java.util.Set; + +public class FileManager { + private static final String BASE_DIR = "exams"; + + public boolean saveQuestions(User user, String[] questions) { + try { + // 创建用户目录 + Path userDir = Paths.get(BASE_DIR, user.getUsername()); + Files.createDirectories(userDir); + + // 生成文件名 + String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()); + Path filePath = userDir.resolve(timestamp + ".txt"); + + // 写入文件 + try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(filePath))) { + for (int i = 0; i < questions.length; i++) { + writer.println(questions[i]); + if (i < questions.length - 1) { + writer.println(); // 题目之间空一行 + } + } + } + + return true; + } catch (IOException e) { + e.printStackTrace(); + return false; + } + } + + public Set loadExistingQuestions(String username) { + Set questions = new HashSet<>(); + Path userDir = Paths.get(BASE_DIR, username); + + if (!Files.exists(userDir)) { + return questions; + } + + try (DirectoryStream stream = Files.newDirectoryStream(userDir, "*.txt")) { + for (Path filePath : stream) { + try (BufferedReader reader = Files.newBufferedReader(filePath)) { + String line; + while ((line = reader.readLine()) != null) { + if (line.trim().isEmpty() || !line.contains(".")) { + continue; + } + // 提取题目内容(去掉题号) + String question = line.substring(line.indexOf(".") + 1).trim(); + questions.add(question); + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + + return questions; + } +} \ No newline at end of file diff --git a/src/LoginSystem.java b/src/LoginSystem.java new file mode 100644 index 0000000..ae93435 --- /dev/null +++ b/src/LoginSystem.java @@ -0,0 +1,62 @@ +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + +public class LoginSystem { + private Map users; + + public LoginSystem() { + initializeUsers(); + } + + private void initializeUsers() { + users = new HashMap<>(); + + // 小学账户 + users.put("张三1", new User("张三1", "123", DifficultyLevel.PRIMARY)); + users.put("张三2", new User("张三2", "123", DifficultyLevel.PRIMARY)); + users.put("张三3", new User("张三3", "123", DifficultyLevel.PRIMARY)); + + // 初中账户 + users.put("李四1", new User("李四1", "123", DifficultyLevel.JUNIOR)); + users.put("李四2", new User("李四2", "123", DifficultyLevel.JUNIOR)); + users.put("李四3", new User("李四3", "123", DifficultyLevel.JUNIOR)); + + // 高中账户 + users.put("王五1", new User("王五1", "123", DifficultyLevel.SENIOR)); + users.put("王五2", new User("王五2", "123", DifficultyLevel.SENIOR)); + users.put("王五3", new User("王五3", "123", DifficultyLevel.SENIOR)); + } + + public User login(Scanner scanner) { + while (true) { + System.out.print("请输入用户名和密码(用空格隔开):"); + String input = scanner.nextLine().trim(); + String[] parts = input.split("\\s+"); + + if (parts.length != 2) { + System.out.println("请输入正确的用户名、密码"); + continue; + } + + String username = parts[0]; + String password = parts[1]; + + User user = users.get(username); + if (user != null && user.getPassword().equals(password)) { + return user; + } else { + System.out.println("请输入正确的用户名、密码"); + } + } + } + + public DifficultyLevel switchLevel(String levelName) { + DifficultyLevel newLevel = DifficultyLevel.fromString(levelName); + if (newLevel == null) { + System.out.println("请输入小学、初中和高中三个选项中的一个"); + return null; + } + return newLevel; + } +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..9f9933b --- /dev/null +++ b/src/Main.java @@ -0,0 +1,74 @@ +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + LoginSystem loginSystem = new LoginSystem(); + QuestionGenerator generator = new QuestionGenerator(); + FileManager fileManager = new FileManager(); + + while (true) { + // 登录阶段 + User currentUser = loginSystem.login(scanner); + if (currentUser == null) { + continue; + } + + System.out.println("当前选择为" + currentUser.getLevel().getDisplayName() + "出题"); + + // 主循环 + while (true) { + System.out.println("准备生成" + currentUser.getLevel().getDisplayName() + + "数学题目,请输入生成题目数量(输入-1将退出当前用户,重新登录)"); + + String input = scanner.nextLine().trim(); + + // 检查切换命令 + if (input.startsWith("切换为")) { + String levelName = input.substring(3).trim(); + DifficultyLevel newLevel = loginSystem.switchLevel(levelName); + if (newLevel != null) { + currentUser.setLevel(newLevel); + System.out.println("准备生成" + currentUser.getLevel().getDisplayName() + + "数学题目,请输入生成题目数量"); + continue; + } + } + + // 检查退出命令 + if (input.equals("-1")) { + System.out.println("退出当前用户,重新登录..."); + break; + } + + // 生成题目 + try { + int count = Integer.parseInt(input); + if (count < 10 || count > 30) { + System.out.println("题目数量应在10-30之间"); + continue; + } + + // 生成题目并检查重复 + String[] questions = generator.generateQuestions(currentUser.getLevel(), count, + currentUser.getUsername()); + if (questions == null) { + System.out.println("生成题目失败,可能存在重复题目"); + continue; + } + + // 保存文件 + boolean success = fileManager.saveQuestions(currentUser, questions); + if (success) { + System.out.println("题目生成成功!"); + } else { + System.out.println("文件保存失败"); + } + + } catch (NumberFormatException e) { + System.out.println("请输入有效的数字"); + } + } + } + } +} \ No newline at end of file diff --git a/src/Question.java b/src/Question.java new file mode 100644 index 0000000..ae414c9 --- /dev/null +++ b/src/Question.java @@ -0,0 +1,4 @@ +public interface Question { + String generateQuestion(); + boolean isValid(); +} \ No newline at end of file diff --git a/src/QuestionGenerator.java b/src/QuestionGenerator.java new file mode 100644 index 0000000..46d690f --- /dev/null +++ b/src/QuestionGenerator.java @@ -0,0 +1,96 @@ +import java.util.HashSet; +import java.util.Random; +import java.util.Set; + +public class QuestionGenerator { + private Random random; + private FileManager fileManager; + + public QuestionGenerator() { + random = new Random(); + fileManager = new FileManager(); + } + + public String[] generateQuestions(DifficultyLevel level, int count, String username) { + Set existingQuestions = fileManager.loadExistingQuestions(username); + Set newQuestions = new HashSet<>(); + String[] questions = new String[count]; + + for (int i = 0; i < count; i++) { + String question; + int attempts = 0; + + do { + question = generateSingleQuestion(level, i + 1); + attempts++; + if (attempts > 100) { + return null; // 避免无限循环 + } + } while (existingQuestions.contains(question) || newQuestions.contains(question)); + + newQuestions.add(question); + questions[i] = question; + } + + return questions; + } + + private String generateSingleQuestion(DifficultyLevel level, int questionNumber) { + int operandCount = random.nextInt(5) + 1; // 1-5个操作数 + StringBuilder question = new StringBuilder(questionNumber + ". "); + + switch (level) { + case PRIMARY: + question.append(generatePrimaryQuestion(operandCount)); + break; + case JUNIOR: + question.append(generateJuniorQuestion(operandCount)); + break; + case SENIOR: + question.append(generateSeniorQuestion(operandCount)); + break; + } + + question.append(" = "); + return question.toString(); + } + + private String generatePrimaryQuestion(int operandCount) { + String[] operators = {"+", "-", "*", "/"}; + StringBuilder question = new StringBuilder(); + + for (int i = 0; i < operandCount; i++) { + if (i > 0) { + question.append(" ").append(operators[random.nextInt(operators.length)]).append(" "); + } + question.append(random.nextInt(100) + 1); + + // 随机添加括号(小学难度) + if (operandCount > 2 && random.nextDouble() < 0.3) { + question.insert(0, "(").append(")"); + } + } + + return question.toString(); + } + + private String generateJuniorQuestion(int operandCount) { + String question = generatePrimaryQuestion(operandCount); + + // 确保至少有一个平方或开根号 + if (random.nextBoolean()) { + return "√" + (random.nextInt(100) + 1) + " + " + question; + } else { + return "(" + (random.nextInt(10) + 1) + ")² + " + question; + } + } + + private String generateSeniorQuestion(int operandCount) { + String question = generatePrimaryQuestion(operandCount); + String[] trigFunctions = {"sin", "cos", "tan"}; + + // 确保至少有一个三角函数 + return trigFunctions[random.nextInt(trigFunctions.length)] + + "(" + (random.nextInt(90) + 1) + "°) + " + question; + } +} \ No newline at end of file diff --git a/src/User.java b/src/User.java new file mode 100644 index 0000000..fa199e4 --- /dev/null +++ b/src/User.java @@ -0,0 +1,17 @@ +public class User { + private String username; + private String password; + private DifficultyLevel level; + + public User(String username, String password, DifficultyLevel level) { + this.username = username; + this.password = password; + this.level = level; + } + + // Getter 和 Setter 方法 + public String getUsername() { return username; } + public String getPassword() { return password; } + public DifficultyLevel getLevel() { return level; } + public void setLevel(DifficultyLevel level) { this.level = level; } +} \ No newline at end of file diff --git a/untitled.iml b/untitled.iml new file mode 100644 index 0000000..9465dd8 --- /dev/null +++ b/untitled.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file