@ -1,98 +1,104 @@
|
||||
package src;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class FileManager {
|
||||
private String baseDir;
|
||||
|
||||
public FileManager() {
|
||||
this.baseDir = System.getProperty("user.dir") + File.separator + "papers";
|
||||
createBaseDirectory();
|
||||
|
||||
private String baseDir;
|
||||
|
||||
public FileManager() {
|
||||
this.baseDir = System.getProperty("user.dir") + File.separator + "papers";
|
||||
createBaseDirectory();
|
||||
}
|
||||
|
||||
// 创建基础目录
|
||||
private void createBaseDirectory() {
|
||||
File dir = new File(baseDir);
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
|
||||
// 创建基础目录
|
||||
private void createBaseDirectory() {
|
||||
File dir = new File(baseDir);
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
}
|
||||
|
||||
// 为用户创建目录
|
||||
private String createUserDirectory(String username) {
|
||||
String userDir = baseDir + File.separator + username;
|
||||
File dir = new File(userDir);
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
|
||||
// 为用户创建目录
|
||||
private String createUserDirectory(String username) {
|
||||
String userDir = baseDir + File.separator + username;
|
||||
File dir = new File(userDir);
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
return userDir;
|
||||
}
|
||||
|
||||
// 生成文件名
|
||||
private String generateFileName() {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
|
||||
return sdf.format(new Date()) + ".txt";
|
||||
}
|
||||
|
||||
// 保存题目到文件
|
||||
public void saveQuestions(String username, String[] questions) throws IOException {
|
||||
String userDir = createUserDirectory(username);
|
||||
String fileName = generateFileName();
|
||||
String filePath = userDir + File.separator + fileName;
|
||||
|
||||
try (PrintWriter writer = new PrintWriter(new FileWriter(filePath))) {
|
||||
for (int i = 0; i < questions.length; i++) {
|
||||
writer.println((i + 1) + ". " + questions[i]);
|
||||
if (i < questions.length - 1) {
|
||||
writer.println(); // 题目之间空一行
|
||||
}
|
||||
return userDir;
|
||||
}
|
||||
}
|
||||
|
||||
// 生成文件名
|
||||
private String generateFileName() {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
|
||||
return sdf.format(new Date()) + ".txt";
|
||||
}
|
||||
|
||||
// 保存题目到文件
|
||||
public void saveQuestions(String username, String[] questions) throws IOException {
|
||||
String userDir = createUserDirectory(username);
|
||||
String fileName = generateFileName();
|
||||
String filePath = userDir + File.separator + fileName;
|
||||
|
||||
try (PrintWriter writer = new PrintWriter(new FileWriter(filePath))) {
|
||||
for (int i = 0; i < questions.length; i++) {
|
||||
writer.println((i + 1) + ". " + questions[i]);
|
||||
if (i < questions.length - 1) {
|
||||
writer.println(); // 题目之间空一行
|
||||
|
||||
System.out.println("题目已保存到: " + filePath);
|
||||
}
|
||||
|
||||
// 检查题目是否重复(读取用户目录下所有文件)
|
||||
public Set<String> loadExistingQuestions(String username) {
|
||||
Set<String> existingQuestions = new HashSet<>();
|
||||
String userDir = baseDir + File.separator + username;
|
||||
File dir = new File(userDir);
|
||||
|
||||
if (dir.exists() && dir.isDirectory()) {
|
||||
File[] files = dir.listFiles((d, name) -> name.endsWith(".txt"));
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||
String line;
|
||||
StringBuilder questionBuilder = new StringBuilder();
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (line.trim().isEmpty()) {
|
||||
if (questionBuilder.length() > 0) {
|
||||
// 移除题号
|
||||
String question = questionBuilder.toString().replaceAll("^\\d+\\.\\s*", "");
|
||||
existingQuestions.add(question.trim());
|
||||
questionBuilder.setLength(0);
|
||||
}
|
||||
} else {
|
||||
questionBuilder.append(line).append(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("题目已保存到: " + filePath);
|
||||
}
|
||||
|
||||
// 检查题目是否重复(读取用户目录下所有文件)
|
||||
public Set<String> loadExistingQuestions(String username) {
|
||||
Set<String> existingQuestions = new HashSet<>();
|
||||
String userDir = baseDir + File.separator + username;
|
||||
File dir = new File(userDir);
|
||||
|
||||
if (dir.exists() && dir.isDirectory()) {
|
||||
File[] files = dir.listFiles((d, name) -> name.endsWith(".txt"));
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||
String line;
|
||||
StringBuilder questionBuilder = new StringBuilder();
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (line.trim().isEmpty()) {
|
||||
if (questionBuilder.length() > 0) {
|
||||
// 移除题号
|
||||
String question = questionBuilder.toString().replaceAll("^\\d+\\.\\s*", "");
|
||||
existingQuestions.add(question.trim());
|
||||
questionBuilder.setLength(0);
|
||||
}
|
||||
} else {
|
||||
questionBuilder.append(line).append(" ");
|
||||
}
|
||||
}
|
||||
// 处理最后一个题目
|
||||
if (questionBuilder.length() > 0) {
|
||||
String question = questionBuilder.toString().replaceAll("^\\d+\\.\\s*", "");
|
||||
existingQuestions.add(question.trim());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("读取文件失败: " + file.getName());
|
||||
}
|
||||
}
|
||||
// 处理最后一个题目
|
||||
if (questionBuilder.length() > 0) {
|
||||
String question = questionBuilder.toString().replaceAll("^\\d+\\.\\s*", "");
|
||||
existingQuestions.add(question.trim());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("读取文件失败: " + file.getName());
|
||||
}
|
||||
}
|
||||
|
||||
return existingQuestions;
|
||||
}
|
||||
}
|
||||
|
||||
return existingQuestions;
|
||||
}
|
||||
}
|
||||
@ -1,85 +1,88 @@
|
||||
package src;
|
||||
|
||||
public class HighSchoolGenerator extends QuestionGenerator {
|
||||
private static final String[] BASIC_OPERATORS = {"+", "-", "*", "/"};
|
||||
private static final String[] TRIG_FUNCTIONS = {"sin", "cos", "tan"};
|
||||
|
||||
public HighSchoolGenerator() {
|
||||
super("高中");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateQuestion() {
|
||||
String question;
|
||||
do {
|
||||
question = generateSingleQuestion();
|
||||
} while (isDuplicate(question) || !question.matches(".*(sin|cos|tan)\\(.*\\).*")); // 确保包含三角函数
|
||||
|
||||
addToGenerated(question);
|
||||
return question;
|
||||
}
|
||||
|
||||
private String generateSingleQuestion() {
|
||||
int operatorCount = generateOperatorCount();
|
||||
StringBuilder questionBuilder = new StringBuilder();
|
||||
boolean hasTrigFunction = false;
|
||||
|
||||
// 生成第一个操作数或三角函数
|
||||
hasTrigFunction = processFirstOperandOrTrig(questionBuilder);
|
||||
|
||||
// 生成操作符和操作数
|
||||
processOperatorsAndOperands(questionBuilder, operatorCount, hasTrigFunction);
|
||||
|
||||
return questionBuilder.toString();
|
||||
}
|
||||
|
||||
private boolean processFirstOperandOrTrig(StringBuilder builder) {
|
||||
if (random.nextBoolean()) {
|
||||
// 使用三角函数
|
||||
String trigFunction = TRIG_FUNCTIONS[random.nextInt(TRIG_FUNCTIONS.length)];
|
||||
int angle = random.nextInt(360) + 1; // 1-360度
|
||||
builder.append(trigFunction).append("(").append(angle).append("°)");
|
||||
return true;
|
||||
} else {
|
||||
// 使用普通数字
|
||||
builder.append(generateOperand());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void processOperatorsAndOperands(StringBuilder builder, int operatorCount, boolean hasTrigFunction) {
|
||||
for (int i = 0; i < operatorCount; i++) {
|
||||
String operator = BASIC_OPERATORS[random.nextInt(BASIC_OPERATORS.length)];
|
||||
|
||||
// 决定下一个操作数是普通数字还是三角函数
|
||||
if (!hasTrigFunction && i == operatorCount - 1) {
|
||||
// 确保至少有一个三角函数
|
||||
appendTrigFunction(builder, operator);
|
||||
hasTrigFunction = true;
|
||||
} else if (random.nextBoolean()) {
|
||||
// 使用三角函数
|
||||
appendTrigFunction(builder, operator);
|
||||
hasTrigFunction = true;
|
||||
} else {
|
||||
// 使用普通数字
|
||||
appendBasicOperand(builder, operator, operatorCount);
|
||||
}
|
||||
}
|
||||
|
||||
private static final String[] BASIC_OPERATORS = {"+", "-", "*", "/"};
|
||||
private static final String[] TRIG_FUNCTIONS = {"sin", "cos", "tan"};
|
||||
|
||||
public HighSchoolGenerator() {
|
||||
super("高中");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateQuestion() {
|
||||
String question;
|
||||
do {
|
||||
question = generateSingleQuestion();
|
||||
} while (isDuplicate(question) || !question.matches(".*(sin|cos|tan)\\(.*\\).*")); // 确保包含三角函数
|
||||
|
||||
addToGenerated(question);
|
||||
return question;
|
||||
}
|
||||
|
||||
private String generateSingleQuestion() {
|
||||
int operatorCount = generateOperatorCount();
|
||||
StringBuilder questionBuilder = new StringBuilder();
|
||||
boolean hasTrigFunction = false;
|
||||
|
||||
// 生成第一个操作数或三角函数
|
||||
hasTrigFunction = processFirstOperandOrTrig(questionBuilder);
|
||||
|
||||
// 生成操作符和操作数
|
||||
processOperatorsAndOperands(questionBuilder, operatorCount, hasTrigFunction);
|
||||
|
||||
return questionBuilder.toString();
|
||||
}
|
||||
|
||||
private boolean processFirstOperandOrTrig(StringBuilder builder) {
|
||||
if (random.nextBoolean()) {
|
||||
// 使用三角函数
|
||||
String trigFunction = TRIG_FUNCTIONS[random.nextInt(TRIG_FUNCTIONS.length)];
|
||||
int angle = random.nextInt(360) + 1; // 1-360度
|
||||
builder.append(trigFunction).append("(").append(angle).append("°)");
|
||||
return true;
|
||||
} else {
|
||||
// 使用普通数字
|
||||
builder.append(generateOperand());
|
||||
return false;
|
||||
}
|
||||
|
||||
private void appendTrigFunction(StringBuilder builder, String operator) {
|
||||
String trigFunction = TRIG_FUNCTIONS[random.nextInt(TRIG_FUNCTIONS.length)];
|
||||
int angle = random.nextInt(360) + 1;
|
||||
builder.append(" ").append(operator).append(" ").append(trigFunction).append("(").append(angle).append("°)");
|
||||
}
|
||||
|
||||
private void processOperatorsAndOperands(StringBuilder builder, int operatorCount,
|
||||
boolean hasTrigFunction) {
|
||||
for (int i = 0; i < operatorCount; i++) {
|
||||
String operator = BASIC_OPERATORS[random.nextInt(BASIC_OPERATORS.length)];
|
||||
|
||||
// 决定下一个操作数是普通数字还是三角函数
|
||||
if (!hasTrigFunction && i == operatorCount - 1) {
|
||||
// 确保至少有一个三角函数
|
||||
appendTrigFunction(builder, operator);
|
||||
hasTrigFunction = true;
|
||||
} else if (random.nextBoolean()) {
|
||||
// 使用三角函数
|
||||
appendTrigFunction(builder, operator);
|
||||
hasTrigFunction = true;
|
||||
} else {
|
||||
// 使用普通数字
|
||||
appendBasicOperand(builder, operator, operatorCount);
|
||||
}
|
||||
}
|
||||
|
||||
private void appendBasicOperand(StringBuilder builder, String operator, int operatorCount) {
|
||||
int operand = generateOperand();
|
||||
// 随机决定是否加括号
|
||||
if (random.nextBoolean() && operatorCount > 1) {
|
||||
builder.insert(0, "(").append(" ").append(operator).append(" ").append(operand).append(")");
|
||||
} else {
|
||||
builder.append(" ").append(operator).append(" ").append(operand);
|
||||
}
|
||||
}
|
||||
|
||||
private void appendTrigFunction(StringBuilder builder, String operator) {
|
||||
String trigFunction = TRIG_FUNCTIONS[random.nextInt(TRIG_FUNCTIONS.length)];
|
||||
int angle = random.nextInt(360) + 1;
|
||||
builder.append(" ").append(operator).append(" ").append(trigFunction).append("(").append(angle)
|
||||
.append("°)");
|
||||
}
|
||||
|
||||
private void appendBasicOperand(StringBuilder builder, String operator, int operatorCount) {
|
||||
int operand = generateOperand();
|
||||
// 随机决定是否加括号
|
||||
if (random.nextBoolean() && operatorCount > 1) {
|
||||
builder.insert(0, "(").append(" ").append(operator).append(" ").append(operand).append(")");
|
||||
} else {
|
||||
builder.append(" ").append(operator).append(" ").append(operand);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,95 +1,99 @@
|
||||
package src;
|
||||
|
||||
public class MiddleSchoolGenerator extends QuestionGenerator {
|
||||
private static final String[] BASIC_OPERATORS = {"+", "-", "*", "/"};
|
||||
private static final String[] ADVANCED_OPERATORS = {"²", "√"};
|
||||
|
||||
public MiddleSchoolGenerator() {
|
||||
super("初中");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateQuestion() {
|
||||
String question;
|
||||
do {
|
||||
question = generateSingleQuestion();
|
||||
} while (isDuplicate(question) || !question.matches(".*[²√].*")); // 确保包含高级运算符
|
||||
|
||||
addToGenerated(question);
|
||||
return question;
|
||||
}
|
||||
|
||||
private String generateSingleQuestion() {
|
||||
int operatorCount = generateOperatorCount();
|
||||
StringBuilder questionBuilder = new StringBuilder();
|
||||
boolean hasAdvancedOperator = false;
|
||||
|
||||
// 生成第一个操作数
|
||||
int firstOperand = generateOperand();
|
||||
|
||||
// 处理第一个操作数(可能使用高级运算符)
|
||||
hasAdvancedOperator = processFirstOperand(questionBuilder, firstOperand);
|
||||
|
||||
// 生成操作符和操作数
|
||||
processOperatorsAndOperands(questionBuilder, operatorCount, hasAdvancedOperator);
|
||||
|
||||
return questionBuilder.toString();
|
||||
}
|
||||
|
||||
private boolean processFirstOperand(StringBuilder builder, int operand) {
|
||||
// 随机决定第一个操作数是否使用高级运算符
|
||||
if (random.nextBoolean()) {
|
||||
String advancedOp = ADVANCED_OPERATORS[random.nextInt(ADVANCED_OPERATORS.length)];
|
||||
if (advancedOp.equals("²")) {
|
||||
builder.append(operand).append("²");
|
||||
} else {
|
||||
builder.append("√").append(operand);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
builder.append(operand);
|
||||
return false;
|
||||
}
|
||||
|
||||
private static final String[] BASIC_OPERATORS = {"+", "-", "*", "/"};
|
||||
private static final String[] ADVANCED_OPERATORS = {"²", "√"};
|
||||
|
||||
public MiddleSchoolGenerator() {
|
||||
super("初中");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateQuestion() {
|
||||
String question;
|
||||
do {
|
||||
question = generateSingleQuestion();
|
||||
} while (isDuplicate(question) || !question.matches(".*[²√].*")); // 确保包含高级运算符
|
||||
|
||||
addToGenerated(question);
|
||||
return question;
|
||||
}
|
||||
|
||||
private String generateSingleQuestion() {
|
||||
int operatorCount = generateOperatorCount();
|
||||
StringBuilder questionBuilder = new StringBuilder();
|
||||
boolean hasAdvancedOperator = false;
|
||||
|
||||
// 生成第一个操作数
|
||||
int firstOperand = generateOperand();
|
||||
|
||||
// 处理第一个操作数(可能使用高级运算符)
|
||||
hasAdvancedOperator = processFirstOperand(questionBuilder, firstOperand);
|
||||
|
||||
// 生成操作符和操作数
|
||||
processOperatorsAndOperands(questionBuilder, operatorCount, hasAdvancedOperator);
|
||||
|
||||
return questionBuilder.toString();
|
||||
}
|
||||
|
||||
private boolean processFirstOperand(StringBuilder builder, int operand) {
|
||||
// 随机决定第一个操作数是否使用高级运算符
|
||||
if (random.nextBoolean()) {
|
||||
String advancedOp = ADVANCED_OPERATORS[random.nextInt(ADVANCED_OPERATORS.length)];
|
||||
if (advancedOp.equals("²")) {
|
||||
builder.append(operand).append("²");
|
||||
} else {
|
||||
builder.append("√").append(operand);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
builder.append(operand);
|
||||
return false;
|
||||
}
|
||||
|
||||
private void processOperatorsAndOperands(StringBuilder builder, int operatorCount, boolean hasAdvancedOperator) {
|
||||
for (int i = 0; i < operatorCount; i++) {
|
||||
String operator = determineOperator(i, operatorCount, hasAdvancedOperator);
|
||||
int operand = generateOperand();
|
||||
|
||||
if (operator.equals("²") || operator.equals("√")) {
|
||||
processAdvancedOperator(builder, operator, operand);
|
||||
hasAdvancedOperator = true;
|
||||
} else {
|
||||
processBasicOperator(builder, operator, operand, operatorCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void processOperatorsAndOperands(StringBuilder builder, int operatorCount,
|
||||
boolean hasAdvancedOperator) {
|
||||
for (int i = 0; i < operatorCount; i++) {
|
||||
String operator = determineOperator(i, operatorCount, hasAdvancedOperator);
|
||||
int operand = generateOperand();
|
||||
|
||||
if (operator.equals("²") || operator.equals("√")) {
|
||||
processAdvancedOperator(builder, operator, operand);
|
||||
hasAdvancedOperator = true;
|
||||
} else {
|
||||
processBasicOperator(builder, operator, operand, operatorCount);
|
||||
}
|
||||
}
|
||||
|
||||
private String determineOperator(int currentIndex, int totalOperators, boolean hasAdvancedOperator) {
|
||||
if (!hasAdvancedOperator && currentIndex == totalOperators - 1) {
|
||||
// 确保至少有一个高级运算符
|
||||
return ADVANCED_OPERATORS[random.nextInt(ADVANCED_OPERATORS.length)];
|
||||
} else {
|
||||
return BASIC_OPERATORS[random.nextInt(BASIC_OPERATORS.length)];
|
||||
}
|
||||
}
|
||||
|
||||
private String determineOperator(int currentIndex, int totalOperators,
|
||||
boolean hasAdvancedOperator) {
|
||||
if (!hasAdvancedOperator && currentIndex == totalOperators - 1) {
|
||||
// 确保至少有一个高级运算符
|
||||
return ADVANCED_OPERATORS[random.nextInt(ADVANCED_OPERATORS.length)];
|
||||
} else {
|
||||
return BASIC_OPERATORS[random.nextInt(BASIC_OPERATORS.length)];
|
||||
}
|
||||
|
||||
private void processAdvancedOperator(StringBuilder builder, String operator, int operand) {
|
||||
String basicOp = BASIC_OPERATORS[random.nextInt(BASIC_OPERATORS.length)];
|
||||
if (operator.equals("²")) {
|
||||
builder.append(" ").append(basicOp).append(" ").append(operand).append("²");
|
||||
} else {
|
||||
builder.append(" ").append(basicOp).append(" √").append(operand);
|
||||
}
|
||||
}
|
||||
|
||||
private void processAdvancedOperator(StringBuilder builder, String operator, int operand) {
|
||||
String basicOp = BASIC_OPERATORS[random.nextInt(BASIC_OPERATORS.length)];
|
||||
if (operator.equals("²")) {
|
||||
builder.append(" ").append(basicOp).append(" ").append(operand).append("²");
|
||||
} else {
|
||||
builder.append(" ").append(basicOp).append(" √").append(operand);
|
||||
}
|
||||
|
||||
private void processBasicOperator(StringBuilder builder, String operator, int operand, int operatorCount) {
|
||||
// 随机决定是否加括号
|
||||
if (random.nextBoolean() && operatorCount > 1) {
|
||||
builder.insert(0, "(").append(" ").append(operator).append(" ").append(operand).append(")");
|
||||
} else {
|
||||
builder.append(" ").append(operator).append(" ").append(operand);
|
||||
}
|
||||
}
|
||||
|
||||
private void processBasicOperator(StringBuilder builder, String operator, int operand,
|
||||
int operatorCount) {
|
||||
// 随机决定是否加括号
|
||||
if (random.nextBoolean() && operatorCount > 1) {
|
||||
builder.insert(0, "(").append(" ").append(operator).append(" ").append(operand).append(")");
|
||||
} else {
|
||||
builder.append(" ").append(operator).append(" ").append(operand);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,40 +1,41 @@
|
||||
package src;
|
||||
|
||||
public class PrimarySchoolGenerator extends QuestionGenerator {
|
||||
private static final String[] OPERATORS = {"+", "-", "*", "/"};
|
||||
|
||||
public PrimarySchoolGenerator() {
|
||||
super("小学");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateQuestion() {
|
||||
String question;
|
||||
do {
|
||||
int operatorCount = generateOperatorCount();
|
||||
StringBuilder questionBuilder = new StringBuilder();
|
||||
|
||||
// 生成第一个操作数
|
||||
questionBuilder.append(generateOperand());
|
||||
|
||||
// 生成操作符和操作数
|
||||
for (int i = 0; i < operatorCount; i++) {
|
||||
String operator = OPERATORS[random.nextInt(OPERATORS.length)];
|
||||
int operand = generateOperand();
|
||||
|
||||
// 随机决定是否加括号
|
||||
if (random.nextBoolean() && operatorCount > 1) {
|
||||
questionBuilder.insert(0, "(").append(" " + operator + " " + operand + ")");
|
||||
} else {
|
||||
questionBuilder.append(" " + operator + " " + operand);
|
||||
}
|
||||
}
|
||||
|
||||
question = questionBuilder.toString();
|
||||
|
||||
} while (isDuplicate(question));
|
||||
|
||||
addToGenerated(question);
|
||||
return question;
|
||||
}
|
||||
|
||||
private static final String[] OPERATORS = {"+", "-", "*", "/"};
|
||||
|
||||
public PrimarySchoolGenerator() {
|
||||
super("小学");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateQuestion() {
|
||||
String question;
|
||||
do {
|
||||
int operatorCount = generateOperatorCount_one();
|
||||
StringBuilder questionBuilder = new StringBuilder();
|
||||
|
||||
// 生成第一个操作数
|
||||
questionBuilder.append(generateOperand());
|
||||
|
||||
// 生成操作符和操作数
|
||||
for (int i = 0; i < operatorCount; i++) {
|
||||
String operator = OPERATORS[random.nextInt(OPERATORS.length)];
|
||||
int operand = generateOperand();
|
||||
|
||||
// 随机决定是否加括号
|
||||
if (random.nextBoolean() && operatorCount > 1) {
|
||||
questionBuilder.insert(0, "(").append(" " + operator + " " + operand + ")");
|
||||
} else {
|
||||
questionBuilder.append(" " + operator + " " + operand);
|
||||
}
|
||||
}
|
||||
|
||||
question = questionBuilder.toString();
|
||||
|
||||
} while (isDuplicate(question));
|
||||
|
||||
addToGenerated(question);
|
||||
return question;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue