@ -0,0 +1,53 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* 负责账号验证与学段映射.
|
||||
*/
|
||||
public final class AccountManager {
|
||||
|
||||
|
||||
private final Map<String, String> userToPassword = new HashMap<>();
|
||||
private final Map<String, String> userToLevel = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 用户姓名密码信息初始化.
|
||||
*/
|
||||
public AccountManager() {
|
||||
|
||||
register("张三1", "123", "小学");
|
||||
register("张三2", "123", "小学");
|
||||
register("张三3", "123", "小学");
|
||||
|
||||
register("李四1", "123", "初中");
|
||||
register("李四2", "123", "初中");
|
||||
register("李四3", "123", "初中");
|
||||
|
||||
register("王五1", "123", "高中");
|
||||
register("王五2", "123", "高中");
|
||||
register("王五3", "123", "高中");
|
||||
}
|
||||
|
||||
|
||||
private void register(String user, String pwd, String level) {
|
||||
userToPassword.put(user, pwd);
|
||||
userToLevel.put(user, level);
|
||||
}
|
||||
|
||||
/**
|
||||
* 账号密码匹配.
|
||||
*/
|
||||
public boolean validate(String user, String pwd) {
|
||||
return user != null && pwd != null && pwd.equals(userToPassword.get(user));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户初始等级.
|
||||
*/
|
||||
public String getLevel(String user) {
|
||||
return userToLevel.get(user);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,79 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 抽象题目生成器基类. 子类需要实现 {@link #generateQuestion()} 来提供不同学段的题目生成逻辑.
|
||||
*/
|
||||
public abstract class BaseQuestionGenerator {
|
||||
|
||||
/**
|
||||
* 用于生成随机数的随机数生成器.
|
||||
*/
|
||||
protected final Random random = new Random();
|
||||
|
||||
/**
|
||||
* 生成一组不重复的数学题目列表.
|
||||
*
|
||||
* @param count 要生成的题目数量.
|
||||
* @param existingQuestions 已经存在的题目集合.
|
||||
* @return 生成的题目列表.
|
||||
*/
|
||||
public List<String> generate(int count, Set<String> existingQuestions) {
|
||||
List<String> result = new ArrayList<>();
|
||||
Set<String> seen = new HashSet<>(existingQuestions == null ? 0 : existingQuestions.size());
|
||||
if (existingQuestions != null) {
|
||||
seen.addAll(existingQuestions);
|
||||
}
|
||||
|
||||
int attempts = 0;
|
||||
while (result.size() < count && attempts < count * 20) {
|
||||
String question = generateQuestion();
|
||||
if (!seen.contains(question)) {
|
||||
result.add(question);
|
||||
seen.add(question);
|
||||
}
|
||||
attempts++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成一个指定范围内的随机整数.
|
||||
*
|
||||
* @param a 下界.
|
||||
* @param b 上界.
|
||||
* @return 返回一个位于 a 和 b 之间的随机整数.
|
||||
*/
|
||||
protected int randInt(int a, int b) {
|
||||
return a + random.nextInt(b - a + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机返回一个基础运算符: "+", "-", "*", 或 "/".
|
||||
*
|
||||
* @return 随机运算符字符串.
|
||||
*/
|
||||
protected String randomBasicOperator() {
|
||||
return switch (random.nextInt(4)) {
|
||||
case 0 -> "+";
|
||||
case 1 -> "-";
|
||||
case 2 -> "*";
|
||||
default -> "/";
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成单个题目. 子类必须实现该方法以提供不同学段的题目生成逻辑.
|
||||
*
|
||||
* @return 返回生成的数学题目字符串.
|
||||
*/
|
||||
protected abstract String generateQuestion();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,16 @@
|
||||
|
||||
/** 常量定义. */
|
||||
public final class Constants {
|
||||
public static final String BASE_OUTPUT_DIR = "output"; // 主输出文件夹
|
||||
public static final int MIN_QUESTIONS = 10;
|
||||
public static final int MAX_QUESTIONS = 30;
|
||||
public static final int MIN_OPERANDS = 1;
|
||||
public static final int MAX_OPERANDS = 5;
|
||||
public static final int MIN_VALUE = 1;
|
||||
public static final int MAX_VALUE = 100;
|
||||
|
||||
|
||||
private Constants() {
|
||||
// no instances
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 高中题目生成器.
|
||||
*/
|
||||
class HighQuestionGenerator extends BaseQuestionGenerator {
|
||||
|
||||
@Override
|
||||
protected String generateQuestion() {
|
||||
int ops = randInt(Constants.MIN_OPERANDS, Constants.MAX_OPERANDS);
|
||||
List<String> tokens = new ArrayList<>();
|
||||
boolean hasTrig = false;
|
||||
|
||||
for (int i = 0; i < ops; i++) {
|
||||
if (!hasTrig && random.nextDouble() < 0.5) {
|
||||
tokens.add(randomTrig());
|
||||
hasTrig = true;
|
||||
} else if (random.nextDouble() < 0.2) {
|
||||
tokens.add("(" + randInt(Constants.MIN_VALUE, Constants.MAX_VALUE) + "^2)");
|
||||
} else {
|
||||
tokens.add(String.valueOf(randInt(Constants.MIN_VALUE, Constants.MAX_VALUE)));
|
||||
}
|
||||
if (i < ops - 1) {
|
||||
tokens.add(randomBasicOperator());
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasTrig) {
|
||||
int pos = Math.max(0, random.nextInt(tokens.size() / 2 + 1) * 2);
|
||||
tokens.add(pos, randomTrig());
|
||||
if (pos + 1 < tokens.size()) {
|
||||
tokens.add(pos + 1, "+");
|
||||
}
|
||||
}
|
||||
|
||||
return String.join(" ", tokens);
|
||||
}
|
||||
|
||||
private String randomTrig() {
|
||||
return switch (random.nextInt(3)) {
|
||||
case 0 -> "sin(" + randInt(0, 90) + ")";
|
||||
case 1 -> "cos(" + randInt(0, 90) + ")";
|
||||
default -> "tan(" + randInt(0, 60) + ")";
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 初中题目生成器.
|
||||
*/
|
||||
class MiddleQuestionGenerator extends BaseQuestionGenerator {
|
||||
|
||||
@Override
|
||||
protected String generateQuestion() {
|
||||
int ops = randInt(Constants.MIN_OPERANDS, Constants.MAX_OPERANDS);
|
||||
List<String> tokens = new ArrayList<>();
|
||||
boolean hasPowerOrSqrt = false;
|
||||
|
||||
for (int i = 0; i < ops; i++) {
|
||||
if (!hasPowerOrSqrt && random.nextDouble() < 0.4) {
|
||||
if (random.nextBoolean()) {
|
||||
tokens.add("(" + randInt(Constants.MIN_VALUE, Constants.MAX_VALUE) + "^2)");
|
||||
} else {
|
||||
tokens.add("sqrt(" + randInt(Constants.MIN_VALUE, Constants.MAX_VALUE) + ")");
|
||||
}
|
||||
hasPowerOrSqrt = true;
|
||||
} else {
|
||||
tokens.add(String.valueOf(randInt(Constants.MIN_VALUE, Constants.MAX_VALUE)));
|
||||
}
|
||||
if (i < ops - 1) {
|
||||
tokens.add(randomBasicOperator());
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasPowerOrSqrt) {
|
||||
int pos = Math.max(0, random.nextInt(tokens.size() / 2 + 1) * 2);
|
||||
tokens.add(pos, "sqrt(4)");
|
||||
if (pos + 1 < tokens.size()) {
|
||||
tokens.add(pos + 1, "+");
|
||||
}
|
||||
}
|
||||
|
||||
return String.join(" ", tokens);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue