pull/4/head
黄培毅 7 days ago
parent dd141bb546
commit 770eff5664

@ -5,32 +5,32 @@ import java.util.Random;
* *
*/ */
public class JuniorQuestionGenerator implements QuestionGenerator { public class JuniorQuestionGenerator implements QuestionGenerator {
@Override @Override
public String generateQuestion(StringBuilder question, Random random) { public String generateQuestion(StringBuilder question, Random random) {
// 确保所有初中题目都包含至少一个平方或开根号的运算符 // 确保所有初中题目都包含至少一个平方或开根号的运算符
int num = random.nextInt(100) + 1; int num = random.nextInt(100) + 1;
boolean isSquare = random.nextBoolean(); boolean isSquare = random.nextBoolean();
if (isSquare) { if (isSquare) {
// 生成平方题 // 生成平方题
question.append(num).append("^2"); question.append(num).append("^2");
} else { } else {
// 生成开根号题,确保是完全平方数 // 生成开根号题,确保是完全平方数
int sqrtNum = random.nextInt(10) + 1; int sqrtNum = random.nextInt(10) + 1;
num = sqrtNum * sqrtNum; num = sqrtNum * sqrtNum;
question.append("√").append(num); question.append("√").append(num);
} }
// 添加一些其他运算符使题目更复杂
int complexity = random.nextInt(2) + 1;
for (int i = 0; i < complexity; i++) {
String[] operators = {"+", "-", "*"};
String op = operators[random.nextInt(operators.length)];
int addNum = random.nextInt(10) + 1;
question.append(" ").append(op).append(" ").append(addNum);
}
question.append(" = ?"); // 添加一些其他运算符使题目更复杂
return question.toString(); int complexity = random.nextInt(2) + 1;
for (int i = 0; i < complexity; i++) {
String[] operators = {"+", "-", "*"};
String op = operators[random.nextInt(operators.length)];
int addNum = random.nextInt(10) + 1;
question.append(" ").append(op).append(" ").append(addNum);
} }
question.append(" = ?");
return question.toString();
}
} }

Binary file not shown.

@ -9,300 +9,300 @@ import java.util.*;
* *
*/ */
public class MathExamGenerator { public class MathExamGenerator {
// 预设的用户账号信息 // 预设的用户账号信息
private static final Map<String, UserInfo> USER_ACCOUNTS = new HashMap<>(); private static final Map<String, UserInfo> USER_ACCOUNTS = new HashMap<>();
// 当前登录用户信息 // 当前登录用户信息
private static UserInfo currentUser = null; private static UserInfo currentUser = null;
// 已生成的题目集,用于检查重复 // 已生成的题目集,用于检查重复
private static final Set<String> generatedQuestions = new HashSet<>(); private static final Set<String> generatedQuestions = new HashSet<>();
private static boolean isRunning = true; private static boolean isRunning = true;
static { static {
// 初始化小学、初中和高中各三个账号 // 初始化小学、初中和高中各三个账号
// 小学账户 // 小学账户
USER_ACCOUNTS.put("张三1", new UserInfo("张三1", "123", "小学")); USER_ACCOUNTS.put("张三1", new UserInfo("张三1", "123", "小学"));
USER_ACCOUNTS.put("张三2", new UserInfo("张三2", "123", "小学")); USER_ACCOUNTS.put("张三2", new UserInfo("张三2", "123", "小学"));
USER_ACCOUNTS.put("张三3", new UserInfo("张三3", "123", "小学")); USER_ACCOUNTS.put("张三3", new UserInfo("张三3", "123", "小学"));
// 初中账户 // 初中账户
USER_ACCOUNTS.put("李四1", new UserInfo("李四1", "123", "初中")); USER_ACCOUNTS.put("李四1", new UserInfo("李四1", "123", "初中"));
USER_ACCOUNTS.put("李四2", new UserInfo("李四2", "123", "初中")); USER_ACCOUNTS.put("李四2", new UserInfo("李四2", "123", "初中"));
USER_ACCOUNTS.put("李四3", new UserInfo("李四3", "123", "初中")); USER_ACCOUNTS.put("李四3", new UserInfo("李四3", "123", "初中"));
// 高中账户 // 高中账户
USER_ACCOUNTS.put("王五1", new UserInfo("王五1", "123", "高中")); USER_ACCOUNTS.put("王五1", new UserInfo("王五1", "123", "高中"));
USER_ACCOUNTS.put("王五2", new UserInfo("王五2", "123", "高中")); USER_ACCOUNTS.put("王五2", new UserInfo("王五2", "123", "高中"));
USER_ACCOUNTS.put("王五3", new UserInfo("王五3", "123", "高中")); USER_ACCOUNTS.put("王五3", new UserInfo("王五3", "123", "高中"));
} }
public static void main(String[] args) { public static void main(String[] args) {
// 使用BufferedReader配合InputStreamReader明确指定GBK编码 // 使用BufferedReader配合InputStreamReader明确指定GBK编码
try (BufferedReader reader = new BufferedReader( try (BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in, Charset.forName("GBK")))) { new InputStreamReader(System.in, Charset.forName("GBK")))) {
System.out.println("欢迎使用中小学数学卷子自动生成程序"); System.out.println("欢迎使用中小学数学卷子自动生成程序");
System.out.println("请使用您的账号和密码登录(格式:用户名 密码)"); System.out.println("请使用您的账号和密码登录(格式:用户名 密码)");
while (isRunning) { while (isRunning) {
if (currentUser == null) { if (currentUser == null) {
handleLogIn(reader); handleLogIn(reader);
} else {
handleRequirement(reader);
}
}
} catch (IOException e) {
System.out.println("输入输出错误:" + e.getMessage());
}
}
public static void handleLogIn(BufferedReader reader) throws IOException {
// 用户未登录,显示登录提示
System.out.print("请输入用户名和密码(用空格隔开,输入exit退出");
String input = reader.readLine();
if (input == null || input.trim().isEmpty()) {
System.out.println("输入不能为空,请重新输入");
return;
} else if (input.equals("exit")) {
isRunning = false;
return;
}
String[] parts = input.split(" ");
if (parts.length == 2) {
String username = parts[0];
String password = parts[1];
// 验证用户登录
boolean loginSuccess = false;
for (Map.Entry<String, UserInfo> entry : USER_ACCOUNTS.entrySet()) {
if (entry.getValue().getUsername().equals(username) &&
entry.getValue().getPassword().equals(password)) {
currentUser = entry.getValue();
loginSuccess = true;
break;
}
}
if (loginSuccess) {
System.out.println("登录成功!当前选择为" + currentUser.getUserType() + "出题");
System.out.println("提示:您可以输入'切换为小学'、'切换为初中'或'切换为高中'来切换题目难度");
handleUserChoice();
loadExistingQuestions();
} else {
System.out.println("登录失败,请输入正确的用户名和密码");
}
} else { } else {
System.out.println("输入格式错误,请用空格分隔用户名和密码"); handleRequirement(reader);
} }
}
} catch (IOException e) {
System.out.println("输入输出错误:" + e.getMessage());
} }
private static void handleRequirement(BufferedReader reader) throws IOException { }
// 用户已登录,显示题目数量输入提示 public static void handleLogIn(BufferedReader reader) throws IOException {
System.out.print("准备生成" + currentUser.getUserType() + // 用户未登录,显示登录提示
"数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录"); System.out.print("请输入用户名和密码(用空格隔开,输入exit退出");
String input = reader.readLine(); String input = reader.readLine();
if (input == null || input.trim().isEmpty()) {
return; if (input == null || input.trim().isEmpty()) {
} System.out.println("输入不能为空,请重新输入");
// 检查是否为切换类型命令 return;
if (input.startsWith("切换为")) { } else if (input.equals("exit")) {
String targetType = input.substring(3).trim(); isRunning = false;
if ("小学".equals(targetType) || "初中".equals(targetType) || "高中".equals(targetType)) { return;
currentUser.setUserType(targetType);
System.out.println("切换成功!准备生成" + currentUser.getUserType() + "数学题目,请输入生成题目数量");
} else {
System.out.println("切换类型错误,请输入'切换为小学'、'切换为初中'或'切换为高中'");
}
return;
}
try {
int questionCount = Integer.parseInt(input);
if (questionCount == -1) {
// 退出登录
currentUser = null;
generatedQuestions.clear();
System.out.println("已退出登录");
} else if (questionCount >= 10 && questionCount <= 30) {
// 生成题目
generateExam(questionCount);
} else {
System.out.println("题目数量必须在10-30之间");
}
} catch (NumberFormatException e) {
System.out.println("请输入有效的数字");
}
} }
// 生成试卷
private static void generateExam(int questionCount) {
List<String> questions = generateUniqueQuestions(questionCount);
// 保存试卷到文件 String[] parts = input.split(" ");
saveExamToFile(questions);
if (parts.length == 2) {
System.out.println("已成功生成" + questions.size() + "道" + currentUser.getUserType() + "数学题目"); String username = parts[0];
String password = parts[1];
// 验证用户登录
boolean loginSuccess = false;
for (Map.Entry<String, UserInfo> entry : USER_ACCOUNTS.entrySet()) {
if (entry.getValue().getUsername().equals(username) &&
entry.getValue().getPassword().equals(password)) {
currentUser = entry.getValue();
loginSuccess = true;
break;
}
}
if (loginSuccess) {
System.out.println("登录成功!当前选择为" + currentUser.getUserType() + "出题");
System.out.println("提示:您可以输入'切换为小学'、'切换为初中'或'切换为高中'来切换题目难度");
handleUserChoice(); handleUserChoice();
loadExistingQuestions();
} else {
System.out.println("登录失败,请输入正确的用户名和密码");
}
} else {
System.out.println("输入格式错误,请用空格分隔用户名和密码");
} }
}
// 生成不重复的题目 private static void handleRequirement(BufferedReader reader) throws IOException {
private static List<String> generateUniqueQuestions(int questionCount) { // 用户已登录,显示题目数量输入提示
List<String> questions = new ArrayList<>(); System.out.print("准备生成" + currentUser.getUserType() +
Random random = new Random(); "数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录");
int attempts = 0; String input = reader.readLine();
int maxAttempts = questionCount * 3; // 最多尝试次数,避免死循环 if (input == null || input.trim().isEmpty()) {
return;
while (questions.size() < questionCount && attempts < maxAttempts) { }
String question = generateQuestion(); // 检查是否为切换类型命令
if (!generatedQuestions.contains(question)) { if (input.startsWith("切换为")) {
questions.add(question); String targetType = input.substring(3).trim();
generatedQuestions.add(question); if ("小学".equals(targetType) || "初中".equals(targetType) || "高中".equals(targetType)) {
} currentUser.setUserType(targetType);
attempts++; System.out.println("切换成功!准备生成" + currentUser.getUserType() + "数学题目,请输入生成题目数量");
} } else {
System.out.println("切换类型错误,请输入'切换为小学'、'切换为初中'或'切换为高中'");
return questions; }
return;
} }
// 处理用户生成试卷后的选择 try {
private static void handleUserChoice() { int questionCount = Integer.parseInt(input);
System.out.println("请选择:");
System.out.println("1. 生成题目"); if (questionCount == -1) {
System.out.println("2. 切换题目难度"); // 退出登录
System.out.println("3. 退出当前用户"); currentUser = null;
System.out.println("4. 退出程序"); generatedQuestions.clear();
System.out.println("已退出登录");
try { } else if (questionCount >= 10 && questionCount <= 30) {
BufferedReader reader = new BufferedReader( // 生成题目
new InputStreamReader(System.in, Charset.forName("GBK"))); generateExam(questionCount);
String choice = reader.readLine(); } else {
System.out.println("题目数量必须在10-30之间");
switch (choice) { }
case "2": } catch (NumberFormatException e) {
handleDifficultySwitch(); System.out.println("请输入有效的数字");
break;
case "3":
currentUser = null;
generatedQuestions.clear();
System.out.println("已退出登录");
break;
case "1":
break;
case "4":
isRunning = false;
break;
default:
System.out.println("无效的选项,请重新输入");
handleUserChoice();
break;
}
} catch (IOException e) {
System.out.println("输入错误:" + e.getMessage());
}
} }
}
// 生成试卷
private static void generateExam(int questionCount) {
List<String> questions = generateUniqueQuestions(questionCount);
// 处理难度切换 // 保存试卷到文件
private static void handleDifficultySwitch() { saveExamToFile(questions);
try {
BufferedReader reader = new BufferedReader( System.out.println("已成功生成" + questions.size() + "道" + currentUser.getUserType() + "数学题目");
new InputStreamReader(System.in, Charset.forName("GBK"))); handleUserChoice();
}
while (true) {
System.out.print("请输入目标难度(小学/初中/高中):"); // 生成不重复的题目
String targetType = reader.readLine(); private static List<String> generateUniqueQuestions(int questionCount) {
List<String> questions = new ArrayList<>();
if ("小学".equals(targetType) || "初中".equals(targetType) || "高中".equals(targetType)) { Random random = new Random();
currentUser.setUserType(targetType); int attempts = 0;
System.out.println("已切换为" + targetType + "难度"); int maxAttempts = questionCount * 3; // 最多尝试次数,避免死循环
break;
} else { while (questions.size() < questionCount && attempts < maxAttempts) {
System.out.println("难度类型错误,请重新输入"); String question = generateQuestion();
} if (!generatedQuestions.contains(question)) {
} questions.add(question);
} catch (IOException e) { generatedQuestions.add(question);
System.out.println("输入错误:" + e.getMessage()); }
} attempts++;
} }
// 根据用户类型获取对应的题目生成器 return questions;
private static QuestionGenerator getQuestionGenerator() { }
if ("小学".equals(currentUser.getUserType())) {
return new PrimaryQuestionGenerator(); // 处理用户生成试卷后的选择
} else if ("初中".equals(currentUser.getUserType())) { private static void handleUserChoice() {
return new JuniorQuestionGenerator(); System.out.println("请选择:");
} else if ("高中".equals(currentUser.getUserType())) { System.out.println("1. 生成题目");
return new SeniorQuestionGenerator(); System.out.println("2. 切换题目难度");
System.out.println("3. 退出当前用户");
System.out.println("4. 退出程序");
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in, Charset.forName("GBK")));
String choice = reader.readLine();
switch (choice) {
case "2":
handleDifficultySwitch();
break;
case "3":
currentUser = null;
generatedQuestions.clear();
System.out.println("已退出登录");
break;
case "1":
break;
case "4":
isRunning = false;
break;
default:
System.out.println("无效的选项,请重新输入");
handleUserChoice();
break;
}
} catch (IOException e) {
System.out.println("输入错误:" + e.getMessage());
}
}
// 处理难度切换
private static void handleDifficultySwitch() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in, Charset.forName("GBK")));
while (true) {
System.out.print("请输入目标难度(小学/初中/高中):");
String targetType = reader.readLine();
if ("小学".equals(targetType) || "初中".equals(targetType) || "高中".equals(targetType)) {
currentUser.setUserType(targetType);
System.out.println("已切换为" + targetType + "难度");
break;
} else {
System.out.println("难度类型错误,请重新输入");
} }
return null; }
} catch (IOException e) {
System.out.println("输入错误:" + e.getMessage());
} }
}
// 根据用户类型获取对应的题目生成器
private static QuestionGenerator getQuestionGenerator() {
if ("小学".equals(currentUser.getUserType())) {
return new PrimaryQuestionGenerator();
} else if ("初中".equals(currentUser.getUserType())) {
return new JuniorQuestionGenerator();
} else if ("高中".equals(currentUser.getUserType())) {
return new SeniorQuestionGenerator();
}
return null;
}
// 根据用户类型生成题目 // 根据用户类型生成题目
private static String generateQuestion() { private static String generateQuestion() {
StringBuilder question = new StringBuilder(); StringBuilder question = new StringBuilder();
Random random = new Random(); Random random = new Random();
QuestionGenerator generator = getQuestionGenerator(); QuestionGenerator generator = getQuestionGenerator();
if (generator != null) {
return generator.generateQuestion(question, random);
}
return "题目生成错误"; if (generator != null) {
return generator.generateQuestion(question, random);
} }
// 加载已存在的题目,避免重复 return "题目生成错误";
private static void loadExistingQuestions() { }
if (currentUser == null) return;
try { // 加载已存在的题目,避免重复
String userDir = currentUser.getUsername(); private static void loadExistingQuestions() {
File dir = new File(userDir); if (currentUser == null) return;
try {
if (dir.exists() && dir.isDirectory()) { String userDir = currentUser.getUsername();
File[] files = dir.listFiles(); File dir = new File(userDir);
if (files != null) {
for (File file : files) { if (dir.exists() && dir.isDirectory()) {
if (file.isFile() && file.getName().endsWith(".txt")) { File[] files = dir.listFiles();
try (BufferedReader reader = new BufferedReader( if (files != null) {
new InputStreamReader(new FileInputStream(file), Charset.forName("GBK")))) { for (File file : files) {
String line; if (file.isFile() && file.getName().endsWith(".txt")) {
while ((line = reader.readLine()) != null) { try (BufferedReader reader = new BufferedReader(
line = line.trim(); new InputStreamReader(new FileInputStream(file), Charset.forName("GBK")))) {
if (!line.isEmpty() && !line.matches("^\\d+\\.")) { String line;
generatedQuestions.add(line); while ((line = reader.readLine()) != null) {
} line = line.trim();
} if (!line.isEmpty() && !line.matches("^\\d+\\.")) {
} generatedQuestions.add(line);
} }
}
} }
}
} }
} catch (IOException e) { }
System.out.println("加载已存在题目时出错:" + e.getMessage());
} }
}
} catch (IOException e) {
System.out.println("加载已存在题目时出错:" + e.getMessage());
} }
}
// 保存试卷到文件
private static void saveExamToFile(List<String> questions) { // 保存试卷到文件
try { private static void saveExamToFile(List<String> questions) {
// 创建用户目录(相对路径) try {
String userDir = currentUser.getUsername(); // 创建用户目录(相对路径)
Files.createDirectories(Paths.get(userDir)); String userDir = currentUser.getUsername();
Files.createDirectories(Paths.get(userDir));
// 生成文件名:年-月-日-时-分-秒.txt
String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()); // 生成文件名:年-月-日-时-分-秒.txt
String filePath = userDir + File.separator + timestamp + ".txt"; String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
String filePath = userDir + File.separator + timestamp + ".txt";
try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(filePath), Charset.forName("GBK")))) { try (BufferedWriter writer = new BufferedWriter(
for (int i = 0; i < questions.size(); i++) { new OutputStreamWriter(new FileOutputStream(filePath), Charset.forName("GBK")))) {
writer.write((i + 1) + ". " + questions.get(i)); for (int i = 0; i < questions.size(); i++) {
writer.newLine(); writer.write((i + 1) + ". " + questions.get(i));
if (i < questions.size() - 1) { writer.newLine();
writer.newLine(); // 题目之间空一行 if (i < questions.size() - 1) {
} writer.newLine(); // 题目之间空一行
} }
}
} catch (IOException e) {
System.out.println("保存试卷时出错:" + e.getMessage());
} }
}
} catch (IOException e) {
System.out.println("保存试卷时出错:" + e.getMessage());
} }
}
} }

@ -5,37 +5,37 @@ import java.util.Random;
* *
*/ */
public class PrimaryQuestionGenerator implements QuestionGenerator { public class PrimaryQuestionGenerator implements QuestionGenerator {
@Override @Override
public String generateQuestion(StringBuilder question, Random random) { public String generateQuestion(StringBuilder question, Random random) {
int num1 = random.nextInt(100) + 1; // 1-100的随机数 int num1 = random.nextInt(100) + 1; // 1-100的随机数
int num2 = random.nextInt(100) + 1; int num2 = random.nextInt(100) + 1;
String[] operators = {"+", "-", "*", "/"}; String[] operators = {"+", "-", "*", "/"};
String op = operators[random.nextInt(operators.length)]; String op = operators[random.nextInt(operators.length)];
// 确保减法和除法结果为正整数 // 确保减法和除法结果为正整数
if ("-".equals(op) && num1 < num2) { if ("-".equals(op) && num1 < num2) {
int temp = num1; int temp = num1;
num1 = num2; num1 = num2;
num2 = temp; num2 = temp;
} else if ("/".equals(op)) { } else if ("/".equals(op)) {
// 确保除法结果为整数 // 确保除法结果为整数
num1 = num2 * (random.nextInt(10) + 1); num1 = num2 * (random.nextInt(10) + 1);
} }
// 10%的概率添加括号
if (random.nextDouble() < 0.1) {
question.append("(");
question.append(num1);
question.append(" ").append(op).append(" ");
question.append(num2);
question.append(")");
} else {
question.append(num1);
question.append(" ").append(op).append(" ");
question.append(num2);
}
question.append(" = ?"); // 10%的概率添加括号
return question.toString(); if (random.nextDouble() < 0.1) {
question.append("(");
question.append(num1);
question.append(" ").append(op).append(" ");
question.append(num2);
question.append(")");
} else {
question.append(num1);
question.append(" ").append(op).append(" ");
question.append(num2);
} }
question.append(" = ?");
return question.toString();
}
} }

@ -5,11 +5,11 @@ import java.util.Random;
* *
*/ */
public interface QuestionGenerator { public interface QuestionGenerator {
/** /**
* *
* @param question StringBuilder * @param question StringBuilder
* @param random * @param random
* @return * @return
*/ */
String generateQuestion(StringBuilder question, Random random); String generateQuestion(StringBuilder question, Random random);
} }

@ -5,25 +5,25 @@ import java.util.Random;
* *
*/ */
public class SeniorQuestionGenerator implements QuestionGenerator { public class SeniorQuestionGenerator implements QuestionGenerator {
@Override @Override
public String generateQuestion(StringBuilder question, Random random) { public String generateQuestion(StringBuilder question, Random random) {
// 确保所有高中题目都包含至少一个sin、cos或tan的运算符 // 确保所有高中题目都包含至少一个sin、cos或tan的运算符
String[] functions = {"sin(", "cos(", "tan("}; String[] functions = {"sin(", "cos(", "tan("};
String func = functions[random.nextInt(functions.length)]; String func = functions[random.nextInt(functions.length)];
int angle = random.nextInt(100)+1; int angle = random.nextInt(100)+1;
question.append(func).append(angle).append(")"); question.append(func).append(angle).append(")");
// 添加一些其他运算符使题目更复杂 // 添加一些其他运算符使题目更复杂
int complexity = random.nextInt(3); int complexity = random.nextInt(3);
for (int i = 0; i < complexity; i++) { for (int i = 0; i < complexity; i++) {
String[] operators = {"+", "-", "*", "/"}; String[] operators = {"+", "-", "*", "/"};
String op = operators[random.nextInt(operators.length)]; String op = operators[random.nextInt(operators.length)];
int num = random.nextInt(10) + 1; int num = random.nextInt(10) + 1;
question.append(" ").append(op).append(" " ).append(num); question.append(" ").append(op).append(" " ).append(num);
}
question.append(" = ?");
return question.toString();
} }
question.append(" = ?");
return question.toString();
}
} }

@ -3,51 +3,51 @@
* *
*/ */
public class UserInfo { public class UserInfo {
private String username; private String username;
private String password; private String password;
private String userType; private String userType;
/** /**
* *
* @param username * @param username
* @param password * @param password
* @param userType // * @param userType //
*/ */
public UserInfo(String username, String password, String userType) { public UserInfo(String username, String password, String userType) {
this.username = username; this.username = username;
this.password = password; this.password = password;
this.userType = userType; this.userType = userType;
} }
/** /**
* *
* @return * @return
*/ */
public String getUsername() { public String getUsername() {
return username; return username;
} }
/** /**
* *
* @return * @return
*/ */
public String getPassword() { public String getPassword() {
return password; return password;
} }
/** /**
* *
* @return * @return
*/ */
public String getUserType() { public String getUserType() {
return userType; return userType;
} }
/** /**
* *
* @param userType * @param userType
*/ */
public void setUserType(String userType) { public void setUserType(String userType) {
this.userType = userType; this.userType = userType;
} }
} }
Loading…
Cancel
Save