|
|
|
|
@ -402,45 +402,6 @@ private double calculateTrigFunction(String function, int angle) {
|
|
|
|
|
default: return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 确保除法运算都能除尽
|
|
|
|
|
*/
|
|
|
|
|
private void ensureDivisibleOperations(List<Integer> operands, List<String> operators) {
|
|
|
|
|
for (int i = 0; i < operators.size(); i++) {
|
|
|
|
|
if (operators.get(i).equals("/")) {
|
|
|
|
|
int dividend = operands.get(i);
|
|
|
|
|
int divisor = operands.get(i + 1);
|
|
|
|
|
|
|
|
|
|
// 如果除不尽,调整被除数使其能被除数整除
|
|
|
|
|
if (dividend % divisor != 0) {
|
|
|
|
|
// 生成一个能被除数整除的新被除数
|
|
|
|
|
int newDividend = divisor * (1 + random.nextInt(MAX_OPERAND_VALUE / divisor));
|
|
|
|
|
operands.set(i, newDividend);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 确保除法运算都能除尽(针对Double类型)
|
|
|
|
|
*/
|
|
|
|
|
private void ensureDivisibleOperationsForDouble(List<Double> operands, List<String> operators) {
|
|
|
|
|
for (int i = 0; i < operators.size(); i++) {
|
|
|
|
|
if (operators.get(i).equals("/")) {
|
|
|
|
|
double dividend = operands.get(i);
|
|
|
|
|
double divisor = operands.get(i + 1);
|
|
|
|
|
|
|
|
|
|
// 只对整数部分进行除法检查
|
|
|
|
|
if (divisor != 0 && dividend % divisor != 0) {
|
|
|
|
|
// 生成一个能被除数整除的新被除数
|
|
|
|
|
int intDivisor = (int) divisor;
|
|
|
|
|
int newDividend = intDivisor * (1 + random.nextInt(MAX_OPERAND_VALUE / intDivisor));
|
|
|
|
|
operands.set(i, (double) newDividend);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -515,9 +476,6 @@ private double calculateTrigFunction(String function, int angle) {
|
|
|
|
|
operators.add(selectedOp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 确保除法运算都能除尽
|
|
|
|
|
ensureDivisibleOperations(operands, operators);
|
|
|
|
|
|
|
|
|
|
// 构建表达式字符串
|
|
|
|
|
expression.append(operands.get(0));
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
@ -582,7 +540,7 @@ private double calculateTrigFunction(String function, int angle) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成包含除法的复合运算表达式(确保除法都能除尽)
|
|
|
|
|
* 生成包含除法的复合运算表达式
|
|
|
|
|
*/
|
|
|
|
|
private String[] generateCompoundExpressionWithDivision(int operatorCount) {
|
|
|
|
|
StringBuilder expression = new StringBuilder();
|
|
|
|
|
@ -600,9 +558,6 @@ private double calculateTrigFunction(String function, int angle) {
|
|
|
|
|
operators.add(availableOps[random.nextInt(availableOps.length)]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 确保除法运算都能除尽
|
|
|
|
|
ensureDivisibleOperations(operands, operators);
|
|
|
|
|
|
|
|
|
|
// 构建表达式字符串
|
|
|
|
|
expression.append(operands.get(0));
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
@ -686,9 +641,6 @@ private double calculateTrigFunction(String function, int angle) {
|
|
|
|
|
operators.add(selectedOp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 确保除法运算都能除尽(只对整数运算进行检查)
|
|
|
|
|
ensureDivisibleOperationsForDouble(operands, operators);
|
|
|
|
|
|
|
|
|
|
// 构建表达式字符串
|
|
|
|
|
expression.append(operands.get(0).intValue());
|
|
|
|
|
for (int i = 0; i < operatorCount; i++) {
|
|
|
|
|
@ -856,9 +808,7 @@ private double calculateTrigFunction(String function, int angle) {
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
} else if (ops.get(i).equals("√")) {
|
|
|
|
|
// 计算开方结果
|
|
|
|
|
int result = (int) Math.sqrt(numbers.get(i));
|
|
|
|
|
numbers.set(i, result);
|
|
|
|
|
// 开方运算已经在生成时处理了
|
|
|
|
|
ops.remove(i);
|
|
|
|
|
i--; // 调整索引
|
|
|
|
|
}
|
|
|
|
|
|