|
|
|
|
@ -2,47 +2,89 @@ import java.util.Random;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 高中题目生成策略实现类。
|
|
|
|
|
* <p>
|
|
|
|
|
* 随机生成包含基本运算和三角函数的数学表达式。
|
|
|
|
|
*
|
|
|
|
|
* <p>随机生成包含基本运算符和三角函数的数学表达式。
|
|
|
|
|
* 操作数数量范围为 1–5;当只有 1 个操作数时,表达式必定包含三角函数。</p>
|
|
|
|
|
*/
|
|
|
|
|
public class HighSchoolQuestionStrategy implements QuestionStrategy {
|
|
|
|
|
|
|
|
|
|
/** 基本四则运算符。 */
|
|
|
|
|
private static final String[] BASIC_OPS = {"+", "-", "*", "/"};
|
|
|
|
|
|
|
|
|
|
/** 支持的三角函数。 */
|
|
|
|
|
private static final String[] TRIG_FUNCS = {"sin", "cos", "tan"};
|
|
|
|
|
|
|
|
|
|
/** 随机数生成器。 */
|
|
|
|
|
private final Random random = new Random();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成随机高中数学题。
|
|
|
|
|
*
|
|
|
|
|
* @return 包含至少一个三角函数的数学表达式字符串
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public String generateQuestion() {
|
|
|
|
|
// 随机操作数个数:2~5
|
|
|
|
|
int operandsCount = random.nextInt(4) + 2;
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
// 操作数个数:1~5
|
|
|
|
|
int operandsCount = random.nextInt(5) + 1;
|
|
|
|
|
StringBuilder expression = new StringBuilder();
|
|
|
|
|
boolean hasTrig = false;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < operandsCount; i++) {
|
|
|
|
|
int num = random.nextInt(100) + 1;
|
|
|
|
|
// 仅一个操作数时,强制生成一个三角函数表达式
|
|
|
|
|
if (operandsCount == 1) {
|
|
|
|
|
return buildTrigOperand();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 每个操作数有概率带三角函数
|
|
|
|
|
if (random.nextBoolean()) {
|
|
|
|
|
String func = TRIG_FUNCS[random.nextInt(TRIG_FUNCS.length)];
|
|
|
|
|
sb.append(func).append("(").append(num).append(")");
|
|
|
|
|
// 生成多个操作数与运算符
|
|
|
|
|
for (int i = 0; i < operandsCount; i++) {
|
|
|
|
|
String operand = buildOperand();
|
|
|
|
|
expression.append(operand);
|
|
|
|
|
if (operand.matches(".*(sin|cos|tan).*")) {
|
|
|
|
|
hasTrig = true;
|
|
|
|
|
} else {
|
|
|
|
|
sb.append(num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加运算符(除最后一个操作数外)
|
|
|
|
|
if (i != operandsCount - 1) {
|
|
|
|
|
String op = BASIC_OPS[random.nextInt(BASIC_OPS.length)];
|
|
|
|
|
sb.append(" ").append(op).append(" ");
|
|
|
|
|
expression.append(' ')
|
|
|
|
|
.append(BASIC_OPS[random.nextInt(BASIC_OPS.length)])
|
|
|
|
|
.append(' ');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 确保至少包含一个三角函数
|
|
|
|
|
if (!hasTrig) {
|
|
|
|
|
String func = TRIG_FUNCS[random.nextInt(TRIG_FUNCS.length)];
|
|
|
|
|
int num = random.nextInt(100) + 1;
|
|
|
|
|
return func + "(" + num + ") + " + sb;
|
|
|
|
|
expression.append(" + ").append(buildTrigOperand());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return expression.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 随机生成一个操作数,可为普通数字或三角函数表达式。
|
|
|
|
|
*/
|
|
|
|
|
private String buildOperand() {
|
|
|
|
|
int num = random.nextInt(100) + 1;
|
|
|
|
|
if (random.nextBoolean()) {
|
|
|
|
|
return buildTrigOperandWithNumber(num);
|
|
|
|
|
}
|
|
|
|
|
return String.valueOf(num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 随机生成一个仅包含三角函数的操作数。
|
|
|
|
|
*/
|
|
|
|
|
private String buildTrigOperand() {
|
|
|
|
|
int num = random.nextInt(100) + 1;
|
|
|
|
|
return buildTrigOperandWithNumber(num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.toString();
|
|
|
|
|
/**
|
|
|
|
|
* 生成指定数字的三角函数操作数。
|
|
|
|
|
*
|
|
|
|
|
* @param number 角度值(1~100)
|
|
|
|
|
* @return 形如 sin(45) 的字符串
|
|
|
|
|
*/
|
|
|
|
|
private String buildTrigOperandWithNumber(int number) {
|
|
|
|
|
String func = TRIG_FUNCS[random.nextInt(TRIG_FUNCS.length)];
|
|
|
|
|
return func + "(" + number + ")";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|