parent
01dd55cd08
commit
b588aba198
@ -0,0 +1,42 @@
|
||||
package mathpuzzle.service;
|
||||
import mathpuzzle.entity.User;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 文件处理器
|
||||
* 负责创建用户目录和保存试卷文件
|
||||
*/
|
||||
public class FileHandler {
|
||||
|
||||
public void ensureUserDirectory(User user) throws IOException {
|
||||
String dirPath = "./" + user.getName();
|
||||
Path path = Paths.get(dirPath);
|
||||
if (!Files.exists(path)) {
|
||||
Files.createDirectories(path);
|
||||
}
|
||||
}
|
||||
|
||||
public void savePaper(User user, List<String> questions) throws IOException {
|
||||
ensureUserDirectory(user);
|
||||
// 生成文件名:年-月-日-时-分-秒.txt
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");
|
||||
String fileName = LocalDateTime.now().format(formatter) + ".txt";
|
||||
String filePath = "./" + user.getName() + "/" + fileName;
|
||||
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
|
||||
for (int i = 0; i < questions.size(); i++) {
|
||||
writer.write((i + 1) + ". " + questions.get(i)); // 添加题号
|
||||
writer.newLine();
|
||||
writer.newLine(); // 每题之间空一行
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package mathpuzzle.service;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 初中题目生成器
|
||||
* 题目必须包含至少一个平方或开根号运算符
|
||||
*/
|
||||
public class JuniorHighGenerator implements QuestionGenerator {
|
||||
private static final String[] ADVANCED_OPS = {"平方", "开根号"};
|
||||
private Random random = new Random();
|
||||
|
||||
@Override
|
||||
public List<String> generateQuestions(int count) {
|
||||
List<String> questions = new ArrayList<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
String question = generateSingleQuestion();
|
||||
questions.add(question);
|
||||
}
|
||||
return questions;
|
||||
}
|
||||
|
||||
private String generateSingleQuestion() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int baseNum = random.nextInt(100) + 1;
|
||||
sb.append(baseNum);
|
||||
|
||||
// 强制加入一个高级运算符
|
||||
String advancedOp = ADVANCED_OPS[random.nextInt(ADVANCED_OPS.length)];
|
||||
if ("平方".equals(advancedOp)) {
|
||||
sb.append(" ").append(advancedOp);
|
||||
} else { // 开根号
|
||||
sb.insert(0, advancedOp + "(").append(")");
|
||||
}
|
||||
|
||||
// 可能再附加一个基础运算
|
||||
if (random.nextBoolean()) {
|
||||
String[] basicOps = {"+", "-", "*", "/"};
|
||||
String op = basicOps[random.nextInt(basicOps.length)];
|
||||
int anotherNum = random.nextInt(100) + 1;
|
||||
sb.append(" ").append(op).append(" ").append(anotherNum);
|
||||
}
|
||||
|
||||
return sb.toString() + " =";
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package mathpuzzle.service;
|
||||
|
||||
import mathpuzzle.entity.User;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 题目查重器
|
||||
* 负责加载历史题目并检查新题目是否重复
|
||||
*/
|
||||
public class QuestionDeduplicator {
|
||||
private Set<String> existingQuestions = new HashSet<>();
|
||||
|
||||
public void loadExistingQuestions(User user) {
|
||||
existingQuestions.clear();
|
||||
String userDir = "./" + user.getName();
|
||||
File dir = new File(userDir);
|
||||
|
||||
if (!dir.exists()) {
|
||||
return; // 目录不存在,无历史题目
|
||||
}
|
||||
|
||||
File[] files = dir.listFiles((d, name) -> name.endsWith(".txt"));
|
||||
if (files == null) return;
|
||||
|
||||
for (File file : files) {
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
// 只加载题目行,忽略题号和空行
|
||||
if (line.trim().isEmpty() || line.matches("\\d+\\. .*")) {
|
||||
String questionContent = line.replaceFirst("\\d+\\. ", "").trim();
|
||||
if (!questionContent.isEmpty() && !questionContent.equals("=")) {
|
||||
existingQuestions.add(questionContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("读取历史文件时出错: " + file.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isDuplicate(String question) {
|
||||
return existingQuestions.contains(question);
|
||||
}
|
||||
|
||||
public void addQuestion(String question) {
|
||||
existingQuestions.add(question);
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package mathpuzzle.service;
|
||||
import java.util.List;
|
||||
|
||||
public interface QuestionGenerator {
|
||||
List<String> generateQuestions(int count);
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package mathpuzzle.service;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 高中题目生成器
|
||||
* 题目必须包含至少一个 sin, cos, tan 三角函数运算符
|
||||
*/
|
||||
public class SeniorHighGenerator implements QuestionGenerator {
|
||||
private static final String[] TRIG_FUNCS = {"sin", "cos", "tan"};
|
||||
private Random random = new Random();
|
||||
|
||||
@Override
|
||||
public List<String> generateQuestions(int count) {
|
||||
List<String> questions = new ArrayList<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
String question = generateSingleQuestion();
|
||||
questions.add(question);
|
||||
}
|
||||
return questions;
|
||||
}
|
||||
|
||||
private String generateSingleQuestion() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String trigFunc = TRIG_FUNCS[random.nextInt(TRIG_FUNCS.length)];
|
||||
int angle = random.nextInt(360); // 角度0-359度
|
||||
|
||||
sb.append(trigFunc).append("(").append(angle).append("°)");
|
||||
|
||||
// 可能再附加一个基础运算
|
||||
if (random.nextBoolean()) {
|
||||
String[] basicOps = {"+", "-", "*", "/"};
|
||||
String op = basicOps[random.nextInt(basicOps.length)];
|
||||
int anotherNum = random.nextInt(100) + 1;
|
||||
sb.append(" ").append(op).append(" ").append(anotherNum);
|
||||
}
|
||||
|
||||
return sb.toString() + " =";
|
||||
}
|
||||
}
|
Loading…
Reference in new issue