You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wang/src/PaperGenerator.java

121 lines
4.4 KiB

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class PaperGenerator {
private IQuestionGenerator questionGenerator;
private User currentUser;
private Random random;
public PaperGenerator(User user) {
this.currentUser = user;
this.questionGenerator = QuestionGeneratorFactory.createGenerator(user.getUserType());
this.random = new Random();
}
public void setUserType(UserType userType) {
this.questionGenerator = QuestionGeneratorFactory.createGenerator(userType);
}
public String[] generatePaper(int questionCount, Filemanager fileManager) {
if (questionCount < 10 || questionCount > 30) {
throw new IllegalArgumentException("题目数量必须在10-30之间");
}
Set<String> allHistoryQuestions = fileManager.loadAllQuestions(currentUser.getUsername());
System.out.println("历史题目数量: " + allHistoryQuestions.size());
String[] questions = new String[questionCount];
Set<String> usedQuestions = new HashSet<>();
for (int i = 0; i < questionCount; i++) {
String question;
int attempt = 0;
int maxAttempts = 500;
do {
question = questionGenerator.generateQuestion();
attempt++;
if (attempt > 100) {
question = generateSimpleQuestion();
}
if (attempt >= maxAttempts) {
System.out.println("警告:第 " + (i+1) + " 题尝试次数过多,使用备用题目");
question = generateFallbackQuestion();
break;
}
} while (allHistoryQuestions.contains(question) ||
usedQuestions.contains(question) ||
!questionGenerator.isValidQuestion(question));
usedQuestions.add(question);
questions[i] = (i + 1) + ". " + question;
System.out.println("成功生成第 " + (i+1) + " 题");
}
return questions;
}
private String generateSimpleQuestion() {
switch (currentUser.getUserType()) {
case PRIMARY:
return generateSimplePrimaryQuestion();
case JUNIOR:
return generateSimpleJuniorQuestion();
case SENIOR:
return generateSimpleSeniorQuestion();
default:
return generateSimplePrimaryQuestion();
}
}
private String generateSimplePrimaryQuestion() {
int a = random.nextInt(100) + 1;
int b = random.nextInt(100) + 1;
String[] ops = {"+", "-", "*", "/"};
String op = ops[random.nextInt(ops.length)];
return a + " " + op + " " + b + " = ";
}
private String generateSimpleJuniorQuestion() {
int a = random.nextInt(100) + 1;
int b = random.nextInt(100) + 1;
String[] ops = {"+", "-", "*", "/"};
String op = ops[random.nextInt(ops.length)];
if (random.nextBoolean()) {
return "√" + a + " " + op + " " + b + " = ";
} else {
return a + "² " + op + " " + b + " = ";
}
}
private String generateSimpleSeniorQuestion() {
int a = random.nextInt(100) + 1;
int angle = random.nextInt(360) + 1;
String[] ops = {"+", "-", "*", "/"};
String op = ops[random.nextInt(ops.length)];
String[] trig = {"sin", "cos", "tan"};
String trigFunc = trig[random.nextInt(trig.length)];
return trigFunc + "(" + angle + "°) " + op + " " + a + " = ";
}
private String generateFallbackQuestion() {
switch (currentUser.getUserType()) {
case PRIMARY:
return (random.nextInt(100) + 1) + " + " + (random.nextInt(100) + 1) + " = ";
case JUNIOR:
return "√" + (random.nextInt(100) + 1) + " + " + (random.nextInt(100) + 1) + " = ";
case SENIOR:
return "sin(" + (random.nextInt(360) + 1) + "°) + " + (random.nextInt(100) + 1) + " = ";
default:
return (random.nextInt(100) + 1) + " + " + (random.nextInt(100) + 1) + " = ";
}
}
}