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.
TestPaperGenerationSystem/src/MiddleSchoolProblemGenerato...

38 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import java.util.ArrayList;
import java.util.List;
/**
* 初中题目生成器(具体策略)。
* 首先生成一个允许出现负数的标准四则运算题目,然后确保至少包含一个平方或开方运算。
*/
public class MiddleSchoolProblemGenerator extends AbstractProblemGenerator {
@Override
protected Equation createProblem() {
// 1. 调用父类的核心方法,并传入 false允许生成包含负数的标准表达式。
Equation basicEquation = createBaseArithmeticProblem(false);
// 2. 将基础表达式元素转为可修改的列表
List<String> operands = new ArrayList<>(basicEquation.operands());
List<Operator> operators = new ArrayList<>(basicEquation.operators());
// 3. 随机选择一个操作数进行变形
int modifyIndex = random.nextInt(operands.size());
String targetOperand = operands.get(modifyIndex).replace("(", "").replace(")", "");
// 4. 随机决定是进行平方还是开方运算
if (random.nextBoolean()) {
// 平方运算
String squaredExpression = String.format("(%s^2)", targetOperand);
operands.set(modifyIndex, squaredExpression);
} else {
// 开方运算
int base = getRandomNumber(2, 10);
int valueToRoot = base * base;
String sqrtExpression = String.format("sqrt(%d)", valueToRoot);
operands.set(modifyIndex, sqrtExpression);
}
return new Equation(operands, operators);
}
}