1 #1

Merged
hnu202326010331 merged 3 commits from develop into main 4 months ago

@ -0,0 +1,24 @@
public enum DifficultyLevel {
PRIMARY("小学"),
JUNIOR("初中"),
SENIOR("高中");
private String chineseName;
DifficultyLevel(String chineseName) {
this.chineseName = chineseName;
}
public String getChineseName() {
return chineseName;
}
public static DifficultyLevel fromChineseName(String name) {
for (DifficultyLevel level : values()) {
if (level.chineseName.equals(name)) {
return level;
}
}
return null;
}
}

@ -0,0 +1,23 @@
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileManager {
public void savePaper(String username, DifficultyLevel level, QuestionPaper paper) {
String folderPath = "papers/" + username + "/";
File folder = new File(folderPath);
if (!folder.exists()) {
folder.mkdirs();
}
String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
String filePath = folderPath + timestamp + ".txt";
try (PrintWriter writer = new PrintWriter(new FileWriter(filePath))) {
writer.write(paper.getFormattedPaper());
System.out.println("试卷已保存至: " + filePath);
} catch (IOException e) {
System.out.println("保存文件时出错: " + e.getMessage());
}
}
}

@ -0,0 +1,149 @@
import java.util.*;
public class Main {
private static Map<String, User> users = new HashMap<>();
private static Scanner scanner = new Scanner(System.in);
private static User currentUser = null;
private static QuestionGenerator generator = new QuestionGenerator();
private static FileManager fileManager = new FileManager();
public static void main(String[] args) {
initializeUsers();
showLoginPrompt();
}
private static void initializeUsers() {
// 小学用户
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));
}
/*private static void showLoginPrompt() {
System.out.println("请输入用户名和密码(用空格隔开):");
while (true) {
String input = scanner.nextLine().trim();
String[] credentials = input.split(" ");
if (credentials.length != 2) {
System.out.println("请输入正确的用户名、密码");
continue;
}
String username = credentials[0];
String password = credentials[1];
if (authenticateUser(username, password)) {
System.out.println("当前选择为" + currentUser.getLevel().getChineseName() + "出题");
showMainMenu();
break;
} else {
System.out.println("请输入正确的用户名、密码");
}
}
}*/
private static void showLoginPrompt() {
System.out.println("请输入用户名和密码(用空格隔开):");
while (true) {
String input = scanner.nextLine().trim();
String[] credentials = input.split(" ");
if (credentials.length != 2) {
System.out.println("请输入正确的用户名、密码");
continue;
}
String username = credentials[0];
String password = credentials[1];
if (authenticateUser(username, password)) {
// 新增:加载该用户的历史题目
generator.loadUserHistory(username);
System.out.println("当前选择为" + currentUser.getLevel().getChineseName() + "出题");
showMainMenu();
break;
} else {
System.out.println("请输入正确的用户名、密码");
}
}
}
private static boolean authenticateUser(String username, String password) {
User user = users.get(username);
if (user != null && user.authenticate(username, password)) {
currentUser = new User(username, password, user.getLevel());
return true;
}
return false;
}
private static void showMainMenu() {
while (true) {
System.out.println("准备生成" + currentUser.getLevel().getChineseName() +
"数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录:");
String input = scanner.nextLine().trim();
if (input.equals("-1")) {
currentUser = null;
generator.clearHistory();
showLoginPrompt();
break;
}
if (input.startsWith("切换为")) {
handleLevelSwitch(input);
continue;
}
try {
int count = Integer.parseInt(input);
if (count >= 10 && count <= 30) {
generatePaper(count);
} else {
System.out.println("题目数量应在10-30之间");
}
} catch (NumberFormatException e) {
System.out.println("请输入有效的数字或'切换为XX'命令");
}
}
}
private static void handleLevelSwitch(String input) {
String levelName = input.substring(3).trim();
DifficultyLevel newLevel = DifficultyLevel.fromChineseName(levelName);
if (newLevel != null) {
currentUser.setLevel(newLevel);
System.out.println("准备生成" + newLevel.getChineseName() + "数学题目,请输入生成题目数量");
} else {
System.out.println("请输入小学、初中和高中三个选项中的一个");
}
}
private static void generatePaper(int questionCount) {
QuestionPaper paper = new QuestionPaper();
for (int i = 0; i < questionCount; i++) {
String question = generator.generateQuestion(currentUser.getLevel());
paper.addQuestion(question);
}
fileManager.savePaper(currentUser.getUsername(), currentUser.getLevel(), paper);
System.out.println("已生成" + questionCount + "道" +
currentUser.getLevel().getChineseName() + "数学题目");
}
}

@ -0,0 +1,201 @@
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class QuestionGenerator {private Random random = new Random();
private Set<String> generatedQuestions = new HashSet<>();
private Set<String> userHistoryQuestions = new HashSet<>(); // 新增:用户历史题目
// 新增方法:加载用户历史题目
public void loadUserHistory(String username) {
userHistoryQuestions.clear();
String folderPath = "papers/" + username + "/";
File folder = new File(folderPath);
if (!folder.exists()) return;
File[] files = folder.listFiles((dir, name) -> name.endsWith(".txt"));
if (files == null) return;
for (File file : files) {
try {
String content = new String(Files.readAllBytes(Paths.get(file.getPath())));
// 提取题目每行一个题目格式如1. 题目内容)
String[] lines = content.split("\n");
for (String line : lines) {
if (line.matches("^\\d+\\.\\s+.+")) {
String question = line.substring(line.indexOf(".") + 1).trim();
userHistoryQuestions.add(question);
}
}
} catch (IOException e) {
System.out.println("读取历史文件失败: " + file.getName());
}
}
}
public String generateQuestion(DifficultyLevel level) {
String question;
int attempt = 0;
do {
question = generateSingleQuestion(level);
attempt++;
if (attempt > 100) {
throw new RuntimeException("无法生成不重复的题目,请清除历史记录");
}
} while (generatedQuestions.contains(question) || userHistoryQuestions.contains(question)); // 修改:同时检查历史题目
generatedQuestions.add(question);
return question;
}
private String generateSingleQuestion(DifficultyLevel level) {
int operandCount = random.nextInt(3) + 2; // 2-4个操作数
StringBuilder question = new StringBuilder();
switch (level) {
case PRIMARY:
question.append(generatePrimaryQuestion(operandCount));
break;
case JUNIOR:
question.append(generateJuniorQuestion(operandCount));
break;
case SENIOR:
question.append(generateSeniorQuestion(operandCount));
break;
}
return question.toString();
}
private String generatePrimaryQuestion(int operandCount) {
String[] operators = {"+", "-", "*", "/"};
return generateBasicQuestion(operandCount, operators, false);
}
private String generateJuniorQuestion(int operandCount) {
String question;
int attempt = 0;
do {
String[] operators = {"+", "-", "*", "/"};
question = generateBasicQuestion(operandCount, operators, true);
// 强制添加平方或开根号修改点1
question = addPowerOrRoot(question);
attempt++;
} while ((!question.contains("²") && !question.contains("√")) && attempt < 10);
return question;
}
private String generateSeniorQuestion(int operandCount) {
String question;
int attempt = 0;
do {
String[] operators = {"+", "-", "*", "/"};
question = generateBasicQuestion(operandCount, operators, true);
// 强制添加三角函数修改点2
question = addTrigonometricFunction(question);
attempt++;
} while ((!question.contains("sin") && !question.contains("cos") && !question.contains("tan")) && attempt < 10);
return question;
}
private String generateBasicQuestion(int operandCount, String[] operators, boolean useParentheses) {
if (operandCount < 2) {
// 单操作数时直接返回数字修改点3
return String.valueOf(getRandomNumber());
}
StringBuilder question = new StringBuilder();
int num = getRandomNumber();
question.append(num);
for (int i = 1; i < operandCount; i++) {
String operator = operators[random.nextInt(operators.length)];
num = getRandomNumber();
if (operator.equals("/")) {
num = Math.max(1, num);
}
boolean needParentheses = useParentheses &&
(operator.equals("*") || operator.equals("/")) &&
i < operandCount - 1;
if (needParentheses) {
question.insert(0, "(");
question.append(") ").append(operator).append(" ").append(num);
} else {
question.append(" ").append(operator).append(" ").append(num);
}
}
return question.toString();
}
private String addPowerOrRoot(String originalQuestion) {
// 修改点4移除长度限制支持单操作数
String[] parts = originalQuestion.split(" ");
// 如果是单操作数表达式
if (parts.length == 1) {
if (random.nextBoolean()) {
return parts[0] + "²";
} else {
return "√(" + parts[0] + ")";
}
}
// 多操作数表达式
int position = random.nextInt(parts.length);
while (position % 2 != 0) {
position = random.nextInt(parts.length);
}
if (random.nextBoolean()) {
parts[position] = parts[position] + "²";
} else {
parts[position] = "√(" + parts[position] + ")";
}
return String.join(" ", parts);
}
private String addTrigonometricFunction(String originalQuestion) {
// 修改点5移除长度限制支持单操作数
String[] parts = originalQuestion.split(" ");
// 如果是单操作数表达式
if (parts.length == 1) {
String[] trigFunctions = {"sin", "cos", "tan"};
String trigFunction = trigFunctions[random.nextInt(trigFunctions.length)];
return trigFunction + "(" + parts[0] + ")";
}
// 多操作数表达式
int position = random.nextInt(parts.length);
while (position % 2 != 0) {
position = random.nextInt(parts.length);
}
String[] trigFunctions = {"sin", "cos", "tan"};
String trigFunction = trigFunctions[random.nextInt(trigFunctions.length)];
parts[position] = trigFunction + "(" + parts[position] + ")";
return String.join(" ", parts);
}
private int getRandomNumber() {
return random.nextInt(100) + 1;
}
public void clearHistory() {
generatedQuestions.clear();
}
}

@ -0,0 +1,22 @@
import java.util.ArrayList;
import java.util.List;
public class QuestionPaper {
private List<String> questions = new ArrayList<>();
public void addQuestion(String question) {
questions.add(question);
}
public List<String> getQuestions() {
return questions;
}
public String getFormattedPaper() {
StringBuilder paper = new StringBuilder();
for (int i = 0; i < questions.size(); i++) {
paper.append(i + 1).append(". ").append(questions.get(i)).append("\n\n");
}
return paper.toString();
}
}

@ -0,0 +1,20 @@
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;
}
public String getUsername() { return username; }
public String getPassword() { return password; }
public DifficultyLevel getLevel() { return level; }
public void setLevel(DifficultyLevel level) { this.level = level; }
public boolean authenticate(String inputUsername, String inputPassword) {
return this.username.equals(inputUsername) && this.password.equals(inputPassword);
}
}

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file not shown.
Loading…
Cancel
Save