Update HighQuestionSetting.java

develop
hnu202304060319 7 months ago
parent 2feb221e7a
commit c6dd5c035e

@ -1,126 +1,123 @@
// HighQuestionSetting.java
public class HighQuestionSetting extends AbstractQuestionSetting {
@Override
public String addParenthesesIfNeeded(Expression child, String parentOperator, boolean isRightChild) {
if (child.getMainOperator() == null
|| child.getMainOperator().equals("²") || child.getMainOperator().equals("√")
|| child.getMainOperator().equals("sin") || child.getMainOperator().equals("cos")
|| child.getMainOperator().equals("tan")) {
return child.getExpression();
}
int parentPriority = getPriority(parentOperator);
int childPriority = getPriority(child.getMainOperator());
if (childPriority < parentPriority) {
return "(" + child.getExpression() + ")";
}
if (isRightChild && (parentOperator.equals("-") || parentOperator.equals("/"))) {
if (parentPriority == childPriority) {
return "(" + child.getExpression() + ")";
}
}
return child.getExpression();
}
public Expression applyUnaryOperator(Expression child, String operator) {
switch (operator) {
case "²":
if (child.getMainOperator() == null) {
return new Expression(child.getExpression() + "²", child.getValue() * child.getValue(), "²");
}
return new Expression("(" + child.getExpression() + ")²", child.getValue() * child.getValue(), "²");
case "√":
if (child.getValue() < 0) {
String numberString = getRandomNumber();
child = new Expression(numberString, Integer.parseInt(numberString), null);
}
if (child.getMainOperator() == null) {
return new Expression("√" + child.getExpression(), (int) Math.sqrt(child.getValue()), "√");
}
return new Expression("√(" + child.getExpression() + ")", (int) Math.sqrt(child.getValue()), "√");
case "sin":
return new Expression("sin(" + child.getExpression() + ")",
(int) Math.round(Math.sin(Math.toRadians(child.getValue()))), "sin");
case "cos":
return new Expression("cos(" + child.getExpression() + ")",
(int) Math.round(Math.cos(Math.toRadians(child.getValue()))), "cos");
case "tan":
while (child.getValue() % 180 == 90) {
String numberString = getRandomNumber();
child = new Expression(numberString, Integer.parseInt(numberString), null);
}
return new Expression("tan(" + child.getExpression() + ")",
(int) Math.round(Math.tan(Math.toRadians(child.getValue()))), "tan");
default:
return child;
}
}
@Override
public Expression setQuestion(int count) {
Expression result = generateFirstQuestion(count);
while (!result.getExpression().contains("sin") && !result.getExpression().contains("cos")
&& !result.getExpression().contains("tan")) {
result = generateFirstQuestion(count);
}
return result;
}
public Expression applyProbability(Expression result) {
if (RANDOM.nextDouble() < 0.3) {
String[] unaryOperators = {"²", "√", "sin", "cos", "tan"};
String unaryOperator = unaryOperators[RANDOM.nextInt(unaryOperators.length)];
result = applyUnaryOperator(result, unaryOperator);
result.setMainOperator(unaryOperator);
}
return result;
}
public Expression generateFirstQuestion(int count) {
if (count == 1) {
String numberString = getRandomNumber();
int number = Integer.parseInt(numberString);
Expression expression = new Expression(numberString, number, null);
expression = applyProbability(expression);
return expression;
}
int leftCount = 1 + RANDOM.nextInt(count - 1);
int rightCount = count - leftCount;
Expression left = generateFirstQuestion(leftCount);
Expression right = generateFirstQuestion(rightCount);
String operator = getRandomOperator();
int value = 0;
switch (operator) {
case "+":
value = left.getValue() + right.getValue();
break;
case "-":
value = left.getValue() - right.getValue();
break;
case "*":
value = left.getValue() * right.getValue();
break;
case "/":
if (right.getValue() == 0) {
return generateFirstQuestion(count);
}
value = left.getValue() / right.getValue();
break;
}
String leftExpression = addParenthesesIfNeeded(left, operator, false);
String rightExpression = addParenthesesIfNeeded(right, operator, true);
Expression result = new Expression(leftExpression + " " + operator + " " + rightExpression, (int) value, operator);
result = applyProbability(result);
return result;
}
// HighQuestionSetting.java
public class HighQuestionSetting extends AbstractQuestionSetting {
@Override
public String addParenthesesIfNeeded(Expression child, String parentOperator, boolean isRightChild) {
if (child.getMainOperator() == null
|| child.getMainOperator().equals("²") || child.getMainOperator().equals("√")
|| child.getMainOperator().equals("sin") || child.getMainOperator().equals("cos")
|| child.getMainOperator().equals("tan")) {
return child.getExpression();
}
int parentPriority = getPriority(parentOperator);
int childPriority = getPriority(child.getMainOperator());
if (childPriority < parentPriority) {
return "(" + child.getExpression() + ")";
}
if (isRightChild && (parentOperator.equals("-") || parentOperator.equals("/"))) {
if (parentPriority == childPriority) {
return "(" + child.getExpression() + ")";
}
}
return child.getExpression();
}
public Expression applyUnaryOperator(Expression child, String operator) {
switch (operator) {
case "²":
if (child.getMainOperator() == null) {
return new Expression(child.getExpression() + "²", child.getValue() * child.getValue(), "²");
}
return new Expression("(" + child.getExpression() + ")²", child.getValue() * child.getValue(), "²");
case "√":
if (child.getValue() < 0) {
String numberString = getRandomNumber();
child = new Expression(numberString, Integer.parseInt(numberString), null);
}
if (child.getMainOperator() == null) {
return new Expression("√" + child.getExpression(), (int) Math.sqrt(child.getValue()), "√");
}
return new Expression("√(" + child.getExpression() + ")", (int) Math.sqrt(child.getValue()), "√");
case "sin":
return new Expression("sin(" + child.getExpression() + ")",
(int) Math.round(Math.sin(Math.toRadians(child.getValue()))), "sin");
case "cos":
return new Expression("cos(" + child.getExpression() + ")",
(int) Math.round(Math.cos(Math.toRadians(child.getValue()))), "cos");
case "tan":
while (child.getValue() % 180 == 90) {
String numberString = getRandomNumber();
child = new Expression(numberString, Integer.parseInt(numberString), null);
}
return new Expression("tan(" + child.getExpression() + ")",
(int) Math.round(Math.tan(Math.toRadians(child.getValue()))), "tan");
default:
return child;
}
}
@Override
public Expression setQuestion(int count) {
Expression result = generateFirstQuestion(count);
while (!result.getExpression().contains("sin") && !result.getExpression().contains("cos")
&& !result.getExpression().contains("tan")) {
result = generateFirstQuestion(count);
}
return result;
}
public Expression applyProbability(Expression result) {
if (RANDOM.nextDouble() < 0.3) {
String[] unaryOperators = {"²", "√", "sin", "cos", "tan"};
String unaryOperator = unaryOperators[RANDOM.nextInt(unaryOperators.length)];
result = applyUnaryOperator(result, unaryOperator);
result.setMainOperator(unaryOperator);
}
return result;
}
public Expression generateFirstQuestion(int count) {
if (count == 1) {
String numberString = getRandomNumber();
int number = Integer.parseInt(numberString);
Expression expression = new Expression(numberString, number, null);
expression = applyProbability(expression);
return expression;
}
int leftCount = 1 + RANDOM.nextInt(count - 1);
int rightCount = count - leftCount;
Expression left = generateFirstQuestion(leftCount);
Expression right = generateFirstQuestion(rightCount);
String operator = getRandomOperator();
int value = 0;
switch (operator) {
case "+":
value = left.getValue() + right.getValue();
break;
case "-":
value = left.getValue() - right.getValue();
break;
case "*":
value = left.getValue() * right.getValue();
break;
case "/":
if (right.getValue() == 0) {
return generateFirstQuestion(count);
}
value = left.getValue() / right.getValue();
break;
}
String leftExpression = addParenthesesIfNeeded(left, operator, false);
String rightExpression = addParenthesesIfNeeded(right, operator, true);
Expression result = new Expression(leftExpression + " " + operator + " " + rightExpression, (int) value, operator);
result = applyProbability(result);
return result;
}
}
Loading…
Cancel
Save