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.
113 lines
3.5 KiB
113 lines
3.5 KiB
// MiddleQueSeting.java
|
|
package com.example.myapp.service;
|
|
|
|
import com.example.myapp.model.Expression;
|
|
|
|
public class MiddleQueSeting extends AbstractQuestionSeting {
|
|
|
|
public Expression applyUnary(Expression child, String op) {
|
|
switch (op) {
|
|
case "²":
|
|
if (child.mainOperator == null) {
|
|
return new Expression(child.expression + "²", child.value * child.value, "²");
|
|
}
|
|
return new Expression("(" + child.expression + ")²", child.value * child.value, "²");
|
|
case "√":
|
|
// 使用适合开方的数(主要是完全平方数)
|
|
String numStr = getNumberForSqrt();
|
|
child = new Expression(numStr, parseNumber(numStr), null);
|
|
|
|
double sqrtValue = Math.sqrt(child.value);
|
|
if (child.mainOperator == null) {
|
|
return new Expression("√" + child.expression, sqrtValue, "√");
|
|
}
|
|
return new Expression("√(" + child.expression + ")", sqrtValue, "√");
|
|
default:
|
|
return child;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public String addParenthesesIfNeeded(Expression child, String parentOp, boolean isRightChild) {
|
|
if (child.mainOperator == null || child.mainOperator.equals("²") || child.mainOperator.equals("√")) {
|
|
return child.expression;
|
|
}
|
|
|
|
int parentPriority = getPriority(parentOp);
|
|
int childPriority = getPriority(child.mainOperator);
|
|
|
|
if (childPriority < parentPriority) {
|
|
return "(" + child.expression + ")";
|
|
}
|
|
|
|
if (isRightChild && (parentOp.equals("-") || parentOp.equals("/"))) {
|
|
if (parentPriority == childPriority) {
|
|
return "(" + child.expression + ")";
|
|
}
|
|
}
|
|
|
|
return child.expression;
|
|
}
|
|
|
|
@Override
|
|
public Expression setQuestion(int count) {
|
|
Expression result = firstSetQuestion(count);
|
|
// 确保初中题目包含平方或开方
|
|
while (!result.expression.contains("²") && !result.expression.contains("√")) {
|
|
result = firstSetQuestion(count);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public Expression firstSetQuestion(int count) {
|
|
if (count == 1) {
|
|
String numStr = getRandomNumber();
|
|
Expression expr = new Expression(numStr, parseNumber(numStr), null);
|
|
return probability(expr);
|
|
}
|
|
int leftCount = 1 + rand.nextInt(count - 1);
|
|
int rightCount = count - leftCount;
|
|
Expression left = firstSetQuestion(leftCount);
|
|
Expression right = firstSetQuestion(rightCount);
|
|
String op = getRandomOperator();
|
|
double value = 0;
|
|
switch (op) {
|
|
case "+":
|
|
value = left.value + right.value;
|
|
break;
|
|
case "-":
|
|
value = left.value - right.value;
|
|
if (value < 0) {
|
|
Expression temp = left;
|
|
left = right;
|
|
right = temp;
|
|
value = left.value - right.value;
|
|
}
|
|
break;
|
|
case "*":
|
|
value = left.value * right.value;
|
|
break;
|
|
case "/":
|
|
if (Math.abs(right.value) < 1e-10) {
|
|
right = firstSetQuestion(rightCount);
|
|
}
|
|
value = left.value / right.value;
|
|
break;
|
|
}
|
|
String leftExpr = addParenthesesIfNeeded(left, op, false);
|
|
String rightExpr = addParenthesesIfNeeded(right, op, true);
|
|
Expression result = new Expression(leftExpr + " " + op + " " + rightExpr, value, op);
|
|
result = probability(result);
|
|
return result;
|
|
}
|
|
|
|
public Expression probability(Expression result) {
|
|
if (rand.nextDouble() < 0.3) {
|
|
String[] unaryOps = {"²", "√"};
|
|
String unaryOp = unaryOps[rand.nextInt(unaryOps.length)];
|
|
result = applyUnary(result, unaryOp);
|
|
result.mainOperator = unaryOp;
|
|
}
|
|
return result;
|
|
}
|
|
} |