commit #7

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

BIN
.DS_Store vendored

Binary file not shown.

BIN
src/.DS_Store vendored

Binary file not shown.

@ -0,0 +1,11 @@
public class Assign {
public static void assign(){
while (true) {
Login.Account user = Login.loginLoop();
Login.Level currentLevel = user.level;
System.out.println("当前选择为 " + LanguageSwitch.levelToChinese(currentLevel) + "出题");
Function.function(currentLevel, user);
}
}
}

@ -0,0 +1,17 @@
import java.util.List;
public class Create {
public static void create(int n,Login.Level currentLevel,Login.Account user) {
List<String> existing = LoadFile.loadExistingQuestions(user.username);
Generator.QuestionGenerator qg = new Generator.QuestionGenerator(currentLevel, existing);
List<String> paper = qg.generatePaper(n);
if (paper.isEmpty()) {
System.out.println("未能生成题目(可能因去重约束导致)。");
} else {
// 保存文件
String savedPath = Save.savePaper(user.username, paper);
System.out.println("已生成 " + paper.size() + " 道题,保存为: " + savedPath);
}
}
}

@ -0,0 +1,48 @@
import java.util.Scanner;
import java.nio.charset.StandardCharsets;
public class Function {
private static final Scanner SCANNER = new Scanner(System.in, StandardCharsets.UTF_8);
public static void function(Login.Level currentLevel,Login.Account user){
while (true) {
System.out.print("系统提示“准备生成 " + LanguageSwitch.levelToChinese(currentLevel)
+ "数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录”\n> ");
String line = SCANNER.nextLine().trim();
if (line.equals("-1")) {
System.out.println("退出当前用户,返回登录界面。");
break;
}
if (line.startsWith("切换为")) {
String target = line.substring("切换为".length()).trim();
if (target.isEmpty()) {
System.out.println("请输入小学、初中和高中三个选项中的一个");
continue;
}
Login.Level newLevel = LanguageSwitch.chineseToLevel(target);
if (newLevel == null) {
System.out.println("请输入小学、初中和高中三个选项中的一个");
} else {
currentLevel = newLevel;
System.out.println(
"切换成功。当前选择为 " + LanguageSwitch.levelToChinese(currentLevel) + "出题");
}
continue;
}
int n;
try {
n = Integer.parseInt(line);
} catch (NumberFormatException e) {
System.out.println("请输入有效的整数10-30或-1退出或 '切换为 XX'。");
continue;
}
if (n < 10 || n > 30) {
System.out.println("题目数量的有效输入范围是“10-30”含10,30或-1退出登录。");
continue;
}
// 生成题目
Create.create(n,currentLevel,user);
}
}
}

@ -64,9 +64,10 @@ public class Generator {
// 生成单题主逻辑
private String generateOneQuestion() {
int operands = RAND.nextInt(5) + 1; // 保证 2..5 个操作数
int operands = RAND.nextInt(5) + 1; // 保证 1..5 个操作数
int operands_p = RAND.nextInt(4) + 2; // 保证 2..5 个操作数
return switch (level) {
case PRIMARY -> genPrimary(operands);
case PRIMARY -> genPrimary(operands_p);
case MIDDLE -> genMiddle(operands);
case HIGH -> genHigh(operands);
};

@ -1,77 +1,10 @@
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Scanner;
public class Main {
/**
*
*/
private static final Scanner SCANNER = new Scanner(System.in, StandardCharsets.UTF_8);
public static void main(String[] args) {
System.out.println("=== 中小学数学卷子自动生成程序 ===");
while (true) {
Login.Account user = Login.loginLoop();
if (user == null) {
continue;
}
Login.Level currentLevel = user.level;
System.out.println("当前选择为 " + LanguageSwitch.levelToChinese(currentLevel) + "出题");
boolean loggedIn = true;
while (loggedIn) {
System.out.print("系统提示“准备生成 " + LanguageSwitch.levelToChinese(currentLevel)
+ "数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录”\n> ");
String line = SCANNER.nextLine().trim();
if (line.equals("-1")) {
System.out.println("退出当前用户,返回登录界面。");
loggedIn = false;
break;
}
if (line.startsWith("切换为")) {
String target = line.substring("切换为".length()).trim();
if (target.isEmpty()) {
System.out.println("请输入小学、初中和高中三个选项中的一个");
continue;
}
Login.Level newLevel = LanguageSwitch.chineseToLevel(target);
if (newLevel == null) {
System.out.println("请输入小学、初中和高中三个选项中的一个");
} else {
currentLevel = newLevel;
System.out.println(
"切换成功。当前选择为 " + LanguageSwitch.levelToChinese(currentLevel) + "出题");
}
continue;
}
int n;
try {
n = Integer.parseInt(line);
} catch (NumberFormatException e) {
System.out.println("请输入有效的整数10-30或-1退出或 '切换为 XX'。");
continue;
}
if (n == -1) {
System.out.println("退出当前用户,返回登录界面。");
loggedIn = false;
break;
}
if (n < 10 || n > 30) {
System.out.println("题目数量的有效输入范围是“10-30”含10,30或-1退出登录。");
continue;
}
// 生成题目
List<String> existing = LoadFile.loadExistingQuestions(user.username);
Generator.QuestionGenerator qg = new Generator.QuestionGenerator(currentLevel, existing);
List<String> paper = qg.generatePaper(n);
if (paper.isEmpty()) {
System.out.println("未能生成题目(可能因去重约束导致)。");
} else {
// 保存文件
String savedPath = Save.savePaper(user.username, paper);
System.out.println("已生成 " + paper.size() + " 道题,保存为: " + savedPath);
}
}
}
Assign.assign();
}
}

@ -17,6 +17,12 @@ public class LoadFile {
* @return
*/
// 读取该用户文件夹下已有题目的所有题目文本
// 将文件中按题号拆分题目
//识别题号
// 新题开始 保存旧题
//去题号
// 继续当前题(空行也可能出现)
// 转链式 去空格 去空字符串 去重 转回list
public static List<String> loadExistingQuestions(String username) {
List<String> all = new ArrayList<>();
Path userDir = Paths.get("data", username);
@ -27,20 +33,15 @@ public class LoadFile {
DirectoryStream<Path> ds = Files.newDirectoryStream(userDir, "*.txt");
for (Path p : ds) {
List<String> lines = Files.readAllLines(p, StandardCharsets.UTF_8);
// 将文件中按题号拆分题目
StringBuilder cur = new StringBuilder();
for (String line : lines) {
//识别题号
if (line.matches("^\\s*\\d+\\..*")) {
// 新题开始 保存旧题
if (!cur.isEmpty()) {
all.add(cur.toString().trim());
}
cur.setLength(0);
//去题号
cur.append(line.replaceFirst("^\\s*\\d+\\.", "").trim());
} else {
// 继续当前题(空行也可能出现)
if (line.trim().isEmpty()) {
if (!cur.isEmpty()) {
all.add(cur.toString().trim());
@ -61,7 +62,6 @@ public class LoadFile {
} catch (IOException e) {
System.err.println("读取题目文件失败:" + e.getMessage());
}
// 转链式 去空格 去空字符串 去重 转回list
return all.stream().map(String::trim).filter(s -> !s.isEmpty()).distinct()
.collect(Collectors.toList());
}

Loading…
Cancel
Save