|
|
|
@ -54,12 +54,13 @@ public class ChoiceQuestionGenerator {
|
|
|
|
* 生成小学题目
|
|
|
|
* 生成小学题目
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private Question generatePrimaryQuestion(int questionNumber) {
|
|
|
|
private Question generatePrimaryQuestion(int questionNumber) {
|
|
|
|
int a = MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE);
|
|
|
|
// 随机选择运算符号数量(1-3个)
|
|
|
|
int b = MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE);
|
|
|
|
int operatorCount = 1 + random.nextInt(3);
|
|
|
|
|
|
|
|
|
|
|
|
String operation = getRandomOperation("+*-");
|
|
|
|
// 生成复合运算表达式
|
|
|
|
String questionText = String.format("%d %s %d = ?", a, operation, b);
|
|
|
|
String[] expression = generateCompoundExpression(operatorCount);
|
|
|
|
int correctAnswer = calculate(a, b, operation);
|
|
|
|
String questionText = expression[0];
|
|
|
|
|
|
|
|
int correctAnswer = Integer.parseInt(expression[1]);
|
|
|
|
|
|
|
|
|
|
|
|
// 生成错误选项
|
|
|
|
// 生成错误选项
|
|
|
|
List<Integer> options = generateWrongOptions(correctAnswer);
|
|
|
|
List<Integer> options = generateWrongOptions(correctAnswer);
|
|
|
|
@ -79,11 +80,33 @@ public class ChoiceQuestionGenerator {
|
|
|
|
* 生成初中题目
|
|
|
|
* 生成初中题目
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private Question generateJuniorQuestion(int questionNumber) {
|
|
|
|
private Question generateJuniorQuestion(int questionNumber) {
|
|
|
|
int a = MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE);
|
|
|
|
// 随机选择运算符号数量(1-3个)
|
|
|
|
|
|
|
|
int operatorCount = 1 + random.nextInt(3);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 30%概率生成纯平方/开方题目,70%概率生成包含平方/开方的复合运算题目
|
|
|
|
|
|
|
|
if (random.nextDouble() < 0.3) {
|
|
|
|
|
|
|
|
return generatePureSquareOrRootQuestion(questionNumber);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return generateJuniorCompoundQuestion(operatorCount, questionNumber);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 50%概率生成平方或开方题目
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成纯平方或开方题目
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private Question generatePureSquareOrRootQuestion(int questionNumber) {
|
|
|
|
if (random.nextBoolean()) {
|
|
|
|
if (random.nextBoolean()) {
|
|
|
|
// 平方题目
|
|
|
|
return generateSquareQuestion(questionNumber);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return generateRootQuestion(questionNumber);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成平方题目
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private Question generateSquareQuestion(int questionNumber) {
|
|
|
|
|
|
|
|
int a = MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE);
|
|
|
|
String questionText = String.format("%d² = ?", a);
|
|
|
|
String questionText = String.format("%d² = ?", a);
|
|
|
|
int correctAnswer = a * a;
|
|
|
|
int correctAnswer = a * a;
|
|
|
|
List<Integer> options = generateWrongOptions(correctAnswer);
|
|
|
|
List<Integer> options = generateWrongOptions(correctAnswer);
|
|
|
|
@ -97,8 +120,12 @@ public class ChoiceQuestionGenerator {
|
|
|
|
String.valueOf(options.get(3)),
|
|
|
|
String.valueOf(options.get(3)),
|
|
|
|
String.valueOf(correctAnswer),
|
|
|
|
String.valueOf(correctAnswer),
|
|
|
|
questionNumber);
|
|
|
|
questionNumber);
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
// 开方题目
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成开方题目
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private Question generateRootQuestion(int questionNumber) {
|
|
|
|
int perfectSquare = (int) Math.pow(random.nextInt(10) + 1, 2);
|
|
|
|
int perfectSquare = (int) Math.pow(random.nextInt(10) + 1, 2);
|
|
|
|
String questionText = String.format("√%d = ?", perfectSquare);
|
|
|
|
String questionText = String.format("√%d = ?", perfectSquare);
|
|
|
|
int correctAnswer = (int) Math.sqrt(perfectSquare);
|
|
|
|
int correctAnswer = (int) Math.sqrt(perfectSquare);
|
|
|
|
@ -114,13 +141,49 @@ public class ChoiceQuestionGenerator {
|
|
|
|
String.valueOf(correctAnswer),
|
|
|
|
String.valueOf(correctAnswer),
|
|
|
|
questionNumber);
|
|
|
|
questionNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成初中复合运算题目
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private Question generateJuniorCompoundQuestion(int operatorCount, int questionNumber) {
|
|
|
|
|
|
|
|
// 生成包含平方/开方的复合运算表达式(确保包含平方或开方)
|
|
|
|
|
|
|
|
String[] expression = generateJuniorCompoundExpressionWithGuarantee(operatorCount);
|
|
|
|
|
|
|
|
String questionText = expression[0];
|
|
|
|
|
|
|
|
int correctAnswer = Integer.parseInt(expression[1]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成错误选项
|
|
|
|
|
|
|
|
List<Integer> options = generateWrongOptions(correctAnswer);
|
|
|
|
|
|
|
|
options.add(correctAnswer);
|
|
|
|
|
|
|
|
Collections.shuffle(options);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new Question(questionText,
|
|
|
|
|
|
|
|
String.valueOf(options.get(0)),
|
|
|
|
|
|
|
|
String.valueOf(options.get(1)),
|
|
|
|
|
|
|
|
String.valueOf(options.get(2)),
|
|
|
|
|
|
|
|
String.valueOf(options.get(3)),
|
|
|
|
|
|
|
|
String.valueOf(correctAnswer),
|
|
|
|
|
|
|
|
questionNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 生成高中题目
|
|
|
|
* 生成高中题目
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private Question generateSeniorQuestion(int questionNumber) {
|
|
|
|
private Question generateSeniorQuestion(int questionNumber) {
|
|
|
|
// 使用常见角度: 0°, 30°, 45°, 60°, 90°, 120°, 135°, 150°, 180°, 210°, 225°, 270°, 300°, 315°, 330°
|
|
|
|
// 随机选择运算符号数量(1-3个)
|
|
|
|
|
|
|
|
int operatorCount = 1 + random.nextInt(3);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 30%概率生成纯三角函数题目,70%概率生成包含三角函数的复合运算题目
|
|
|
|
|
|
|
|
if (random.nextDouble() < 0.3) {
|
|
|
|
|
|
|
|
return generatePureTrigQuestion(questionNumber);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return generateSeniorCompoundQuestion(operatorCount, questionNumber);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成纯三角函数题目
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private Question generatePureTrigQuestion(int questionNumber) {
|
|
|
|
int[] commonAngles = {0, 30, 45, 60, 90, 120, 135, 150, 180, 210, 225, 270, 300, 315, 330};
|
|
|
|
int[] commonAngles = {0, 30, 45, 60, 90, 120, 135, 150, 180, 210, 225, 270, 300, 315, 330};
|
|
|
|
int angle = commonAngles[random.nextInt(commonAngles.length)];
|
|
|
|
int angle = commonAngles[random.nextInt(commonAngles.length)];
|
|
|
|
String[] functions = {"sin", "cos", "tan"};
|
|
|
|
String[] functions = {"sin", "cos", "tan"};
|
|
|
|
@ -144,7 +207,30 @@ public class ChoiceQuestionGenerator {
|
|
|
|
options.get(3),
|
|
|
|
options.get(3),
|
|
|
|
correctAnswer,
|
|
|
|
correctAnswer,
|
|
|
|
questionNumber);
|
|
|
|
questionNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成高中复合运算题目
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private Question generateSeniorCompoundQuestion(int operatorCount, int questionNumber) {
|
|
|
|
|
|
|
|
// 生成包含三角函数的复合运算表达式(确保包含三角函数)
|
|
|
|
|
|
|
|
String[] expression = generateSeniorCompoundExpressionWithGuarantee(operatorCount);
|
|
|
|
|
|
|
|
String questionText = expression[0];
|
|
|
|
|
|
|
|
int correctAnswer = Integer.parseInt(expression[1]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成错误选项
|
|
|
|
|
|
|
|
List<Integer> options = generateWrongOptions(correctAnswer);
|
|
|
|
|
|
|
|
options.add(correctAnswer);
|
|
|
|
|
|
|
|
Collections.shuffle(options);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new Question(questionText,
|
|
|
|
|
|
|
|
String.valueOf(options.get(0)),
|
|
|
|
|
|
|
|
String.valueOf(options.get(1)),
|
|
|
|
|
|
|
|
String.valueOf(options.get(2)),
|
|
|
|
|
|
|
|
String.valueOf(options.get(3)),
|
|
|
|
|
|
|
|
String.valueOf(correctAnswer),
|
|
|
|
|
|
|
|
questionNumber);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 生成三角函数题目的选项
|
|
|
|
* 生成三角函数题目的选项
|
|
|
|
@ -248,10 +334,22 @@ private String generateNearbyValue(double correctAnswer, Set<String> usedOptions
|
|
|
|
* 计算三角函数
|
|
|
|
* 计算三角函数
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private double calculateTrigFunction(String function, int angle) {
|
|
|
|
private double calculateTrigFunction(String function, int angle) {
|
|
|
|
double radians = Math.toRadians(angle);
|
|
|
|
|
|
|
|
switch (function) {
|
|
|
|
switch (function) {
|
|
|
|
case "sin":
|
|
|
|
case "sin":
|
|
|
|
// 对特定角度返回精确值
|
|
|
|
return calculateSin(angle);
|
|
|
|
|
|
|
|
case "cos":
|
|
|
|
|
|
|
|
return calculateCos(angle);
|
|
|
|
|
|
|
|
case "tan":
|
|
|
|
|
|
|
|
return calculateTan(angle);
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 计算正弦函数
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private double calculateSin(int angle) {
|
|
|
|
switch (angle) {
|
|
|
|
switch (angle) {
|
|
|
|
case 0: return 0;
|
|
|
|
case 0: return 0;
|
|
|
|
case 30: return 0.5;
|
|
|
|
case 30: return 0.5;
|
|
|
|
@ -268,10 +366,15 @@ private double calculateTrigFunction(String function, int angle) {
|
|
|
|
case 300: return -Math.sqrt(3)/2;
|
|
|
|
case 300: return -Math.sqrt(3)/2;
|
|
|
|
case 315: return -Math.sqrt(2)/2;
|
|
|
|
case 315: return -Math.sqrt(2)/2;
|
|
|
|
case 330: return -0.5;
|
|
|
|
case 330: return -0.5;
|
|
|
|
default: return Math.sin(radians);
|
|
|
|
default:
|
|
|
|
|
|
|
|
return Math.sin(Math.toRadians(angle));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "cos":
|
|
|
|
}
|
|
|
|
// 对特定角度返回精确值
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 计算余弦函数
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private double calculateCos(int angle) {
|
|
|
|
switch (angle) {
|
|
|
|
switch (angle) {
|
|
|
|
case 0: return 1;
|
|
|
|
case 0: return 1;
|
|
|
|
case 30: return Math.sqrt(3)/2;
|
|
|
|
case 30: return Math.sqrt(3)/2;
|
|
|
|
@ -288,10 +391,15 @@ private double calculateTrigFunction(String function, int angle) {
|
|
|
|
case 300: return 0.5;
|
|
|
|
case 300: return 0.5;
|
|
|
|
case 315: return Math.sqrt(2)/2;
|
|
|
|
case 315: return Math.sqrt(2)/2;
|
|
|
|
case 330: return Math.sqrt(3)/2;
|
|
|
|
case 330: return Math.sqrt(3)/2;
|
|
|
|
default: return Math.cos(radians);
|
|
|
|
default:
|
|
|
|
|
|
|
|
return Math.cos(Math.toRadians(angle));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "tan":
|
|
|
|
}
|
|
|
|
// 对特定角度返回精确值或未定义
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 计算正切函数
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private double calculateTan(int angle) {
|
|
|
|
switch (angle) {
|
|
|
|
switch (angle) {
|
|
|
|
case 0: return 0;
|
|
|
|
case 0: return 0;
|
|
|
|
case 30: return Math.sqrt(3)/3;
|
|
|
|
case 30: return Math.sqrt(3)/3;
|
|
|
|
@ -308,9 +416,8 @@ private double calculateTrigFunction(String function, int angle) {
|
|
|
|
case 300: return -Math.sqrt(3);
|
|
|
|
case 300: return -Math.sqrt(3);
|
|
|
|
case 315: return -1;
|
|
|
|
case 315: return -1;
|
|
|
|
case 330: return -Math.sqrt(3)/3;
|
|
|
|
case 330: return -Math.sqrt(3)/3;
|
|
|
|
default: return Math.tan(radians);
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return Math.tan(Math.toRadians(angle));
|
|
|
|
default: return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -356,6 +463,596 @@ private double calculateTrigFunction(String function, int angle) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成复合运算表达式
|
|
|
|
|
|
|
|
* @param operatorCount 运算符数量(1-3个)
|
|
|
|
|
|
|
|
* @return 返回[表达式字符串, 计算结果字符串]
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private String[] generateCompoundExpression(int operatorCount) {
|
|
|
|
|
|
|
|
StringBuilder expression = new StringBuilder();
|
|
|
|
|
|
|
|
List<Integer> operands = new ArrayList<>();
|
|
|
|
|
|
|
|
List<String> operators = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成操作数(运算符数量+1个操作数)
|
|
|
|
|
|
|
|
for (int i = 0; i <= operatorCount; i++) {
|
|
|
|
|
|
|
|
operands.add(MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成运算符
|
|
|
|
|
|
|
|
String[] availableOps = {"+", "-", "*"};
|
|
|
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
|
|
|
operators.add(availableOps[random.nextInt(availableOps.length)]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构建表达式字符串
|
|
|
|
|
|
|
|
expression.append(operands.get(0));
|
|
|
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
|
|
|
expression.append(" ").append(operators.get(i)).append(" ").append(operands.get(i + 1));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
expression.append(" = ?");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 计算表达式结果
|
|
|
|
|
|
|
|
int result = calculateCompoundExpression(operands, operators);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new String[]{expression.toString(), String.valueOf(result)};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成初中复合运算表达式(确保包含平方/开方)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private String[] generateJuniorCompoundExpressionWithGuarantee(int operatorCount) {
|
|
|
|
|
|
|
|
StringBuilder expression = new StringBuilder();
|
|
|
|
|
|
|
|
List<Integer> operands = new ArrayList<>();
|
|
|
|
|
|
|
|
List<String> operators = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成操作数(运算符数量+1个操作数)
|
|
|
|
|
|
|
|
for (int i = 0; i <= operatorCount; i++) {
|
|
|
|
|
|
|
|
operands.add(MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 确保至少有一个平方或开方运算
|
|
|
|
|
|
|
|
boolean hasSquareOrRoot = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成运算符
|
|
|
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
|
|
|
String[] availableOps;
|
|
|
|
|
|
|
|
if (!hasSquareOrRoot && i == operatorCount - 1) {
|
|
|
|
|
|
|
|
// 最后一个运算符必须是平方或开方
|
|
|
|
|
|
|
|
availableOps = new String[]{"²", "√"};
|
|
|
|
|
|
|
|
hasSquareOrRoot = true;
|
|
|
|
|
|
|
|
} else if (!hasSquareOrRoot && random.nextDouble() < 0.4) {
|
|
|
|
|
|
|
|
// 40%概率选择平方或开方
|
|
|
|
|
|
|
|
availableOps = new String[]{"+", "-", "*", "/", "²", "√"};
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 其他情况选择基础运算
|
|
|
|
|
|
|
|
availableOps = new String[]{"+", "-", "*", "/"};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String selectedOp = availableOps[random.nextInt(availableOps.length)];
|
|
|
|
|
|
|
|
if (selectedOp.equals("²") || selectedOp.equals("√")) {
|
|
|
|
|
|
|
|
hasSquareOrRoot = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
operators.add(selectedOp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构建表达式字符串
|
|
|
|
|
|
|
|
expression.append(operands.get(0));
|
|
|
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
|
|
|
if (operators.get(i).equals("²")) {
|
|
|
|
|
|
|
|
expression.append("²");
|
|
|
|
|
|
|
|
} else if (operators.get(i).equals("√")) {
|
|
|
|
|
|
|
|
// 确保开方的是完全平方数
|
|
|
|
|
|
|
|
int perfectSquare = (int) Math.pow(random.nextInt(10) + 1, 2);
|
|
|
|
|
|
|
|
expression.append("√").append(perfectSquare);
|
|
|
|
|
|
|
|
operands.set(i + 1, (int) Math.sqrt(perfectSquare));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
expression.append(" ").append(operators.get(i)).append(" ").append(operands.get(i + 1));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
expression.append(" = ?");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 计算表达式结果
|
|
|
|
|
|
|
|
int result = calculateJuniorCompoundExpression(operands, operators);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new String[]{expression.toString(), String.valueOf(result)};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成初中复合运算表达式(包含平方/开方)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private String[] generateJuniorCompoundExpression(int operatorCount) {
|
|
|
|
|
|
|
|
StringBuilder expression = new StringBuilder();
|
|
|
|
|
|
|
|
List<Integer> operands = new ArrayList<>();
|
|
|
|
|
|
|
|
List<String> operators = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成操作数(运算符数量+1个操作数)
|
|
|
|
|
|
|
|
for (int i = 0; i <= operatorCount; i++) {
|
|
|
|
|
|
|
|
operands.add(MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成运算符(包含平方/开方)
|
|
|
|
|
|
|
|
String[] availableOps = {"+", "-", "*", "/", "²", "√"};
|
|
|
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
|
|
|
operators.add(availableOps[random.nextInt(availableOps.length)]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构建表达式字符串
|
|
|
|
|
|
|
|
expression.append(operands.get(0));
|
|
|
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
|
|
|
if (operators.get(i).equals("²")) {
|
|
|
|
|
|
|
|
expression.append("²");
|
|
|
|
|
|
|
|
} else if (operators.get(i).equals("√")) {
|
|
|
|
|
|
|
|
// 确保开方的是完全平方数
|
|
|
|
|
|
|
|
int perfectSquare = (int) Math.pow(random.nextInt(10) + 1, 2);
|
|
|
|
|
|
|
|
expression.append("√").append(perfectSquare);
|
|
|
|
|
|
|
|
operands.set(i + 1, (int) Math.sqrt(perfectSquare));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
expression.append(" ").append(operators.get(i)).append(" ").append(operands.get(i + 1));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
expression.append(" = ?");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 计算表达式结果
|
|
|
|
|
|
|
|
int result = calculateJuniorCompoundExpression(operands, operators);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new String[]{expression.toString(), String.valueOf(result)};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成包含除法的复合运算表达式
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private String[] generateCompoundExpressionWithDivision(int operatorCount) {
|
|
|
|
|
|
|
|
StringBuilder expression = new StringBuilder();
|
|
|
|
|
|
|
|
List<Integer> operands = new ArrayList<>();
|
|
|
|
|
|
|
|
List<String> operators = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成操作数(运算符数量+1个操作数)
|
|
|
|
|
|
|
|
for (int i = 0; i <= operatorCount; i++) {
|
|
|
|
|
|
|
|
operands.add(MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成运算符(包含除法)
|
|
|
|
|
|
|
|
String[] availableOps = {"+", "-", "*", "/"};
|
|
|
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
|
|
|
operators.add(availableOps[random.nextInt(availableOps.length)]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构建表达式字符串
|
|
|
|
|
|
|
|
expression.append(operands.get(0));
|
|
|
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
|
|
|
expression.append(" ").append(operators.get(i)).append(" ").append(operands.get(i + 1));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
expression.append(" = ?");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 计算表达式结果
|
|
|
|
|
|
|
|
int result = calculateCompoundExpressionWithDivision(operands, operators);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new String[]{expression.toString(), String.valueOf(result)};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 计算复合表达式
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private int calculateCompoundExpression(List<Integer> operands, List<String> operators) {
|
|
|
|
|
|
|
|
// 先处理乘除法,再处理加减法
|
|
|
|
|
|
|
|
List<Integer> numbers = new ArrayList<>(operands);
|
|
|
|
|
|
|
|
List<String> ops = new ArrayList<>(operators);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 第一轮:处理乘除法
|
|
|
|
|
|
|
|
for (int i = 0; i < ops.size(); i++) {
|
|
|
|
|
|
|
|
if (ops.get(i).equals("*")) {
|
|
|
|
|
|
|
|
int result = numbers.get(i) * numbers.get(i + 1);
|
|
|
|
|
|
|
|
numbers.set(i, result);
|
|
|
|
|
|
|
|
numbers.remove(i + 1);
|
|
|
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 第二轮:处理加减法
|
|
|
|
|
|
|
|
int result = numbers.get(0);
|
|
|
|
|
|
|
|
for (int i = 0; i < ops.size(); i++) {
|
|
|
|
|
|
|
|
if (ops.get(i).equals("+")) {
|
|
|
|
|
|
|
|
result += numbers.get(i + 1);
|
|
|
|
|
|
|
|
} else if (ops.get(i).equals("-")) {
|
|
|
|
|
|
|
|
result -= numbers.get(i + 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成高中复合运算表达式(确保包含三角函数)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private String[] generateSeniorCompoundExpressionWithGuarantee(int operatorCount) {
|
|
|
|
|
|
|
|
StringBuilder expression = new StringBuilder();
|
|
|
|
|
|
|
|
List<Double> operands = new ArrayList<>();
|
|
|
|
|
|
|
|
List<String> operators = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成操作数(运算符数量+1个操作数)
|
|
|
|
|
|
|
|
for (int i = 0; i <= operatorCount; i++) {
|
|
|
|
|
|
|
|
operands.add((double)(MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 确保至少有一个三角函数运算
|
|
|
|
|
|
|
|
boolean hasTrigFunction = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成运算符
|
|
|
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
|
|
|
String[] availableOps;
|
|
|
|
|
|
|
|
if (!hasTrigFunction && i == operatorCount - 1) {
|
|
|
|
|
|
|
|
// 最后一个运算符必须是三角函数
|
|
|
|
|
|
|
|
availableOps = new String[]{"sin", "cos", "tan"};
|
|
|
|
|
|
|
|
hasTrigFunction = true;
|
|
|
|
|
|
|
|
} else if (!hasTrigFunction && random.nextDouble() < 0.4) {
|
|
|
|
|
|
|
|
// 40%概率选择三角函数
|
|
|
|
|
|
|
|
availableOps = new String[]{"+", "-", "*", "/", "sin", "cos", "tan"};
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 其他情况选择基础运算
|
|
|
|
|
|
|
|
availableOps = new String[]{"+", "-", "*", "/"};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String selectedOp = availableOps[random.nextInt(availableOps.length)];
|
|
|
|
|
|
|
|
if (selectedOp.equals("sin") || selectedOp.equals("cos") || selectedOp.equals("tan")) {
|
|
|
|
|
|
|
|
hasTrigFunction = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
operators.add(selectedOp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构建表达式字符串
|
|
|
|
|
|
|
|
expression.append(operands.get(0).intValue());
|
|
|
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
|
|
|
if (operators.get(i).equals("sin") || operators.get(i).equals("cos") || operators.get(i).equals("tan")) {
|
|
|
|
|
|
|
|
// 生成常见角度
|
|
|
|
|
|
|
|
int[] commonAngles = {0, 30, 45, 60, 90, 120, 135, 150, 180, 210, 225, 270, 300, 315, 330};
|
|
|
|
|
|
|
|
int angle = commonAngles[random.nextInt(commonAngles.length)];
|
|
|
|
|
|
|
|
expression.append(" ").append(operators.get(i)).append("(").append(angle).append("°)");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 计算三角函数值
|
|
|
|
|
|
|
|
double trigValue = calculateTrigFunction(operators.get(i), angle);
|
|
|
|
|
|
|
|
operands.set(i + 1, trigValue);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
expression.append(" ").append(operators.get(i)).append(" ").append(operands.get(i + 1).intValue());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
expression.append(" = ?");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 计算表达式结果
|
|
|
|
|
|
|
|
int result = calculateSeniorCompoundExpression(operands, operators);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new String[]{expression.toString(), String.valueOf(result)};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成高中复合运算表达式(包含三角函数)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private String[] generateSeniorCompoundExpression(int operatorCount) {
|
|
|
|
|
|
|
|
StringBuilder expression = new StringBuilder();
|
|
|
|
|
|
|
|
List<Double> operands = new ArrayList<>();
|
|
|
|
|
|
|
|
List<String> operators = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成操作数(运算符数量+1个操作数)
|
|
|
|
|
|
|
|
for (int i = 0; i <= operatorCount; i++) {
|
|
|
|
|
|
|
|
operands.add((double)(MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成运算符(包含三角函数)
|
|
|
|
|
|
|
|
String[] availableOps = {"+", "-", "*", "/", "sin", "cos", "tan"};
|
|
|
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
|
|
|
operators.add(availableOps[random.nextInt(availableOps.length)]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构建表达式字符串
|
|
|
|
|
|
|
|
expression.append(operands.get(0).intValue());
|
|
|
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
|
|
|
if (operators.get(i).equals("sin") || operators.get(i).equals("cos") || operators.get(i).equals("tan")) {
|
|
|
|
|
|
|
|
// 生成常见角度
|
|
|
|
|
|
|
|
int[] commonAngles = {0, 30, 45, 60, 90, 120, 135, 150, 180, 210, 225, 270, 300, 315, 330};
|
|
|
|
|
|
|
|
int angle = commonAngles[random.nextInt(commonAngles.length)];
|
|
|
|
|
|
|
|
expression.append(" ").append(operators.get(i)).append("(").append(angle).append("°)");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 计算三角函数值
|
|
|
|
|
|
|
|
double trigValue = calculateTrigFunction(operators.get(i), angle);
|
|
|
|
|
|
|
|
operands.set(i + 1, trigValue);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
expression.append(" ").append(operators.get(i)).append(" ").append(operands.get(i + 1).intValue());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
expression.append(" = ?");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 计算表达式结果
|
|
|
|
|
|
|
|
int result = calculateSeniorCompoundExpression(operands, operators);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new String[]{expression.toString(), String.valueOf(result)};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成高级复合运算表达式(包含平方运算)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private String[] generateAdvancedCompoundExpression(int operatorCount) {
|
|
|
|
|
|
|
|
StringBuilder expression = new StringBuilder();
|
|
|
|
|
|
|
|
List<Integer> operands = new ArrayList<>();
|
|
|
|
|
|
|
|
List<String> operators = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成操作数(运算符数量+1个操作数)
|
|
|
|
|
|
|
|
for (int i = 0; i <= operatorCount; i++) {
|
|
|
|
|
|
|
|
operands.add(MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成运算符(包含平方运算)
|
|
|
|
|
|
|
|
String[] availableOps = {"+", "-", "*", "/", "²"};
|
|
|
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
|
|
|
operators.add(availableOps[random.nextInt(availableOps.length)]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构建表达式字符串
|
|
|
|
|
|
|
|
expression.append(operands.get(0));
|
|
|
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
|
|
|
if (operators.get(i).equals("²")) {
|
|
|
|
|
|
|
|
expression.append("²");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
expression.append(" ").append(operators.get(i)).append(" ").append(operands.get(i + 1));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
expression.append(" = ?");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 计算表达式结果
|
|
|
|
|
|
|
|
int result = calculateAdvancedCompoundExpression(operands, operators);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new String[]{expression.toString(), String.valueOf(result)};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 计算包含除法的复合表达式
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private int calculateCompoundExpressionWithDivision(List<Integer> operands, List<String> operators) {
|
|
|
|
|
|
|
|
// 先处理乘除法,再处理加减法
|
|
|
|
|
|
|
|
List<Integer> numbers = new ArrayList<>(operands);
|
|
|
|
|
|
|
|
List<String> ops = new ArrayList<>(operators);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 第一轮:处理乘除法
|
|
|
|
|
|
|
|
for (int i = 0; i < ops.size(); i++) {
|
|
|
|
|
|
|
|
if (ops.get(i).equals("*")) {
|
|
|
|
|
|
|
|
int result = numbers.get(i) * numbers.get(i + 1);
|
|
|
|
|
|
|
|
numbers.set(i, result);
|
|
|
|
|
|
|
|
numbers.remove(i + 1);
|
|
|
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
|
|
|
} else if (ops.get(i).equals("/")) {
|
|
|
|
|
|
|
|
if (numbers.get(i + 1) != 0) {
|
|
|
|
|
|
|
|
int result = numbers.get(i) / numbers.get(i + 1);
|
|
|
|
|
|
|
|
numbers.set(i, result);
|
|
|
|
|
|
|
|
numbers.remove(i + 1);
|
|
|
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 避免除零错误,重新生成
|
|
|
|
|
|
|
|
numbers.set(i + 1, 1 + random.nextInt(MAX_OPERAND_VALUE));
|
|
|
|
|
|
|
|
int result = numbers.get(i) / numbers.get(i + 1);
|
|
|
|
|
|
|
|
numbers.set(i, result);
|
|
|
|
|
|
|
|
numbers.remove(i + 1);
|
|
|
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 第二轮:处理加减法
|
|
|
|
|
|
|
|
int result = numbers.get(0);
|
|
|
|
|
|
|
|
for (int i = 0; i < ops.size(); i++) {
|
|
|
|
|
|
|
|
if (ops.get(i).equals("+")) {
|
|
|
|
|
|
|
|
result += numbers.get(i + 1);
|
|
|
|
|
|
|
|
} else if (ops.get(i).equals("-")) {
|
|
|
|
|
|
|
|
result -= numbers.get(i + 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 计算初中复合表达式(包含平方/开方)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private int calculateJuniorCompoundExpression(List<Integer> operands, List<String> operators) {
|
|
|
|
|
|
|
|
List<Integer> numbers = new ArrayList<>(operands);
|
|
|
|
|
|
|
|
List<String> ops = new ArrayList<>(operators);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 第一轮:处理平方和开方运算
|
|
|
|
|
|
|
|
processSquareAndRootOperations(numbers, ops);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 第二轮:处理乘除法
|
|
|
|
|
|
|
|
processMultiplicationAndDivision(numbers, ops);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 第三轮:处理加减法
|
|
|
|
|
|
|
|
return processAdditionAndSubtraction(numbers, ops);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 处理平方和开方运算
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void processSquareAndRootOperations(List<Integer> numbers, List<String> ops) {
|
|
|
|
|
|
|
|
for (int i = 0; i < ops.size(); i++) {
|
|
|
|
|
|
|
|
if (ops.get(i).equals("²")) {
|
|
|
|
|
|
|
|
int result = numbers.get(i) * numbers.get(i);
|
|
|
|
|
|
|
|
numbers.set(i, result);
|
|
|
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
|
|
|
} else if (ops.get(i).equals("√")) {
|
|
|
|
|
|
|
|
// 开方运算已经在生成时处理了
|
|
|
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 处理乘除法运算
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void processMultiplicationAndDivision(List<Integer> numbers, List<String> ops) {
|
|
|
|
|
|
|
|
for (int i = 0; i < ops.size(); i++) {
|
|
|
|
|
|
|
|
if (ops.get(i).equals("*")) {
|
|
|
|
|
|
|
|
int result = numbers.get(i) * numbers.get(i + 1);
|
|
|
|
|
|
|
|
numbers.set(i, result);
|
|
|
|
|
|
|
|
numbers.remove(i + 1);
|
|
|
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
|
|
|
} else if (ops.get(i).equals("/")) {
|
|
|
|
|
|
|
|
handleDivision(numbers, ops, i);
|
|
|
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 处理除法运算
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void handleDivision(List<Integer> numbers, List<String> ops, int i) {
|
|
|
|
|
|
|
|
if (numbers.get(i + 1) != 0) {
|
|
|
|
|
|
|
|
int result = numbers.get(i) / numbers.get(i + 1);
|
|
|
|
|
|
|
|
numbers.set(i, result);
|
|
|
|
|
|
|
|
numbers.remove(i + 1);
|
|
|
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 避免除零错误,重新生成
|
|
|
|
|
|
|
|
numbers.set(i + 1, 1 + random.nextInt(MAX_OPERAND_VALUE));
|
|
|
|
|
|
|
|
int result = numbers.get(i) / numbers.get(i + 1);
|
|
|
|
|
|
|
|
numbers.set(i, result);
|
|
|
|
|
|
|
|
numbers.remove(i + 1);
|
|
|
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 处理加减法运算
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private int processAdditionAndSubtraction(List<Integer> numbers, List<String> ops) {
|
|
|
|
|
|
|
|
int result = numbers.get(0);
|
|
|
|
|
|
|
|
for (int i = 0; i < ops.size(); i++) {
|
|
|
|
|
|
|
|
if (ops.get(i).equals("+")) {
|
|
|
|
|
|
|
|
result += numbers.get(i + 1);
|
|
|
|
|
|
|
|
} else if (ops.get(i).equals("-")) {
|
|
|
|
|
|
|
|
result -= numbers.get(i + 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 计算高中复合表达式(包含三角函数)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private int calculateSeniorCompoundExpression(List<Double> operands, List<String> operators) {
|
|
|
|
|
|
|
|
List<Double> numbers = new ArrayList<>(operands);
|
|
|
|
|
|
|
|
List<String> ops = new ArrayList<>(operators);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 第一轮:处理三角函数运算
|
|
|
|
|
|
|
|
processTrigonometricOperations(numbers, ops);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 第二轮:处理乘除法
|
|
|
|
|
|
|
|
processDoubleMultiplicationAndDivision(numbers, ops);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 第三轮:处理加减法
|
|
|
|
|
|
|
|
double result = processDoubleAdditionAndSubtraction(numbers, ops);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (int) Math.round(result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 处理三角函数运算
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void processTrigonometricOperations(List<Double> numbers, List<String> ops) {
|
|
|
|
|
|
|
|
for (int i = 0; i < ops.size(); i++) {
|
|
|
|
|
|
|
|
if (ops.get(i).equals("sin") || ops.get(i).equals("cos") || ops.get(i).equals("tan")) {
|
|
|
|
|
|
|
|
// 三角函数运算已经在生成时处理了
|
|
|
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 处理双精度乘除法运算
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void processDoubleMultiplicationAndDivision(List<Double> numbers, List<String> ops) {
|
|
|
|
|
|
|
|
for (int i = 0; i < ops.size(); i++) {
|
|
|
|
|
|
|
|
if (ops.get(i).equals("*")) {
|
|
|
|
|
|
|
|
double result = numbers.get(i) * numbers.get(i + 1);
|
|
|
|
|
|
|
|
numbers.set(i, result);
|
|
|
|
|
|
|
|
numbers.remove(i + 1);
|
|
|
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
|
|
|
} else if (ops.get(i).equals("/")) {
|
|
|
|
|
|
|
|
handleDoubleDivision(numbers, ops, i);
|
|
|
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 处理双精度除法运算
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void handleDoubleDivision(List<Double> numbers, List<String> ops, int i) {
|
|
|
|
|
|
|
|
if (numbers.get(i + 1) != 0) {
|
|
|
|
|
|
|
|
double result = numbers.get(i) / numbers.get(i + 1);
|
|
|
|
|
|
|
|
numbers.set(i, result);
|
|
|
|
|
|
|
|
numbers.remove(i + 1);
|
|
|
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 避免除零错误,重新生成
|
|
|
|
|
|
|
|
numbers.set(i + 1, 1.0 + random.nextInt(MAX_OPERAND_VALUE));
|
|
|
|
|
|
|
|
double result = numbers.get(i) / numbers.get(i + 1);
|
|
|
|
|
|
|
|
numbers.set(i, result);
|
|
|
|
|
|
|
|
numbers.remove(i + 1);
|
|
|
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 处理双精度加减法运算
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private double processDoubleAdditionAndSubtraction(List<Double> numbers, List<String> ops) {
|
|
|
|
|
|
|
|
double result = numbers.get(0);
|
|
|
|
|
|
|
|
for (int i = 0; i < ops.size(); i++) {
|
|
|
|
|
|
|
|
if (ops.get(i).equals("+")) {
|
|
|
|
|
|
|
|
result += numbers.get(i + 1);
|
|
|
|
|
|
|
|
} else if (ops.get(i).equals("-")) {
|
|
|
|
|
|
|
|
result -= numbers.get(i + 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 计算高级复合表达式(包含平方运算)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private int calculateAdvancedCompoundExpression(List<Integer> operands, List<String> operators) {
|
|
|
|
|
|
|
|
List<Integer> numbers = new ArrayList<>(operands);
|
|
|
|
|
|
|
|
List<String> ops = new ArrayList<>(operators);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 第一轮:处理平方运算
|
|
|
|
|
|
|
|
processSquareOperations(numbers, ops);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 第二轮:处理乘除法
|
|
|
|
|
|
|
|
processMultiplicationAndDivision(numbers, ops);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 第三轮:处理加减法
|
|
|
|
|
|
|
|
return processAdditionAndSubtraction(numbers, ops);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 处理平方运算
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void processSquareOperations(List<Integer> numbers, List<String> ops) {
|
|
|
|
|
|
|
|
for (int i = 0; i < ops.size(); i++) {
|
|
|
|
|
|
|
|
if (ops.get(i).equals("²")) {
|
|
|
|
|
|
|
|
int result = numbers.get(i) * numbers.get(i);
|
|
|
|
|
|
|
|
numbers.set(i, result);
|
|
|
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取随机运算符
|
|
|
|
* 获取随机运算符
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|