diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /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..e9710cf --- /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..9ea5392 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/math_exam_generator2.iml b/math_exam_generator2.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/math_exam_generator2.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/Info.class b/src/Info.class new file mode 100644 index 0000000..6c580e7 Binary files /dev/null and b/src/Info.class differ diff --git a/src/Info.java b/src/Info.java new file mode 100644 index 0000000..9a06250 --- /dev/null +++ b/src/Info.java @@ -0,0 +1,2 @@ +public interface Info { +} diff --git a/src/JuniorQuestionGenerator.class b/src/JuniorQuestionGenerator.class new file mode 100644 index 0000000..754cb5e Binary files /dev/null and b/src/JuniorQuestionGenerator.class differ diff --git a/src/JuniorQuestionGenerator.java b/src/JuniorQuestionGenerator.java new file mode 100644 index 0000000..eaad998 --- /dev/null +++ b/src/JuniorQuestionGenerator.java @@ -0,0 +1,36 @@ +import java.util.Random; + +/** + * 初中题目生成器实现 + * 生成适合初中生的数学题目,包含平方和开根号运算 + */ +public class JuniorQuestionGenerator implements QuestionGenerator { + @Override + public String generateQuestion(StringBuilder question, Random random) { + // 确保所有初中题目都包含至少一个平方或开根号的运算符 + int num = random.nextInt(100) + 1; + boolean isSquare = random.nextBoolean(); + + if (isSquare) { + // 生成平方题 + question.append(num).append("^2"); + } else { + // 生成开根号题,确保是完全平方数 + int sqrtNum = random.nextInt(10) + 1; + num = sqrtNum * sqrtNum; + question.append("√").append(num); + } + + // 添加一些其他运算符使题目更复杂 + int complexity = random.nextInt(2) + 1; + for (int i = 0; i < complexity; i++) { + String[] operators = {"+", "-", "*"}; + String op = operators[random.nextInt(operators.length)]; + int addNum = random.nextInt(10) + 1; + question.append(" ").append(op).append(" ").append(addNum); + } + + question.append(" = ?"); + return question.toString(); + } +} \ No newline at end of file diff --git a/src/MathExamGenerator.class b/src/MathExamGenerator.class new file mode 100644 index 0000000..7a7e19e Binary files /dev/null and b/src/MathExamGenerator.class differ diff --git a/src/MathExamGenerator.java b/src/MathExamGenerator.java new file mode 100644 index 0000000..0eff0f6 --- /dev/null +++ b/src/MathExamGenerator.java @@ -0,0 +1,306 @@ +import java.io.*; +import java.nio.charset.Charset; +import java.nio.file.*; +import java.text.SimpleDateFormat; +import java.util.*; + +/** + * 中小学数学卷子自动生成程序 + * 主类,负责用户登录、题目生成和文件操作 + */ +public class MathExamGenerator { + // 预设的用户账号信息 + private static final Map USER_ACCOUNTS = new HashMap<>(); + + // 当前登录用户信息 + private static UserInfo currentUser = null; + // 已生成的题目集,用于检查重复 + private static final Set generatedQuestions = new HashSet<>(); + + private static boolean isRunning = true; + + static { + // 初始化小学、初中和高中各三个账号 + // 小学账户 + USER_ACCOUNTS.put("张三1", new UserInfo("张三1", "123", "小学")); + USER_ACCOUNTS.put("张三2", new UserInfo("张三2", "123", "小学")); + USER_ACCOUNTS.put("张三3", new UserInfo("张三3", "123", "小学")); + + // 初中账户 + USER_ACCOUNTS.put("李四1", new UserInfo("李四1", "123", "初中")); + USER_ACCOUNTS.put("李四2", new UserInfo("李四2", "123", "初中")); + USER_ACCOUNTS.put("李四3", new UserInfo("李四3", "123", "初中")); + + // 高中账户 + USER_ACCOUNTS.put("王五1", new UserInfo("王五1", "123", "高中")); + USER_ACCOUNTS.put("王五2", new UserInfo("王五2", "123", "高中")); + USER_ACCOUNTS.put("王五3", new UserInfo("王五3", "123", "高中")); + } + + public static void main(String[] args) { + // 使用BufferedReader配合InputStreamReader,明确指定GBK编码 + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(System.in, Charset.forName("GBK")))) { + + System.out.println("欢迎使用中小学数学卷子自动生成程序"); + System.out.println("请使用您的账号和密码登录(格式:用户名 密码)"); + + while (isRunning) { + if (currentUser == null) { + handleLogIn(reader); + } else { + handleRequirement(reader); + } + } + } catch (IOException e) { + System.out.println("输入输出错误:" + e.getMessage()); + } + } + public static void handleLogIn(BufferedReader reader) throws IOException { + // 用户未登录,显示登录提示 + System.out.print("请输入用户名和密码(用空格隔开,输入exit退出):"); + String input = reader.readLine(); + + if (input == null || input.trim().isEmpty()) { + return; + } else if (input.equals("exit")) { + isRunning = false; + return; + } + + String[] parts = input.split(" "); + + if (parts.length == 2) { + String username = parts[0]; + String password = parts[1]; + // 验证用户登录 + boolean loginSuccess = false; + for (Map.Entry entry : USER_ACCOUNTS.entrySet()) { + if (entry.getValue().getUsername().equals(username) && + entry.getValue().getPassword().equals(password)) { + currentUser = entry.getValue(); + loginSuccess = true; + break; + } + } + if (loginSuccess) { + System.out.println("登录成功!当前选择为" + currentUser.getUserType() + "出题"); + System.out.println("提示:您可以输入'切换为小学'、'切换为初中'或'切换为高中'来切换题目难度"); + handleUserChoice(); + loadExistingQuestions(); + } else { + System.out.println("登录失败,请输入正确的用户名和密码"); + } + } else { + System.out.println("输入格式错误,请用空格分隔用户名和密码"); + } + } + private static void handleRequirement(BufferedReader reader) throws IOException { + // 用户已登录,显示题目数量输入提示 + System.out.print("准备生成" + currentUser.getUserType() + + "数学题目,请输入生成题目数量(输入-1将退出当前用户,重新登录):"); + String input = reader.readLine(); + if (input == null || input.trim().isEmpty()) { + return; + } + // 检查是否为切换类型命令 + if (input.startsWith("切换为")) { + String targetType = input.substring(3).trim(); + if ("小学".equals(targetType) || "初中".equals(targetType) || "高中".equals(targetType)) { + currentUser.setUserType(targetType); + System.out.println("切换成功!准备生成" + currentUser.getUserType() + "数学题目,请输入生成题目数量"); + } else { + System.out.println("切换类型错误,请输入'切换为小学'、'切换为初中'或'切换为高中'"); + } + return; + } + + try { + int questionCount = Integer.parseInt(input); + + if (questionCount == -1) { + // 退出登录 + currentUser = null; + generatedQuestions.clear(); + System.out.println("已退出登录"); + } else if (questionCount >= 10 && questionCount <= 30) { + // 生成题目 + generateExam(questionCount); + } else { + System.out.println("题目数量必须在10-30之间"); + } + } catch (NumberFormatException e) { + System.out.println("请输入有效的数字"); + } + } + // 生成试卷 + private static void generateExam(int questionCount) { + List questions = generateUniqueQuestions(questionCount); + + // 保存试卷到文件 + saveExamToFile(questions); + + System.out.println("已成功生成" + questions.size() + "道" + currentUser.getUserType() + "数学题目"); + handleUserChoice(); + } + + // 生成不重复的题目 + private static List generateUniqueQuestions(int questionCount) { + List questions = new ArrayList<>(); + Random random = new Random(); + int attempts = 0; + int maxAttempts = questionCount * 3; // 最多尝试次数,避免死循环 + + while (questions.size() < questionCount && attempts < maxAttempts) { + String question = generateQuestion(); + if (!generatedQuestions.contains(question)) { + questions.add(question); + generatedQuestions.add(question); + } + attempts++; + } + + return questions; + } + + // 处理用户生成试卷后的选择 + private static void handleUserChoice() { + System.out.println("请选择:"); + System.out.println("1. 生成题目"); + System.out.println("2. 切换题目难度"); + System.out.println("3. 退出当前用户"); + System.out.println("4. 退出程序"); + + try { + BufferedReader reader = new BufferedReader( + new InputStreamReader(System.in, Charset.forName("GBK"))); + String choice = reader.readLine(); + + switch (choice) { + case "2": + handleDifficultySwitch(); + break; + case "3": + currentUser = null; + generatedQuestions.clear(); + System.out.println("已退出登录"); + break; + case "1": + break; + case "4": + isRunning = false; + break; + default: + // 继续生成题目,不需要额外操作 + break; + } + } catch (IOException e) { + System.out.println("输入错误:" + e.getMessage()); + } + } + + // 处理难度切换 + private static void handleDifficultySwitch() { + try { + BufferedReader reader = new BufferedReader( + new InputStreamReader(System.in, Charset.forName("GBK"))); + + while (true) { + System.out.print("请输入目标难度(小学/初中/高中):"); + String targetType = reader.readLine(); + + if ("小学".equals(targetType) || "初中".equals(targetType) || "高中".equals(targetType)) { + currentUser.setUserType(targetType); + System.out.println("已切换为" + targetType + "难度"); + break; + } else { + System.out.println("难度类型错误,请重新输入"); + } + } + } catch (IOException e) { + System.out.println("输入错误:" + e.getMessage()); + } + } + + // 根据用户类型获取对应的题目生成器 + private static QuestionGenerator getQuestionGenerator() { + if ("小学".equals(currentUser.getUserType())) { + return new PrimaryQuestionGenerator(); + } else if ("初中".equals(currentUser.getUserType())) { + return new JuniorQuestionGenerator(); + } else if ("高中".equals(currentUser.getUserType())) { + return new SeniorQuestionGenerator(); + } + return null; + } + + // 根据用户类型生成题目 + private static String generateQuestion() { + StringBuilder question = new StringBuilder(); + Random random = new Random(); + QuestionGenerator generator = getQuestionGenerator(); + + if (generator != null) { + return generator.generateQuestion(question, random); + } + + return "题目生成错误"; + } + + // 加载已存在的题目,避免重复 + private static void loadExistingQuestions() { + if (currentUser == null) return; + try { + String userDir = currentUser.getUsername(); + File dir = new File(userDir); + + if (dir.exists() && dir.isDirectory()) { + File[] files = dir.listFiles(); + if (files != null) { + for (File file : files) { + if (file.isFile() && file.getName().endsWith(".txt")) { + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(new FileInputStream(file), Charset.forName("GBK")))) { + String line; + while ((line = reader.readLine()) != null) { + line = line.trim(); + if (!line.isEmpty() && !line.matches("^\\d+\\.")) { + generatedQuestions.add(line); + } + } + } + } + } + } + } + } catch (IOException e) { + System.out.println("加载已存在题目时出错:" + e.getMessage()); + } + } + + // 保存试卷到文件 + private static void saveExamToFile(List questions) { + try { + // 创建用户目录(相对路径) + String userDir = currentUser.getUsername(); + Files.createDirectories(Paths.get(userDir)); + + // 生成文件名:年-月-日-时-分-秒.txt + String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()); + String filePath = userDir + File.separator + timestamp + ".txt"; + + try (BufferedWriter writer = new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(filePath), Charset.forName("GBK")))) { + for (int i = 0; i < questions.size(); i++) { + writer.write((i + 1) + ". " + questions.get(i)); + writer.newLine(); + if (i < questions.size() - 1) { + writer.newLine(); // 题目之间空一行 + } + } + } + } catch (IOException e) { + System.out.println("保存试卷时出错:" + e.getMessage()); + } + } +} \ No newline at end of file diff --git a/src/PrimaryQuestionGenerator.class b/src/PrimaryQuestionGenerator.class new file mode 100644 index 0000000..cd819b7 Binary files /dev/null and b/src/PrimaryQuestionGenerator.class differ diff --git a/src/PrimaryQuestionGenerator.java b/src/PrimaryQuestionGenerator.java new file mode 100644 index 0000000..a846199 --- /dev/null +++ b/src/PrimaryQuestionGenerator.java @@ -0,0 +1,41 @@ +import java.util.Random; + +/** + * 小学题目生成器实现 + * 生成适合小学生的数学题目 + */ +public class PrimaryQuestionGenerator implements QuestionGenerator { + @Override + public String generateQuestion(StringBuilder question, Random random) { + int num1 = random.nextInt(100) + 1; // 1-100的随机数 + int num2 = random.nextInt(100) + 1; + String[] operators = {"+", "-", "*", "/"}; + String op = operators[random.nextInt(operators.length)]; + + // 确保减法和除法结果为正整数 + if ("-".equals(op) && num1 < num2) { + int temp = num1; + num1 = num2; + num2 = temp; + } else if ("/".equals(op)) { + // 确保除法结果为整数 + num1 = num2 * (random.nextInt(10) + 1); + } + + // 10%的概率添加括号 + if (random.nextDouble() < 0.1) { + question.append("("); + question.append(num1); + question.append(" ").append(op).append(" "); + question.append(num2); + question.append(")"); + } else { + question.append(num1); + question.append(" ").append(op).append(" "); + question.append(num2); + } + + question.append(" = ?"); + return question.toString(); + } +} \ No newline at end of file diff --git a/src/QuestionGenerator.class b/src/QuestionGenerator.class new file mode 100644 index 0000000..bac5f6d Binary files /dev/null and b/src/QuestionGenerator.class differ diff --git a/src/QuestionGenerator.java b/src/QuestionGenerator.java new file mode 100644 index 0000000..1f99877 --- /dev/null +++ b/src/QuestionGenerator.java @@ -0,0 +1,15 @@ +import java.util.Random; + +/** + * 题目生成器接口 + * 定义生成数学题目的基本方法 + */ +public interface QuestionGenerator { + /** + * 生成一道数学题目 + * @param question 用于构建题目的StringBuilder + * @param random 随机数生成器 + * @return 生成的题目字符串 + */ + String generateQuestion(StringBuilder question, Random random); +} \ No newline at end of file diff --git a/src/SeniorQuestionGenerator.class b/src/SeniorQuestionGenerator.class new file mode 100644 index 0000000..5204465 Binary files /dev/null and b/src/SeniorQuestionGenerator.class differ diff --git a/src/SeniorQuestionGenerator.java b/src/SeniorQuestionGenerator.java new file mode 100644 index 0000000..d7d2de6 --- /dev/null +++ b/src/SeniorQuestionGenerator.java @@ -0,0 +1,29 @@ +import java.util.Random; + +/** + * 高中题目生成器实现 + * 生成适合高中生的数学题目,包含三角函数运算 + */ +public class SeniorQuestionGenerator implements QuestionGenerator { + @Override + public String generateQuestion(StringBuilder question, Random random) { + // 确保所有高中题目都包含至少一个sin、cos或tan的运算符 + String[] functions = {"sin(", "cos(", "tan("}; + String func = functions[random.nextInt(functions.length)]; + int angle = random.nextInt(180); + + question.append(func).append(angle).append(")"); + + // 添加一些其他运算符使题目更复杂 + int complexity = random.nextInt(3); + for (int i = 0; i < complexity; i++) { + String[] operators = {"+", "-", "*", "/"}; + String op = operators[random.nextInt(operators.length)]; + int num = random.nextInt(10) + 1; + question.append(" ").append(op).append(" " ).append(num); + } + + question.append(" = ?"); + return question.toString(); + } +} \ No newline at end of file diff --git a/src/UserInfo.class b/src/UserInfo.class new file mode 100644 index 0000000..0820244 Binary files /dev/null and b/src/UserInfo.class differ diff --git a/src/UserInfo.java b/src/UserInfo.java new file mode 100644 index 0000000..0abbd06 --- /dev/null +++ b/src/UserInfo.java @@ -0,0 +1,53 @@ +/** + * 用户信息类 + * 存储用户的基本信息,包括用户名、密码和用户类型 + */ +public class UserInfo implements Info { + private String username; + private String password; + private String userType; + + /** + * 构造函数 + * @param username 用户名 + * @param password 密码 + * @param userType 用户类型(小学/初中/高中) + */ + public UserInfo(String username, String password, String userType) { + this.username = username; + this.password = password; + this.userType = userType; + } + + /** + * 获取用户名 + * @return 用户名 + */ + public String getUsername() { + return username; + } + + /** + * 获取密码 + * @return 密码 + */ + public String getPassword() { + return password; + } + + /** + * 获取用户类型 + * @return 用户类型 + */ + public String getUserType() { + return userType; + } + + /** + * 设置用户类型 + * @param userType 用户类型 + */ + public void setUserType(String userType) { + this.userType = userType; + } +} \ No newline at end of file diff --git a/src/张三2/2025-09-24-12-24-30.txt b/src/张三2/2025-09-24-12-24-30.txt new file mode 100644 index 0000000..5112caa --- /dev/null +++ b/src/张三2/2025-09-24-12-24-30.txt @@ -0,0 +1,39 @@ +1. 1 * 34 = ? + +2. 30 + 93 = ? + +3. 82 * 33 = ? + +4. 98 * 97 = ? + +5. 58 * 77 = ? + +6. 97 - 96 = ? + +7. 86 - 27 = ? + +8. 73 - 22 = ? + +9. 52 + 5 = ? + +10. 42 - 6 = ? + +11. 72 * 62 = ? + +12. 60 - 23 = ? + +13. 43 - 37 = ? + +14. 198 / 99 = ? + +15. 4 / 1 = ? + +16. 42 + 57 = ? + +17. 356 / 89 = ? + +18. 144 / 36 = ? + +19. 189 / 21 = ? + +20. 74 + 43 = ?