编码规范优化

pull/3/head
宋奇峰 5 months ago
parent beddcbcb51
commit 1990441707

BIN
.DS_Store vendored

Binary file not shown.

@ -1,30 +1,30 @@
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.Collectors;
public class questionGenerator {
public class Generator {
private static final Random rand = new Random();
static class QuestionGenerator {
private final login.Level level;
private final Login.Level level;
private final Set<String> existing;
QuestionGenerator(login.Level level, List<String> existingQuestions) {
QuestionGenerator(Login.Level level, List<String> existingQuestions) {
this.level = level;
this.existing = existingQuestions.stream().map(String::trim).collect(Collectors.toSet());
}
List<String> generatePaper(int n) {
Set<String> generated = new LinkedHashSet<>();
int attempts = 0;
// 防止死循环
int attempts = 0;
int maxAttempts = 2000;
while (generated.size() < n && attempts < maxAttempts) {
attempts++;
String q = generateOneQuestion();
// 统一去掉题号与多余空白来比对
String key = normalize(q);
//与文件内+本次前面生成的查重
if (!existing.contains(key) && !generated.contains(key)) {
generated.add(q);
}
@ -34,14 +34,14 @@ public class questionGenerator {
}
return new ArrayList<>(generated);
}
//去空格
private String normalize(String s) {
return s.replaceAll("\\s+","").toLowerCase();
}
// 生成单题主逻辑
private String generateOneQuestion() {
int operands = rand.nextInt(4) + 2; // 保证 2..5 个操作数
int operands = rand.nextInt(5) + 1; // 保证 2..5 个操作数
return switch (level) {
case PRIMARY -> genPrimary(operands);
case MIDDLE -> genMiddle(operands);
@ -79,9 +79,8 @@ public class questionGenerator {
private String genMiddle(int operands) {
// 基本表达式生成,后插入平方或开根号
String expr = genPrimary(operands); // 基本算术
// decide to apply square or sqrt to random operand or subexpression
// 2选1
if (rand.nextBoolean()) {
// apply ^2 somewhere
// 找一个数字位置并替换为 (x)^2
expr = applySquare(expr);
} else {
@ -93,7 +92,7 @@ public class questionGenerator {
// 生成高中题:至少包含 sin/cos/tan
private String genHigh(int operands) {
String expr = genPrimary(operands);
expr = applyTrig(expr); // 把某个数或子表达式包成 trig(...)
expr = applyTrig(expr);
return expr;
}
@ -115,7 +114,6 @@ public class questionGenerator {
private String applySqrt(String expr) {
List<int[]> spans = findNumberSpans(expr);
if (spans.isEmpty()) {
// fallback: wrap entire expr
return "sqrt(" + expr + ")";
}
int[] s = spans.get(rand.nextInt(spans.size()));
@ -139,7 +137,7 @@ public class questionGenerator {
return before + func + "(" + num + ")" + after;
}
// 找到表达式中纯数字的起止索引
// 找出数字位置
private List<int[]> findNumberSpans(String expr) {
List<int[]> spans = new ArrayList<>();
char[] chs = expr.toCharArray();
@ -156,11 +154,11 @@ public class questionGenerator {
}
return spans;
}
//a..b随机数
private int randInt(int a, int b) {
return rand.nextInt(b - a + 1) + a;
}
//抽签功能
private <T> T randomChoice(List<T> list) {
return list.get(rand.nextInt(list.size()));
}

@ -1,20 +1,18 @@
import java.nio.charset.StandardCharsets;
import java.io.*;
import java.nio.file.*;
import java.util.*;
public class mpg_main {
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();
Login.Account user = Login.LoginLoop();
if (user == null) continue;
login.Level currentLevel = user.level;
System.out.println("当前选择为 " + languageSwitch.levelToChinese(currentLevel) + "出题");
Login.Level currentLevel = user.level;
System.out.println("当前选择为 " + LanguageSwitch.levelToChinese(currentLevel) + "出题");
boolean loggedIn = true;
while (loggedIn) {
System.out.print("系统提示“准备生成 " + languageSwitch.levelToChinese(currentLevel) + "数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录”\n> ");
System.out.print("系统提示“准备生成 " + LanguageSwitch.levelToChinese(currentLevel) + "数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录”\n> ");
String line = scanner.nextLine().trim();
if (line.equals("-1")) {
System.out.println("退出当前用户,返回登录界面。");
@ -27,12 +25,12 @@ public class mpg_main {
System.out.println("请输入小学、初中和高中三个选项中的一个");
continue;
}
login.Level newLevel = languageSwitch.chineseToLevel(target);
Login.Level newLevel = LanguageSwitch.chineseToLevel(target);
if (newLevel == null) {
System.out.println("请输入小学、初中和高中三个选项中的一个");
} else {
currentLevel = newLevel;
System.out.println("切换成功。当前选择为 " + languageSwitch.levelToChinese(currentLevel) + "出题");
System.out.println("切换成功。当前选择为 " + LanguageSwitch.levelToChinese(currentLevel) + "出题");
}
continue;
}
@ -53,14 +51,14 @@ public class mpg_main {
continue;
}
// 生成题目
List<String> existing = loadFile.loadExistingQuestions(user.username);
questionGenerator.QuestionGenerator qg = new questionGenerator.QuestionGenerator(currentLevel, existing);
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 = savepaper.savePaper(user.username, paper);
String savedPath = Save.savePaper(user.username, paper);
System.out.println("已生成 " + paper.size() + " 道题,保存为: " + savedPath);
}
}

@ -3,7 +3,7 @@ import java.nio.file.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.nio.charset.StandardCharsets;
public class savepaper {
public class Save {
// 保存试卷,返回保存路径字符串
public static String savePaper(String username, List<String> paper) {
Path userDir = Paths.get("data", username);

@ -1,6 +1,6 @@
public class languageSwitch {
public class LanguageSwitch {
//等级转中文输出
public static String levelToChinese(login.Level l) {
public static String levelToChinese(Login.Level l) {
return switch (l) {
case PRIMARY -> "小学";
case MIDDLE -> "初中";
@ -9,12 +9,12 @@ public class languageSwitch {
};
}
//等级中文输入转level
public static login.Level chineseToLevel(String s) {
public static Login.Level chineseToLevel(String s) {
s = s.trim();//去除前后空格
return switch (s) {
case "小学" -> login.Level.PRIMARY;
case "初中" -> login.Level.MIDDLE;
case "高中" -> login.Level.HIGH;
case "小学" -> Login.Level.PRIMARY;
case "初中" -> Login.Level.MIDDLE;
case "高中" -> Login.Level.HIGH;
default -> null;
};
}

@ -3,9 +3,9 @@ import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.Collectors;
public class loadFile {
public class LoadFile {
// 读取该用户文件夹下已有题目的所有题目文本
public static List<String> loadExistingQuestions(String username) {
public static List<String> loadExistingQuestions(String username) {
List<String> all = new ArrayList<>();
Path userDir = Paths.get("data", username);
if (!Files.exists(userDir)) return all;

@ -3,7 +3,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class login {
public class Login {
public static class Account {
String username;//用户名
String password;//密码
@ -35,7 +35,7 @@ public class login {
accounts.put("王五2", new Account("王五2","123", Level.HIGH));
accounts.put("王五3", new Account("王五3","123", Level.HIGH));
}
public static Account loginLoop() {
public static Account LoginLoop() {
while (true) {
System.out.print("请输入用户名和密码,两者之间用空格隔开:\n> ");
String line = scanner.nextLine().trim();//行输入;
@ -49,7 +49,7 @@ public class login {
Account acc = accounts.get(username);
if (acc != null && acc.password.equals(password)) {
System.out.println("登录成功。当前选择为 " + languageSwitch.levelToChinese(acc.level) + "出题");
System.out.println("登录成功。当前选择为 " + LanguageSwitch.levelToChinese(acc.level) + "出题");
return acc;
} else {
System.out.println("请输入正确的用户名、密码");

Loading…
Cancel
Save