parent
f47190ba33
commit
06383dd7fa
@ -0,0 +1,137 @@
|
|||||||
|
// Primaryproblem.java
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 小学数学题目生成器实现类。
|
||||||
|
*/
|
||||||
|
public class PrimarySchoolProblemGenerator implements MathProblemGenerator {
|
||||||
|
private static final Random random = new Random();
|
||||||
|
private static int basicOperationCounter = 1;
|
||||||
|
private static int expressionComplexityCounter = 1;
|
||||||
|
private String userName = "default";
|
||||||
|
private String timestamp = "default";
|
||||||
|
private int problemNumber = 1;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
PrimarySchoolProblemGenerator generator = new PrimarySchoolProblemGenerator();
|
||||||
|
generator.userName = "张三1";
|
||||||
|
TimeManager timeManager = new TimeManager();
|
||||||
|
generator.timestamp = timeManager.getCurrentTime();
|
||||||
|
|
||||||
|
for (int index = 0; index < 15; index++) {
|
||||||
|
basicOperationCounter = 1;
|
||||||
|
expressionComplexityCounter = 1;
|
||||||
|
System.out.println((index + 1) + ". " + generator.generateSmartExpression());
|
||||||
|
generator.problemNumber++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start(int problemCount, String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
TimeManager timeManager = new TimeManager();
|
||||||
|
timestamp = timeManager.getCurrentTime();
|
||||||
|
|
||||||
|
for (int index = 0; index < problemCount; index++) {
|
||||||
|
basicOperationCounter = 1;
|
||||||
|
expressionComplexityCounter = 1;
|
||||||
|
System.out.println((index + 1) + ". " + generateSmartExpression());
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String generateSmartExpression() {
|
||||||
|
int operatorCount = random.nextInt(3) + 1;
|
||||||
|
int basicOperationTarget = random.nextInt(operatorCount) + 1;
|
||||||
|
String result;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
boolean isDuplicate = false;
|
||||||
|
result = buildExpression(operatorCount, basicOperationTarget);
|
||||||
|
|
||||||
|
ExpressionJudge expressionJudge = new ExpressionJudge();
|
||||||
|
if (expressionJudge.hasError(result)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
FileManager fileManager = new FileManager();
|
||||||
|
fileManager.readUserFiles(userName);
|
||||||
|
|
||||||
|
for (String line : fileManager.getFileLines()) {
|
||||||
|
String[] parts = line.split("\\.", 2);
|
||||||
|
String expression = parts.length > 1 ? parts[1] : line;
|
||||||
|
if (result.equals(expression)) {
|
||||||
|
isDuplicate = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isDuplicate) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
fileManager.createUserFile(userName, timestamp);
|
||||||
|
fileManager.writeToFile(userName, timestamp, problemNumber + "." + result);
|
||||||
|
fileManager.writeToFile(userName, timestamp, " ");
|
||||||
|
problemNumber++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String buildExpression(int operatorCount, int basicOperationTarget) {
|
||||||
|
if (operatorCount == 1) {
|
||||||
|
return generateSimpleExpression(basicOperationTarget);
|
||||||
|
}
|
||||||
|
|
||||||
|
int splitPoint = random.nextInt(operatorCount - 1) + 1;
|
||||||
|
|
||||||
|
String leftPart = (splitPoint > 1 && random.nextBoolean())
|
||||||
|
? "(" + buildExpression(splitPoint, basicOperationTarget) + ")"
|
||||||
|
: buildExpression(splitPoint, basicOperationTarget);
|
||||||
|
|
||||||
|
String rightPart = (operatorCount - splitPoint > 1 && random.nextBoolean())
|
||||||
|
? "(" + buildExpression(operatorCount - splitPoint, basicOperationTarget) + ")"
|
||||||
|
: buildExpression(operatorCount - splitPoint, basicOperationTarget);
|
||||||
|
|
||||||
|
char operator = getRandomOperator("+-*/");
|
||||||
|
return leftPart + " " + operator + " " + rightPart;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String generateSimpleExpression(int basicOperationTarget) {
|
||||||
|
int firstOperand = random.nextInt(100) + 1;
|
||||||
|
int secondOperand = random.nextInt(100) + 1;
|
||||||
|
char operator = getRandomOperator("+-*/");
|
||||||
|
|
||||||
|
if (expressionComplexityCounter == 3) {
|
||||||
|
return firstOperand + "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (basicOperationCounter == basicOperationTarget) {
|
||||||
|
operator = getRandomOperator("+-*/");
|
||||||
|
expressionComplexityCounter++;
|
||||||
|
return firstOperand + " " + operator + " " + secondOperand;
|
||||||
|
} else {
|
||||||
|
basicOperationCounter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (random.nextInt(2) == 0) {
|
||||||
|
return firstOperand + "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (random.nextBoolean()) {
|
||||||
|
expressionComplexityCounter++;
|
||||||
|
return firstOperand + " " + operator + " " + secondOperand;
|
||||||
|
} else {
|
||||||
|
expressionComplexityCounter++;
|
||||||
|
return "(" + firstOperand + " " + operator + " " + secondOperand + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getRandomOperator(String operators) {
|
||||||
|
return operators.charAt(random.nextInt(operators.length()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue