|
|
@ -1,45 +1,26 @@
|
|
|
|
import java.util.HashSet;
|
|
|
|
package services;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import interfaces.QuestionInterface;
|
|
|
|
|
|
|
|
import models.DifficultyLevel;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class QuestionGenerator {
|
|
|
|
public class MathQuestion implements QuestionInterface {
|
|
|
|
|
|
|
|
private String questionText;
|
|
|
|
|
|
|
|
private DifficultyLevel difficulty;
|
|
|
|
private Random random;
|
|
|
|
private Random random;
|
|
|
|
private FileManager fileManager;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public QuestionGenerator() {
|
|
|
|
|
|
|
|
random = new Random();
|
|
|
|
|
|
|
|
fileManager = new FileManager();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String[] generateQuestions(DifficultyLevel level, int count, String username) {
|
|
|
|
|
|
|
|
Set<String> existingQuestions = fileManager.loadExistingQuestions(username);
|
|
|
|
|
|
|
|
Set<String> newQuestions = new HashSet<>();
|
|
|
|
|
|
|
|
String[] questions = new String[count];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
|
|
|
String question;
|
|
|
|
|
|
|
|
int attempts = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
public MathQuestion(DifficultyLevel difficulty) {
|
|
|
|
question = generateSingleQuestion(level, i + 1);
|
|
|
|
this.difficulty = difficulty;
|
|
|
|
attempts++;
|
|
|
|
this.random = new Random();
|
|
|
|
if (attempts > 100) {
|
|
|
|
this.questionText = generateQuestion();
|
|
|
|
return null; // 避免无限循环
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (existingQuestions.contains(question) || newQuestions.contains(question));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newQuestions.add(question);
|
|
|
|
@Override
|
|
|
|
questions[i] = question;
|
|
|
|
public String generateQuestion() {
|
|
|
|
}
|
|
|
|
int operandCount = random.nextInt(5) + 1;
|
|
|
|
|
|
|
|
StringBuilder question = new StringBuilder();
|
|
|
|
return questions;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String generateSingleQuestion(DifficultyLevel level, int questionNumber) {
|
|
|
|
|
|
|
|
int operandCount = random.nextInt(5) + 1; // 1-5个操作数
|
|
|
|
|
|
|
|
StringBuilder question = new StringBuilder(questionNumber + ". ");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (level) {
|
|
|
|
switch (difficulty) {
|
|
|
|
case PRIMARY:
|
|
|
|
case PRIMARY:
|
|
|
|
question.append(generatePrimaryQuestion(operandCount));
|
|
|
|
question.append(generatePrimaryQuestion(operandCount));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
@ -55,6 +36,21 @@ public class QuestionGenerator {
|
|
|
|
return question.toString();
|
|
|
|
return question.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public boolean isValid() {
|
|
|
|
|
|
|
|
return questionText != null && !questionText.trim().isEmpty();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public String getQuestionText() {
|
|
|
|
|
|
|
|
return questionText;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public DifficultyLevel getDifficulty() {
|
|
|
|
|
|
|
|
return difficulty;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String generatePrimaryQuestion(int operandCount) {
|
|
|
|
private String generatePrimaryQuestion(int operandCount) {
|
|
|
|
String[] operators = {"+", "-", "*", "/"};
|
|
|
|
String[] operators = {"+", "-", "*", "/"};
|
|
|
|
StringBuilder question = new StringBuilder();
|
|
|
|
StringBuilder question = new StringBuilder();
|
|
|
@ -65,19 +61,15 @@ public class QuestionGenerator {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
question.append(random.nextInt(100) + 1);
|
|
|
|
question.append(random.nextInt(100) + 1);
|
|
|
|
|
|
|
|
|
|
|
|
// 随机添加括号(小学难度)
|
|
|
|
|
|
|
|
if (operandCount > 2 && random.nextDouble() < 0.3) {
|
|
|
|
if (operandCount > 2 && random.nextDouble() < 0.3) {
|
|
|
|
question.insert(0, "(").append(")");
|
|
|
|
question.insert(0, "(").append(")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return question.toString();
|
|
|
|
return question.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String generateJuniorQuestion(int operandCount) {
|
|
|
|
private String generateJuniorQuestion(int operandCount) {
|
|
|
|
String question = generatePrimaryQuestion(operandCount);
|
|
|
|
String question = generatePrimaryQuestion(operandCount);
|
|
|
|
|
|
|
|
|
|
|
|
// 确保至少有一个平方或开根号
|
|
|
|
|
|
|
|
if (random.nextBoolean()) {
|
|
|
|
if (random.nextBoolean()) {
|
|
|
|
return "√" + (random.nextInt(100) + 1) + " + " + question;
|
|
|
|
return "√" + (random.nextInt(100) + 1) + " + " + question;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -88,8 +80,6 @@ public class QuestionGenerator {
|
|
|
|
private String generateSeniorQuestion(int operandCount) {
|
|
|
|
private String generateSeniorQuestion(int operandCount) {
|
|
|
|
String question = generatePrimaryQuestion(operandCount);
|
|
|
|
String question = generatePrimaryQuestion(operandCount);
|
|
|
|
String[] trigFunctions = {"sin", "cos", "tan"};
|
|
|
|
String[] trigFunctions = {"sin", "cos", "tan"};
|
|
|
|
|
|
|
|
|
|
|
|
// 确保至少有一个三角函数
|
|
|
|
|
|
|
|
return trigFunctions[random.nextInt(trigFunctions.length)] +
|
|
|
|
return trigFunctions[random.nextInt(trigFunctions.length)] +
|
|
|
|
"(" + (random.nextInt(90) + 1) + "°) + " + question;
|
|
|
|
"(" + (random.nextInt(90) + 1) + "°) + " + question;
|
|
|
|
}
|
|
|
|
}
|