Compare commits
12 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
07c91d030d | 4 months ago |
|
|
67d4f9f7f1 | 4 months ago |
|
|
cbcb7ee143 | 4 months ago |
|
|
92e2977ecb | 4 months ago |
|
|
76050ec231 | 4 months ago |
|
|
c663a3e433 | 4 months ago |
|
|
6a88bc6ace | 4 months ago |
|
|
6eb4dcc91e | 4 months ago |
|
|
aab9148420 | 4 months ago |
|
|
be5348cb77 | 4 months ago |
|
|
2ecec7e23f | 4 months ago |
|
|
b980c61e8e | 4 months ago |
@ -0,0 +1,67 @@
|
|||||||
|
import java.text.DecimalFormat;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class ElementaryProblemGenerator extends AbstractProblemGenerator {
|
||||||
|
public ElementaryProblemGenerator(Random random, DecimalFormat df) {
|
||||||
|
super(random, df);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MathProblem generate() {
|
||||||
|
// 小学题目要求:答案不得为负数。最多尝试若干次生成,若仍为负则回退到简单加法题。
|
||||||
|
int maxAttempts = 50;
|
||||||
|
for (int attempt = 0; attempt < maxAttempts; attempt++) {
|
||||||
|
int operandCount = random.nextInt(4) + 2; // 2-5个操作数
|
||||||
|
String[] operators = {"+", "-", "*", "/"};
|
||||||
|
int[] operands = new int[operandCount];
|
||||||
|
String[] selectedOperators = new String[operandCount - 1];
|
||||||
|
for (int i = 0; i < operandCount; i++) operands[i] = random.nextInt(100) + 1;
|
||||||
|
// 运算符选择(避免连乘/连除,调整概率)
|
||||||
|
String previousOperator = null;
|
||||||
|
double pPlus = 0.45, pMinus = 0.35, pTimes = 0.15, pDivide = 0.05;
|
||||||
|
for (int i = 0; i < operandCount - 1; i++) {
|
||||||
|
String operator;
|
||||||
|
if ("*".equals(previousOperator) || "/".equals(previousOperator)) {
|
||||||
|
double randValue = random.nextDouble();
|
||||||
|
double total = pPlus + pMinus;
|
||||||
|
operator = randValue < (pPlus / total) ? "+" : "-";
|
||||||
|
} else {
|
||||||
|
double randValue = random.nextDouble();
|
||||||
|
if (randValue < pPlus) operator = "+";
|
||||||
|
else if (randValue < pPlus + pMinus) operator = "-";
|
||||||
|
else if (randValue < pPlus + pMinus + pTimes) operator = "*";
|
||||||
|
else operator = "/";
|
||||||
|
}
|
||||||
|
selectedOperators[i] = operator;
|
||||||
|
previousOperator = operator;
|
||||||
|
}
|
||||||
|
// 括号选择
|
||||||
|
int startPos = findBracketStart(toDoubleArray(operands), selectedOperators, 0.3);
|
||||||
|
String expression;
|
||||||
|
double result;
|
||||||
|
if (startPos != -1) {
|
||||||
|
expression = buildExpressionWithBrackets(operands, selectedOperators, startPos);
|
||||||
|
result = evaluateExpressionWithBrackets(operands, selectedOperators, startPos);
|
||||||
|
} else {
|
||||||
|
StringBuilder expBuilder = new StringBuilder();
|
||||||
|
expBuilder.append(operands[0]);
|
||||||
|
result = operands[0];
|
||||||
|
for (int i = 0; i < operandCount - 1; i++) {
|
||||||
|
expBuilder.append(" ").append(selectedOperators[i]).append(" ").append(operands[i + 1]);
|
||||||
|
result = applyOperator(result, operands[i + 1], selectedOperators[i]);
|
||||||
|
}
|
||||||
|
expression = expBuilder.toString();
|
||||||
|
}
|
||||||
|
if (result >= 0) {
|
||||||
|
return new MathProblem(expression, df.format(result), "小学");
|
||||||
|
}
|
||||||
|
// 如果为负数,继续下一次尝试
|
||||||
|
}
|
||||||
|
// 回退:生成一个保证非负的简单加法题
|
||||||
|
int a = random.nextInt(100) + 1;
|
||||||
|
int b = random.nextInt(100) + 1;
|
||||||
|
String expression = a + " + " + b;
|
||||||
|
double result = a + b;
|
||||||
|
return new MathProblem(expression, df.format(result), "小学");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue