Update PowerManager.java

pull/4/head
hnu202326010305 6 days ago
parent 3faec770ef
commit 4d57401f20

@ -1,87 +1,140 @@
import java.util.List; import java.util.List;
import java.util.ArrayList;
import java.util.Random; import java.util.Random;
/**
*
*/
public class PowerManager { public class PowerManager {
private static final Random RANDOM = new Random(); private static final Random RANDOM = new Random();
// 普通幂运算 //添加一次幂运算,30%概率再加一次
public String addPowerOperations(String expression, List<Integer> numPositions) { public String addPowerOperations(String expression, List<Integer> numPositions,
if (numPositions.isEmpty()) return expression; List<Integer> trigPositions) {
if (numPositions.isEmpty()) {
return expression;
}
String[] tokens = expression.split(" "); String[] tokens = expression.split(" ");
int targetIndex = chooseTargetIndex(tokens, numPositions, null);
if (targetIndex != -1) { addOnePower(tokens, numPositions, trigPositions);
tokens[targetIndex] = applyPower(tokens[targetIndex], tokens, targetIndex); if (RANDOM.nextDouble() < 0.3) {
addOnePower(tokens, numPositions, trigPositions);
} }
return String.join(" ", tokens); return String.join(" ", tokens);
} }
// 避免加在 trig 已占用的位置,同时避免加在运算符右边 // 添加一次幂运算,平方/根号各50%
public String addPowerOperationsAvoid(String expression, List<Integer> numPositions, List<Integer> trigPositions) { private void addOnePower(String[] tokens, List<Integer> numPositions, List<Integer> trigPositions) {
if (numPositions.isEmpty()) return expression; if (RANDOM.nextBoolean()) {
tryAddSquare(tokens, numPositions, trigPositions);
String[] tokens = expression.split(" "); } else {
int targetIndex = chooseTargetIndex(tokens, numPositions, trigPositions); tryAddSqrt(tokens, numPositions, trigPositions);
}
}
if (targetIndex != -1) { // 添加平方
tokens[targetIndex] = applyPower(tokens[targetIndex], tokens, targetIndex); private void tryAddSquare(String[] tokens, List<Integer> numPositions,
List<Integer> trigPositions) {
int target = chooseTargetIndex(tokens, numPositions, trigPositions, true);
if (target != -1) {
tokens[target] = tokens[target] + "^2";
} }
}
return String.join(" ", tokens); // 添加根号
private void tryAddSqrt(String[] tokens, List<Integer> numPositions,
List<Integer> trigPositions) {
int target = chooseTargetIndex(tokens, numPositions, trigPositions, false);
if (target != -1) {
tokens[target] = "√" + tokens[target];
}
} }
// 选择合适的位置加幂运算 // 选择可插入位置
private int chooseTargetIndex(String[] tokens, List<Integer> numPositions, List<Integer> avoidPositions) { private int chooseTargetIndex(String[] tokens, List<Integer> numPositions,
for (int tries = 0; tries < 20; tries++) { List<Integer> trigPositions, boolean isSquare) {
int idx = numPositions.get(RANDOM.nextInt(numPositions.size())); List<Integer> candidates = new ArrayList<>();
for (int idx : numPositions) {
String token = tokens[idx];
if ((avoidPositions == null || !avoidPositions.contains(idx)) && canApplyPower(tokens, idx)) { // 避免 trig 位置
return idx; if (trigPositions != null && trigPositions.contains(idx)) {
continue;
}
// 不能已有运算
if (token.contains("^") || token.contains("√")) {
continue;
}
// 括号匹配检查
if ((token.equals("(") || token.equals(")")) && !checkParenCanAdd(tokens, idx, isSquare)) {
continue;
}
// 根号/平方合法性
if (isSquare) {
if (isNumber(token) || token.equals(")")) {
candidates.add(idx);
}
}
if (!isSquare) {
if (isNumber(token) || token.equals("(")) {
candidates.add(idx);
}
} }
} }
return -1;
if (candidates.isEmpty()) {
return -1;
}
return candidates.get(RANDOM.nextInt(candidates.size()));
} }
// 检查该位置是否可以加幂运算 // 检查括号匹配另一侧是否可以加运算
private boolean canApplyPower(String[] tokens, int index) { private boolean checkParenCanAdd(String[] tokens, int index, boolean isSquare) {
if (!isNumber(tokens[index])) return false; int match = findMatchingParen(tokens, index);
if (tokens[index].contains("^") || tokens[index].contains("√")) return false; // 避免重复幂运算 if (match == -1) {
return false;
}
// 不能加在运算符右边 // 检查匹配括号是否已加运算
if (index > 0) { if (tokens[match].contains("^") || tokens[match].contains("√")) {
String prev = tokens[index - 1]; return false;
if (prev.equals("/") || prev.equals("*") || prev.equals("+") || prev.equals("-")) {
return false;
}
} }
return true;
}
// 应用平方或开方(根号放左括号左侧,平方放右括号右侧) // 左括号只能加根号,右括号只能加平方
private String applyPower(String token, String[] tokens, int index) { if (isSquare && tokens[index].equals("(")) {
boolean useSqrt = RANDOM.nextBoolean(); return false;
}
return isSquare || !tokens[index].equals(")");
}
if (useSqrt) { // 找匹配括号
// 根号放在左括号左侧,如果前面是左括号,则放在括号左边 private int findMatchingParen(String[] tokens, int index) {
if (index > 0 && tokens[index - 1].equals(")")) { if (tokens[index].equals("(")) {
return "√" + token; // 特殊情况,仍然放数字前 int depth = 0;
for (int i = index; i < tokens.length; i++) {
if (tokens[i].equals("(")) {
depth++;
} else if (tokens[i].equals(")")) {
depth--;
if (depth == 0) {
return i;
}
}
} }
if (index > 0 && tokens[index - 1].equals("(")) { } else if (tokens[index].equals(")")) {
return "√" + token; int depth = 0;
for (int i = index; i >= 0; i--) {
if (tokens[i].equals(")")) {
depth++;
} else if (tokens[i].equals("(")) {
depth--;
if (depth == 0) {
return i;
}
}
} }
return "√" + token;
} else {
// 平方放在右括号右侧,如果后面是右括号,则放在括号右边
if (index < tokens.length - 1 && tokens[index + 1].equals(")")) {
return token + "^2";
}
return token + "^2";
} }
return -1;
} }
private boolean isNumber(String token) { private boolean isNumber(String token) {

Loading…
Cancel
Save