重写查重逻辑,交换操作数顺序的题目不视为重复

main
Teptao 7 months ago
parent f2970a1ee2
commit 73a07cdccf

@ -33,7 +33,7 @@ public abstract class AbstractProblemGenerator implements IProblemGenerator {
while (newProblems.size() < count && tries < maxTries) {
Equation candidate = createProblem();
String canonicalForm = candidate.toCanonicalString();
String canonicalForm = candidate.toString();
if (!existingProblems.contains(canonicalForm) && currentBatchSet.add(canonicalForm)) {
newProblems.add(candidate);

@ -51,7 +51,7 @@ public class Equation {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
// 使用 IntStream 来优雅地处理操作数和操作符的交替拼接
// 使用 IntStream 处理操作数和操作符的交替拼接
IntStream.range(0, operators.size())
.forEach(i -> sb.append(operands.get(i))
.append(" ")
@ -62,21 +62,4 @@ public class Equation {
return sb.toString();
}
/**
*
* ( "3 + 5") ("3 + 5")
*
*
* @return
*/
public String toCanonicalString() {
// 简化处理:仅对两个操作数且操作符可交换的情况进行规范化
if (operands.size() == 2 && operators.size() == 1 && operators.get(0).isCommutative()) {
List<String> sortedOperands = operands.stream().sorted().collect(Collectors.toList());
return sortedOperands.get(0) + " " + operators.get(0).getSymbol() + " " + sortedOperands.get(1);
}
// 对于其他所有复杂情况,直接按原样拼接
return toString().replace(" = ?", "");
}
}

@ -21,7 +21,7 @@ public interface IFileService {
*
*
* @param user
* @return
* @return
* @throws IOException
*/
Set<String> loadAllProblemHistory(User user) throws IOException;

@ -12,7 +12,6 @@ import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
@ -24,9 +23,6 @@ public class TextFilePersistence implements IFileService {
private static final DateTimeFormatter FILE_NAME_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");
// 正则表达式用于从文件中解析出题目表达式部分(忽略题号和答案)
private static final Pattern PROBLEM_PATTERN = Pattern.compile("^\\d+\\.\\s*(.+?)\\s*=");
@Override
public void saveProblems(User user, List<Equation> problems) throws IOException {
Path userDirectory = Paths.get(user.getStoragePath());
@ -71,39 +67,14 @@ public class TextFilePersistence implements IFileService {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
Matcher matcher = PROBLEM_PATTERN.matcher(line.trim());
if (matcher.find()) {
String expression = matcher.group(1).trim();
// 将解析出的表达式转换为规范化形式
// 这是一个简化的转换,假设表达式格式固定
// 更健壮的实现会构建一个临时的 Equation 对象来获取其规范化字符串
String canonicalForm = normalizeExpression(expression);
historySet.add(canonicalForm);
}
String cleanedLine = line.replaceFirst("^\\d+\\.\\s*", "");
System.out.println(cleanedLine);
historySet.add(cleanedLine);
}
}
}
return historySet;
}
/**
*
* Equation.toCanonicalString()
*
* @param expression "5 + 3" "12 * 4"
* @return
*/
private String normalizeExpression(String expression) {
String[] parts = expression.split(" ");
if (parts.length == 3) {
String op = parts[1];
if (op.equals("+") || op.equals("*")) {
// 如果是可交换运算符,对操作数进行排序
if (parts[0].compareTo(parts[2]) > 0) {
return parts[2] + " " + op + " " + parts[0];
}
}
}
return expression; // 其他情况直接返回
}
}
Loading…
Cancel
Save