|
|
|
|
@ -54,12 +54,13 @@ public class ChoiceQuestionGenerator {
|
|
|
|
|
* 生成小学题目
|
|
|
|
|
*/
|
|
|
|
|
private Question generatePrimaryQuestion(int questionNumber) {
|
|
|
|
|
int a = MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE);
|
|
|
|
|
int b = MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE);
|
|
|
|
|
// 随机选择运算符号数量(1-3个)
|
|
|
|
|
int operatorCount = 1 + random.nextInt(3);
|
|
|
|
|
|
|
|
|
|
String operation = getRandomOperation("+*-");
|
|
|
|
|
String questionText = String.format("%d %s %d = ?", a, operation, b);
|
|
|
|
|
int correctAnswer = calculate(a, b, operation);
|
|
|
|
|
// 生成复合运算表达式
|
|
|
|
|
String[] expression = generateCompoundExpression(operatorCount);
|
|
|
|
|
String questionText = expression[0];
|
|
|
|
|
int correctAnswer = Integer.parseInt(expression[1]);
|
|
|
|
|
|
|
|
|
|
// 生成错误选项
|
|
|
|
|
List<Integer> options = generateWrongOptions(correctAnswer);
|
|
|
|
|
@ -79,13 +80,52 @@ public class ChoiceQuestionGenerator {
|
|
|
|
|
* 生成初中题目
|
|
|
|
|
*/
|
|
|
|
|
private Question generateJuniorQuestion(int questionNumber) {
|
|
|
|
|
int a = MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE);
|
|
|
|
|
// 随机选择运算符号数量(1-3个)
|
|
|
|
|
int operatorCount = 1 + random.nextInt(3);
|
|
|
|
|
|
|
|
|
|
// 50%概率生成平方或开方题目
|
|
|
|
|
if (random.nextBoolean()) {
|
|
|
|
|
// 平方题目
|
|
|
|
|
String questionText = String.format("%d² = ?", a);
|
|
|
|
|
int correctAnswer = a * a;
|
|
|
|
|
// 30%概率生成纯平方/开方题目,70%概率生成包含平方/开方的复合运算题目
|
|
|
|
|
if (random.nextDouble() < 0.3) {
|
|
|
|
|
// 生成纯平方或开方题目
|
|
|
|
|
if (random.nextBoolean()) {
|
|
|
|
|
// 平方题目
|
|
|
|
|
int a = MIN_OPERAND_VALUE + random.nextInt(MAX_OPERAND_VALUE);
|
|
|
|
|
String questionText = String.format("%d² = ?", a);
|
|
|
|
|
int correctAnswer = a * a;
|
|
|
|
|
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);
|
|
|
|
|
} else {
|
|
|
|
|
// 开方题目
|
|
|
|
|
int perfectSquare = (int) Math.pow(random.nextInt(10) + 1, 2);
|
|
|
|
|
String questionText = String.format("√%d = ?", perfectSquare);
|
|
|
|
|
int correctAnswer = (int) Math.sqrt(perfectSquare);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 生成包含平方/开方的复合运算表达式(确保包含平方或开方)
|
|
|
|
|
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);
|
|
|
|
|
@ -97,11 +137,49 @@ public class ChoiceQuestionGenerator {
|
|
|
|
|
String.valueOf(options.get(3)),
|
|
|
|
|
String.valueOf(correctAnswer),
|
|
|
|
|
questionNumber);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成高中题目
|
|
|
|
|
*/
|
|
|
|
|
private Question generateSeniorQuestion(int questionNumber) {
|
|
|
|
|
// 随机选择运算符号数量(1-3个)
|
|
|
|
|
int operatorCount = 1 + random.nextInt(3);
|
|
|
|
|
|
|
|
|
|
// 30%概率生成纯三角函数题目,70%概率生成包含三角函数的复合运算题目
|
|
|
|
|
if (random.nextDouble() < 0.3) {
|
|
|
|
|
// 生成纯三角函数题目
|
|
|
|
|
int[] commonAngles = {0, 30, 45, 60, 90, 120, 135, 150, 180, 210, 225, 270, 300, 315, 330};
|
|
|
|
|
int angle = commonAngles[random.nextInt(commonAngles.length)];
|
|
|
|
|
String[] functions = {"sin", "cos", "tan"};
|
|
|
|
|
String function = functions[random.nextInt(functions.length)];
|
|
|
|
|
|
|
|
|
|
String questionText = String.format("%s(%d°) = ?", function, angle);
|
|
|
|
|
double result = calculateTrigFunction(function, angle);
|
|
|
|
|
|
|
|
|
|
// 生成选项
|
|
|
|
|
List<String> options = generateTrigOptions(result);
|
|
|
|
|
// 使用格式化后的值作为正确答案,确保与选项一致
|
|
|
|
|
String correctAnswer = formatTrigValue(result);
|
|
|
|
|
|
|
|
|
|
// 随机打乱选项
|
|
|
|
|
Collections.shuffle(options);
|
|
|
|
|
|
|
|
|
|
return new Question(questionText,
|
|
|
|
|
options.get(0),
|
|
|
|
|
options.get(1),
|
|
|
|
|
options.get(2),
|
|
|
|
|
options.get(3),
|
|
|
|
|
correctAnswer,
|
|
|
|
|
questionNumber);
|
|
|
|
|
} else {
|
|
|
|
|
// 开方题目
|
|
|
|
|
int perfectSquare = (int) Math.pow(random.nextInt(10) + 1, 2);
|
|
|
|
|
String questionText = String.format("√%d = ?", perfectSquare);
|
|
|
|
|
int correctAnswer = (int) Math.sqrt(perfectSquare);
|
|
|
|
|
// 生成包含三角函数的复合运算表达式(确保包含三角函数)
|
|
|
|
|
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);
|
|
|
|
|
@ -114,33 +192,177 @@ public class ChoiceQuestionGenerator {
|
|
|
|
|
String.valueOf(correctAnswer),
|
|
|
|
|
questionNumber);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成三角函数题目的选项
|
|
|
|
|
*/
|
|
|
|
|
private List<String> generateTrigOptions(double correctAnswer) {
|
|
|
|
|
List<String> options = new ArrayList<>();
|
|
|
|
|
Set<String> usedOptions = new HashSet<>();
|
|
|
|
|
|
|
|
|
|
// 格式化正确答案,保留适当的小数位数
|
|
|
|
|
String formattedCorrectAnswer = formatTrigValue(correctAnswer);
|
|
|
|
|
options.add(formattedCorrectAnswer);
|
|
|
|
|
usedOptions.add(formattedCorrectAnswer);
|
|
|
|
|
|
|
|
|
|
// 添加常见三角函数值作为错误选项
|
|
|
|
|
String[] commonValues = {"0", "0.5", "0.71", "0.87", "1", "1.73", "-0.5", "-0.71", "-0.87", "-1", "-1.73", "undefined"};
|
|
|
|
|
List<String> commonValuesList = new ArrayList<>(Arrays.asList(commonValues));
|
|
|
|
|
|
|
|
|
|
// 打乱常见值列表
|
|
|
|
|
Collections.shuffle(commonValuesList);
|
|
|
|
|
|
|
|
|
|
// 添加错误选项
|
|
|
|
|
int wrongOptionsNeeded = 3;
|
|
|
|
|
for (String value : commonValuesList) {
|
|
|
|
|
if (wrongOptionsNeeded <= 0) break;
|
|
|
|
|
// 确保不添加与正确答案相同的选项
|
|
|
|
|
if (!usedOptions.contains(value) && !value.equals(formattedCorrectAnswer)) {
|
|
|
|
|
options.add(value);
|
|
|
|
|
usedOptions.add(value);
|
|
|
|
|
wrongOptionsNeeded--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成高中题目
|
|
|
|
|
*/
|
|
|
|
|
private Question generateSeniorQuestion(int questionNumber) {
|
|
|
|
|
int angle = random.nextInt(360);
|
|
|
|
|
String[] functions = {"sin", "cos", "tan"};
|
|
|
|
|
String function = functions[random.nextInt(functions.length)];
|
|
|
|
|
// 如果还需要更多选项,则生成接近正确答案的选项
|
|
|
|
|
while (options.size() < 4) {
|
|
|
|
|
String wrongOption = generateNearbyValue(correctAnswer, usedOptions);
|
|
|
|
|
// 确保新生成的选项不重复且不与正确答案相同
|
|
|
|
|
if (!usedOptions.contains(wrongOption) && !wrongOption.equals(formattedCorrectAnswer)) {
|
|
|
|
|
options.add(wrongOption);
|
|
|
|
|
usedOptions.add(wrongOption);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String questionText = String.format("%s(%d°) = ?", function, angle);
|
|
|
|
|
double result = calculateTrigFunction(function, angle);
|
|
|
|
|
int correctAnswer = (int) Math.round(result * 100) / 100; // 保留两位小数
|
|
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<Integer> options = generateWrongOptions(correctAnswer);
|
|
|
|
|
options.add(correctAnswer);
|
|
|
|
|
Collections.shuffle(options);
|
|
|
|
|
/**
|
|
|
|
|
* 格式化三角函数值
|
|
|
|
|
*/
|
|
|
|
|
private String formatTrigValue(double value) {
|
|
|
|
|
// 对于特殊值进行精确表示
|
|
|
|
|
if (Math.abs(value) < 1e-10) return "0"; // 接近0的值
|
|
|
|
|
if (Math.abs(value - 1.0) < 1e-10) return "1";
|
|
|
|
|
if (Math.abs(value + 1.0) < 1e-10) return "-1";
|
|
|
|
|
if (Math.abs(value - 0.5) < 1e-10) return "0.5";
|
|
|
|
|
if (Math.abs(value + 0.5) < 1e-10) return "-0.5";
|
|
|
|
|
if (Math.abs(value - Math.sqrt(2)/2) < 1e-10) return "0.71"; // √2/2 ≈ 0.707
|
|
|
|
|
if (Math.abs(value + Math.sqrt(2)/2) < 1e-10) return "-0.71";
|
|
|
|
|
if (Math.abs(value - Math.sqrt(3)/2) < 1e-10) return "0.87"; // √3/2 ≈ 0.866
|
|
|
|
|
if (Math.abs(value + Math.sqrt(3)/2) < 1e-10) return "-0.87";
|
|
|
|
|
if (Math.abs(value - Math.sqrt(3)) < 1e-10) return "1.73"; // √3 ≈ 1.732
|
|
|
|
|
if (Math.abs(value + Math.sqrt(3)) < 1e-10) return "-1.73";
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
// 对于tan(90°)等未定义值
|
|
|
|
|
if (Double.isInfinite(value) || Double.isNaN(value)) {
|
|
|
|
|
return "undefined";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 其他值保留两位小数
|
|
|
|
|
return String.format("%.2f", value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成接近正确答案的选项
|
|
|
|
|
*/
|
|
|
|
|
private String generateNearbyValue(double correctAnswer, Set<String> usedOptions) {
|
|
|
|
|
// 对于特殊值直接返回简单值
|
|
|
|
|
if (Double.isInfinite(correctAnswer) || Double.isNaN(correctAnswer)) {
|
|
|
|
|
return "0";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 生成在正确答案附近的值
|
|
|
|
|
double variation = (random.nextDouble() - 0.5) * 2; // -1 到 1 之间的随机数
|
|
|
|
|
double wrongAnswer = correctAnswer + variation;
|
|
|
|
|
|
|
|
|
|
// 格式化生成的值
|
|
|
|
|
String formatted = formatTrigValue(wrongAnswer);
|
|
|
|
|
|
|
|
|
|
// 确保不会生成与已使用选项相同的值,最多尝试10次
|
|
|
|
|
int attempts = 0;
|
|
|
|
|
while (usedOptions.contains(formatted) && attempts < 10) {
|
|
|
|
|
variation = (random.nextDouble() - 0.5) * 2;
|
|
|
|
|
wrongAnswer = correctAnswer + variation;
|
|
|
|
|
formatted = formatTrigValue(wrongAnswer);
|
|
|
|
|
attempts++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return formatted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 计算三角函数
|
|
|
|
|
*/
|
|
|
|
|
private double calculateTrigFunction(String function, int angle) {
|
|
|
|
|
double radians = Math.toRadians(angle);
|
|
|
|
|
switch (function) {
|
|
|
|
|
case "sin":
|
|
|
|
|
// 对特定角度返回精确值
|
|
|
|
|
switch (angle) {
|
|
|
|
|
case 0: return 0;
|
|
|
|
|
case 30: return 0.5;
|
|
|
|
|
case 45: return Math.sqrt(2)/2;
|
|
|
|
|
case 60: return Math.sqrt(3)/2;
|
|
|
|
|
case 90: return 1;
|
|
|
|
|
case 120: return Math.sqrt(3)/2;
|
|
|
|
|
case 135: return Math.sqrt(2)/2;
|
|
|
|
|
case 150: return 0.5;
|
|
|
|
|
case 180: return 0;
|
|
|
|
|
case 210: return -0.5;
|
|
|
|
|
case 225: return -Math.sqrt(2)/2;
|
|
|
|
|
case 270: return -1;
|
|
|
|
|
case 300: return -Math.sqrt(3)/2;
|
|
|
|
|
case 315: return -Math.sqrt(2)/2;
|
|
|
|
|
case 330: return -0.5;
|
|
|
|
|
default: return Math.sin(radians);
|
|
|
|
|
}
|
|
|
|
|
case "cos":
|
|
|
|
|
// 对特定角度返回精确值
|
|
|
|
|
switch (angle) {
|
|
|
|
|
case 0: return 1;
|
|
|
|
|
case 30: return Math.sqrt(3)/2;
|
|
|
|
|
case 45: return Math.sqrt(2)/2;
|
|
|
|
|
case 60: return 0.5;
|
|
|
|
|
case 90: return 0;
|
|
|
|
|
case 120: return -0.5;
|
|
|
|
|
case 135: return -Math.sqrt(2)/2;
|
|
|
|
|
case 150: return -Math.sqrt(3)/2;
|
|
|
|
|
case 180: return -1;
|
|
|
|
|
case 210: return -Math.sqrt(3)/2;
|
|
|
|
|
case 225: return -Math.sqrt(2)/2;
|
|
|
|
|
case 270: return 0;
|
|
|
|
|
case 300: return 0.5;
|
|
|
|
|
case 315: return Math.sqrt(2)/2;
|
|
|
|
|
case 330: return Math.sqrt(3)/2;
|
|
|
|
|
default: return Math.cos(radians);
|
|
|
|
|
}
|
|
|
|
|
case "tan":
|
|
|
|
|
// 对特定角度返回精确值或未定义
|
|
|
|
|
switch (angle) {
|
|
|
|
|
case 0: return 0;
|
|
|
|
|
case 30: return Math.sqrt(3)/3;
|
|
|
|
|
case 45: return 1;
|
|
|
|
|
case 60: return Math.sqrt(3);
|
|
|
|
|
case 90: return Double.POSITIVE_INFINITY; // 未定义
|
|
|
|
|
case 120: return -Math.sqrt(3);
|
|
|
|
|
case 135: return -1;
|
|
|
|
|
case 150: return -Math.sqrt(3)/3;
|
|
|
|
|
case 180: return 0;
|
|
|
|
|
case 210: return Math.sqrt(3)/3;
|
|
|
|
|
case 225: return 1;
|
|
|
|
|
case 270: return Double.POSITIVE_INFINITY; // 未定义
|
|
|
|
|
case 300: return -Math.sqrt(3);
|
|
|
|
|
case 315: return -1;
|
|
|
|
|
case 330: return -Math.sqrt(3)/3;
|
|
|
|
|
default: return Math.tan(radians);
|
|
|
|
|
}
|
|
|
|
|
default: return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成错误选项
|
|
|
|
|
*/
|
|
|
|
|
@ -181,17 +403,570 @@ public class ChoiceQuestionGenerator {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 计算三角函数
|
|
|
|
|
* 生成复合运算表达式
|
|
|
|
|
* @param operatorCount 运算符数量(1-3个)
|
|
|
|
|
* @return 返回[表达式字符串, 计算结果字符串]
|
|
|
|
|
*/
|
|
|
|
|
private double calculateTrigFunction(String function, int angle) {
|
|
|
|
|
double radians = Math.toRadians(angle);
|
|
|
|
|
switch (function) {
|
|
|
|
|
case "sin": return Math.sin(radians);
|
|
|
|
|
case "cos": return Math.cos(radians);
|
|
|
|
|
case "tan": return Math.tan(radians);
|
|
|
|
|
default: return 0;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// 第一轮:处理平方和开方运算
|
|
|
|
|
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--; // 调整索引
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 第二轮:处理乘除法
|
|
|
|
|
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 calculateSeniorCompoundExpression(List<Double> operands, List<String> operators) {
|
|
|
|
|
// 先处理三角函数运算
|
|
|
|
|
List<Double> numbers = new ArrayList<>(operands);
|
|
|
|
|
List<String> ops = new ArrayList<>(operators);
|
|
|
|
|
|
|
|
|
|
// 第一轮:处理三角函数运算
|
|
|
|
|
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--; // 调整索引
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 第二轮:处理乘除法
|
|
|
|
|
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("/")) {
|
|
|
|
|
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);
|
|
|
|
|
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);
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 第三轮:处理加减法
|
|
|
|
|
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 (int) Math.round(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 计算高级复合表达式(包含平方运算)
|
|
|
|
|
*/
|
|
|
|
|
private int calculateAdvancedCompoundExpression(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);
|
|
|
|
|
numbers.set(i, result);
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 第二轮:处理乘除法
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|