|
|
|
|
@ -9,31 +9,31 @@ public class ChoiceQuestionGenerator {
|
|
|
|
|
private final Random random = new Random();
|
|
|
|
|
private final int MAX_OPERAND_VALUE = 50; // 降低数值范围,便于计算
|
|
|
|
|
private final int MIN_OPERAND_VALUE = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成指定数量的选择题
|
|
|
|
|
*/
|
|
|
|
|
public List<Question> generateQuestions(Level level, int count) {
|
|
|
|
|
List<Question> questions = new ArrayList<>();
|
|
|
|
|
Set<String> usedQuestions = new HashSet<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int attempts = 0;
|
|
|
|
|
int maxAttempts = count * 100; // 最大尝试次数
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (questions.size() < count && attempts < maxAttempts) {
|
|
|
|
|
Question question = generateSingleQuestion(level, questions.size() + 1);
|
|
|
|
|
String questionKey = question.getQuestionText();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!usedQuestions.contains(questionKey)) {
|
|
|
|
|
usedQuestions.add(questionKey);
|
|
|
|
|
questions.add(question);
|
|
|
|
|
}
|
|
|
|
|
attempts++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return questions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成单个选择题
|
|
|
|
|
*/
|
|
|
|
|
@ -49,38 +49,38 @@ public class ChoiceQuestionGenerator {
|
|
|
|
|
return generatePrimaryQuestion(questionNumber);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成小学题目
|
|
|
|
|
*/
|
|
|
|
|
private Question generatePrimaryQuestion(int questionNumber) {
|
|
|
|
|
int a = MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE);
|
|
|
|
|
int b = MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String operation = getRandomOperation("+*-");
|
|
|
|
|
String questionText = String.format("%d %s %d = ?", a, operation, b);
|
|
|
|
|
int correctAnswer = calculate(a, b, operation);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成错误选项
|
|
|
|
|
List<Integer> options = generateWrongOptions(correctAnswer);
|
|
|
|
|
options.add(correctAnswer);
|
|
|
|
|
Collections.shuffle(options);
|
|
|
|
|
|
|
|
|
|
return new Question(questionText,
|
|
|
|
|
String.valueOf(options.get(0)),
|
|
|
|
|
String.valueOf(options.get(1)),
|
|
|
|
|
String.valueOf(options.get(2)),
|
|
|
|
|
String.valueOf(options.get(3)),
|
|
|
|
|
String.valueOf(correctAnswer),
|
|
|
|
|
|
|
|
|
|
return new Question(questionText,
|
|
|
|
|
String.valueOf(options.get(0)),
|
|
|
|
|
String.valueOf(options.get(1)),
|
|
|
|
|
String.valueOf(options.get(2)),
|
|
|
|
|
String.valueOf(options.get(3)),
|
|
|
|
|
String.valueOf(correctAnswer),
|
|
|
|
|
questionNumber);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成初中题目
|
|
|
|
|
*/
|
|
|
|
|
private Question generateJuniorQuestion(int questionNumber) {
|
|
|
|
|
int a = MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 50%概率生成平方或开方题目
|
|
|
|
|
if (random.nextBoolean()) {
|
|
|
|
|
// 平方题目
|
|
|
|
|
@ -89,13 +89,13 @@ public class ChoiceQuestionGenerator {
|
|
|
|
|
List<Integer> options = generateWrongOptions(correctAnswer);
|
|
|
|
|
options.add(correctAnswer);
|
|
|
|
|
Collections.shuffle(options);
|
|
|
|
|
|
|
|
|
|
return new Question(questionText,
|
|
|
|
|
String.valueOf(options.get(0)),
|
|
|
|
|
String.valueOf(options.get(1)),
|
|
|
|
|
String.valueOf(options.get(2)),
|
|
|
|
|
String.valueOf(options.get(3)),
|
|
|
|
|
String.valueOf(correctAnswer),
|
|
|
|
|
|
|
|
|
|
return new Question(questionText,
|
|
|
|
|
String.valueOf(options.get(0)),
|
|
|
|
|
String.valueOf(options.get(1)),
|
|
|
|
|
String.valueOf(options.get(2)),
|
|
|
|
|
String.valueOf(options.get(3)),
|
|
|
|
|
String.valueOf(correctAnswer),
|
|
|
|
|
questionNumber);
|
|
|
|
|
} else {
|
|
|
|
|
// 开方题目
|
|
|
|
|
@ -105,42 +105,216 @@ public class ChoiceQuestionGenerator {
|
|
|
|
|
List<Integer> options = generateWrongOptions(correctAnswer);
|
|
|
|
|
options.add(correctAnswer);
|
|
|
|
|
Collections.shuffle(options);
|
|
|
|
|
|
|
|
|
|
return new Question(questionText,
|
|
|
|
|
String.valueOf(options.get(0)),
|
|
|
|
|
String.valueOf(options.get(1)),
|
|
|
|
|
String.valueOf(options.get(2)),
|
|
|
|
|
String.valueOf(options.get(3)),
|
|
|
|
|
String.valueOf(correctAnswer),
|
|
|
|
|
|
|
|
|
|
return new Question(questionText,
|
|
|
|
|
String.valueOf(options.get(0)),
|
|
|
|
|
String.valueOf(options.get(1)),
|
|
|
|
|
String.valueOf(options.get(2)),
|
|
|
|
|
String.valueOf(options.get(3)),
|
|
|
|
|
String.valueOf(correctAnswer),
|
|
|
|
|
questionNumber);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成高中题目
|
|
|
|
|
*/
|
|
|
|
|
* 生成高中题目
|
|
|
|
|
*/
|
|
|
|
|
private Question generateSeniorQuestion(int questionNumber) {
|
|
|
|
|
int angle = random.nextInt(360);
|
|
|
|
|
// 使用常见角度: 0°, 30°, 45°, 60°, 90°, 120°, 135°, 150°, 180°, 210°, 225°, 270°, 300°, 315°, 330°
|
|
|
|
|
int[] commonAngles = {0, 30, 45, 60, 90, 120, 135, 150, 180, 210, 225, 270, 300, 315, 330};
|
|
|
|
|
int angle = commonAngles[random.nextInt(commonAngles.length)];
|
|
|
|
|
String[] functions = {"sin", "cos", "tan"};
|
|
|
|
|
String function = functions[random.nextInt(functions.length)];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String questionText = String.format("%s(%d°) = ?", function, angle);
|
|
|
|
|
double result = calculateTrigFunction(function, angle);
|
|
|
|
|
int correctAnswer = (int) Math.round(result * 100) / 100; // 保留两位小数
|
|
|
|
|
|
|
|
|
|
List<Integer> options = generateWrongOptions(correctAnswer);
|
|
|
|
|
options.add(correctAnswer);
|
|
|
|
|
|
|
|
|
|
// 生成选项
|
|
|
|
|
List<String> options = generateTrigOptions(result);
|
|
|
|
|
// 使用格式化后的值作为正确答案,确保与选项一致
|
|
|
|
|
String correctAnswer = formatTrigValue(result);
|
|
|
|
|
|
|
|
|
|
// 随机打乱选项
|
|
|
|
|
Collections.shuffle(options);
|
|
|
|
|
|
|
|
|
|
return new Question(questionText,
|
|
|
|
|
String.valueOf(options.get(0)),
|
|
|
|
|
String.valueOf(options.get(1)),
|
|
|
|
|
String.valueOf(options.get(2)),
|
|
|
|
|
String.valueOf(options.get(3)),
|
|
|
|
|
String.valueOf(correctAnswer),
|
|
|
|
|
questionNumber);
|
|
|
|
|
|
|
|
|
|
return new Question(questionText,
|
|
|
|
|
options.get(0),
|
|
|
|
|
options.get(1),
|
|
|
|
|
options.get(2),
|
|
|
|
|
options.get(3),
|
|
|
|
|
correctAnswer,
|
|
|
|
|
questionNumber);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成三角函数题目的选项
|
|
|
|
|
*/
|
|
|
|
|
private List<String> generateTrigOptions(double correctAnswer) {
|
|
|
|
|
List<String> options = new ArrayList<>();
|
|
|
|
|
Set<String> usedOptions = new HashSet<>();
|
|
|
|
|
|
|
|
|
|
// 格式化正确答案,保留适当的小数位数
|
|
|
|
|
String formattedCorrectAnswer = formatTrigValue(correctAnswer);
|
|
|
|
|
options.add(formattedCorrectAnswer);
|
|
|
|
|
usedOptions.add(formattedCorrectAnswer);
|
|
|
|
|
|
|
|
|
|
// 添加常见三角函数值作为错误选项
|
|
|
|
|
String[] commonValues = {"0", "0.5", "0.71", "0.87", "1", "1.73", "-0.5", "-0.71", "-0.87", "-1", "-1.73", "undefined"};
|
|
|
|
|
List<String> commonValuesList = new ArrayList<>(Arrays.asList(commonValues));
|
|
|
|
|
|
|
|
|
|
// 打乱常见值列表
|
|
|
|
|
Collections.shuffle(commonValuesList);
|
|
|
|
|
|
|
|
|
|
// 添加错误选项
|
|
|
|
|
int wrongOptionsNeeded = 3;
|
|
|
|
|
for (String value : commonValuesList) {
|
|
|
|
|
if (wrongOptionsNeeded <= 0) break;
|
|
|
|
|
// 确保不添加与正确答案相同的选项
|
|
|
|
|
if (!usedOptions.contains(value) && !value.equals(formattedCorrectAnswer)) {
|
|
|
|
|
options.add(value);
|
|
|
|
|
usedOptions.add(value);
|
|
|
|
|
wrongOptionsNeeded--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果还需要更多选项,则生成接近正确答案的选项
|
|
|
|
|
while (options.size() < 4) {
|
|
|
|
|
String wrongOption = generateNearbyValue(correctAnswer, usedOptions);
|
|
|
|
|
// 确保新生成的选项不重复且不与正确答案相同
|
|
|
|
|
if (!usedOptions.contains(wrongOption) && !wrongOption.equals(formattedCorrectAnswer)) {
|
|
|
|
|
options.add(wrongOption);
|
|
|
|
|
usedOptions.add(wrongOption);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 格式化三角函数值
|
|
|
|
|
*/
|
|
|
|
|
private String formatTrigValue(double value) {
|
|
|
|
|
// 对于特殊值进行精确表示
|
|
|
|
|
if (Math.abs(value) < 1e-10) return "0"; // 接近0的值
|
|
|
|
|
if (Math.abs(value - 1.0) < 1e-10) return "1";
|
|
|
|
|
if (Math.abs(value + 1.0) < 1e-10) return "-1";
|
|
|
|
|
if (Math.abs(value - 0.5) < 1e-10) return "0.5";
|
|
|
|
|
if (Math.abs(value + 0.5) < 1e-10) return "-0.5";
|
|
|
|
|
if (Math.abs(value - Math.sqrt(2)/2) < 1e-10) return "0.71"; // √2/2 ≈ 0.707
|
|
|
|
|
if (Math.abs(value + Math.sqrt(2)/2) < 1e-10) return "-0.71";
|
|
|
|
|
if (Math.abs(value - Math.sqrt(3)/2) < 1e-10) return "0.87"; // √3/2 ≈ 0.866
|
|
|
|
|
if (Math.abs(value + Math.sqrt(3)/2) < 1e-10) return "-0.87";
|
|
|
|
|
if (Math.abs(value - Math.sqrt(3)) < 1e-10) return "1.73"; // √3 ≈ 1.732
|
|
|
|
|
if (Math.abs(value + Math.sqrt(3)) < 1e-10) return "-1.73";
|
|
|
|
|
|
|
|
|
|
// 对于tan(90°)等未定义值
|
|
|
|
|
if (Double.isInfinite(value) || Double.isNaN(value)) {
|
|
|
|
|
return "undefined";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 其他值保留两位小数
|
|
|
|
|
return String.format("%.2f", value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成接近正确答案的选项
|
|
|
|
|
*/
|
|
|
|
|
private String generateNearbyValue(double correctAnswer, Set<String> usedOptions) {
|
|
|
|
|
// 对于特殊值直接返回简单值
|
|
|
|
|
if (Double.isInfinite(correctAnswer) || Double.isNaN(correctAnswer)) {
|
|
|
|
|
return "0";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 生成在正确答案附近的值
|
|
|
|
|
double variation = (random.nextDouble() - 0.5) * 2; // -1 到 1 之间的随机数
|
|
|
|
|
double wrongAnswer = correctAnswer + variation;
|
|
|
|
|
|
|
|
|
|
// 格式化生成的值
|
|
|
|
|
String formatted = formatTrigValue(wrongAnswer);
|
|
|
|
|
|
|
|
|
|
// 确保不会生成与已使用选项相同的值,最多尝试10次
|
|
|
|
|
int attempts = 0;
|
|
|
|
|
while (usedOptions.contains(formatted) && attempts < 10) {
|
|
|
|
|
variation = (random.nextDouble() - 0.5) * 2;
|
|
|
|
|
wrongAnswer = correctAnswer + variation;
|
|
|
|
|
formatted = formatTrigValue(wrongAnswer);
|
|
|
|
|
attempts++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return formatted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 计算三角函数
|
|
|
|
|
*/
|
|
|
|
|
private double calculateTrigFunction(String function, int angle) {
|
|
|
|
|
double radians = Math.toRadians(angle);
|
|
|
|
|
switch (function) {
|
|
|
|
|
case "sin":
|
|
|
|
|
// 对特定角度返回精确值
|
|
|
|
|
switch (angle) {
|
|
|
|
|
case 0: return 0;
|
|
|
|
|
case 30: return 0.5;
|
|
|
|
|
case 45: return Math.sqrt(2)/2;
|
|
|
|
|
case 60: return Math.sqrt(3)/2;
|
|
|
|
|
case 90: return 1;
|
|
|
|
|
case 120: return Math.sqrt(3)/2;
|
|
|
|
|
case 135: return Math.sqrt(2)/2;
|
|
|
|
|
case 150: return 0.5;
|
|
|
|
|
case 180: return 0;
|
|
|
|
|
case 210: return -0.5;
|
|
|
|
|
case 225: return -Math.sqrt(2)/2;
|
|
|
|
|
case 270: return -1;
|
|
|
|
|
case 300: return -Math.sqrt(3)/2;
|
|
|
|
|
case 315: return -Math.sqrt(2)/2;
|
|
|
|
|
case 330: return -0.5;
|
|
|
|
|
default: return Math.sin(radians);
|
|
|
|
|
}
|
|
|
|
|
case "cos":
|
|
|
|
|
// 对特定角度返回精确值
|
|
|
|
|
switch (angle) {
|
|
|
|
|
case 0: return 1;
|
|
|
|
|
case 30: return Math.sqrt(3)/2;
|
|
|
|
|
case 45: return Math.sqrt(2)/2;
|
|
|
|
|
case 60: return 0.5;
|
|
|
|
|
case 90: return 0;
|
|
|
|
|
case 120: return -0.5;
|
|
|
|
|
case 135: return -Math.sqrt(2)/2;
|
|
|
|
|
case 150: return -Math.sqrt(3)/2;
|
|
|
|
|
case 180: return -1;
|
|
|
|
|
case 210: return -Math.sqrt(3)/2;
|
|
|
|
|
case 225: return -Math.sqrt(2)/2;
|
|
|
|
|
case 270: return 0;
|
|
|
|
|
case 300: return 0.5;
|
|
|
|
|
case 315: return Math.sqrt(2)/2;
|
|
|
|
|
case 330: return Math.sqrt(3)/2;
|
|
|
|
|
default: return Math.cos(radians);
|
|
|
|
|
}
|
|
|
|
|
case "tan":
|
|
|
|
|
// 对特定角度返回精确值或未定义
|
|
|
|
|
switch (angle) {
|
|
|
|
|
case 0: return 0;
|
|
|
|
|
case 30: return Math.sqrt(3)/3;
|
|
|
|
|
case 45: return 1;
|
|
|
|
|
case 60: return Math.sqrt(3);
|
|
|
|
|
case 90: return Double.POSITIVE_INFINITY; // 未定义
|
|
|
|
|
case 120: return -Math.sqrt(3);
|
|
|
|
|
case 135: return -1;
|
|
|
|
|
case 150: return -Math.sqrt(3)/3;
|
|
|
|
|
case 180: return 0;
|
|
|
|
|
case 210: return Math.sqrt(3)/3;
|
|
|
|
|
case 225: return 1;
|
|
|
|
|
case 270: return Double.POSITIVE_INFINITY; // 未定义
|
|
|
|
|
case 300: return -Math.sqrt(3);
|
|
|
|
|
case 315: return -1;
|
|
|
|
|
case 330: return -Math.sqrt(3)/3;
|
|
|
|
|
default: return Math.tan(radians);
|
|
|
|
|
}
|
|
|
|
|
default: return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成错误选项
|
|
|
|
|
*/
|
|
|
|
|
@ -148,7 +322,7 @@ public class ChoiceQuestionGenerator {
|
|
|
|
|
List<Integer> options = new ArrayList<>();
|
|
|
|
|
Set<Integer> usedOptions = new HashSet<>();
|
|
|
|
|
usedOptions.add(correctAnswer);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (options.size() < 3) {
|
|
|
|
|
int wrongAnswer;
|
|
|
|
|
if (correctAnswer == 0) {
|
|
|
|
|
@ -158,16 +332,16 @@ public class ChoiceQuestionGenerator {
|
|
|
|
|
int variation = random.nextInt(Math.max(1, Math.abs(correctAnswer) / 2)) + 1;
|
|
|
|
|
wrongAnswer = correctAnswer + (random.nextBoolean() ? variation : -variation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!usedOptions.contains(wrongAnswer)) {
|
|
|
|
|
options.add(wrongAnswer);
|
|
|
|
|
usedOptions.add(wrongAnswer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 计算基本运算
|
|
|
|
|
*/
|
|
|
|
|
@ -180,20 +354,8 @@ public class ChoiceQuestionGenerator {
|
|
|
|
|
default: return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 计算三角函数
|
|
|
|
|
*/
|
|
|
|
|
private double calculateTrigFunction(String function, int angle) {
|
|
|
|
|
double radians = Math.toRadians(angle);
|
|
|
|
|
switch (function) {
|
|
|
|
|
case "sin": return Math.sin(radians);
|
|
|
|
|
case "cos": return Math.cos(radians);
|
|
|
|
|
case "tan": return Math.tan(radians);
|
|
|
|
|
default: return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取随机运算符
|
|
|
|
|
*/
|
|
|
|
|
@ -201,7 +363,7 @@ public class ChoiceQuestionGenerator {
|
|
|
|
|
int index = random.nextInt(operations.length());
|
|
|
|
|
return String.valueOf(operations.charAt(index));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 难度级别枚举
|
|
|
|
|
*/
|
|
|
|
|
|