|
|
|
|
@ -1,123 +1,102 @@
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface IQuestionGenerator {
|
|
|
|
|
String generateQuestion();
|
|
|
|
|
boolean isValidQuestion(String question);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PrimaryQuestionGenerator implements IQuestionGenerator {
|
|
|
|
|
private Random random = new Random();
|
|
|
|
|
private String[] operators = {"+", "-", "*", "/"};
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String generateQuestion() {
|
|
|
|
|
int operandCount = random.nextInt(3) + 2;
|
|
|
|
|
StringBuilder question = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
question.append(random.nextInt(100) + 1);
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < operandCount; i++) {
|
|
|
|
|
String operator = operators[random.nextInt(operators.length)];
|
|
|
|
|
int operand = random.nextInt(100) + 1;
|
|
|
|
|
try {
|
|
|
|
|
int operandCount = random.nextInt(3) + 2;
|
|
|
|
|
StringBuilder question = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
if (random.nextBoolean() && i < operandCount - 1) {
|
|
|
|
|
question.append(" ").append(operator).append(" (").append(operand);
|
|
|
|
|
if (random.nextBoolean()) {
|
|
|
|
|
question.append(" ").append(operators[random.nextInt(operators.length)])
|
|
|
|
|
.append(" ").append(random.nextInt(100) + 1).append(")");
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
question.append(random.nextInt(100) + 1);
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < operandCount; i++) {
|
|
|
|
|
String operator = operators[random.nextInt(operators.length)];
|
|
|
|
|
int operand = random.nextInt(100) + 1;
|
|
|
|
|
question.append(" ").append(operator).append(" ").append(operand);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return question.toString() + " = ";
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return (random.nextInt(100) + 1) + " + " + (random.nextInt(100) + 1) + " = ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String result = question.toString();
|
|
|
|
|
int openCount = countChar(result, '(');
|
|
|
|
|
int closeCount = countChar(result, ')');
|
|
|
|
|
while (openCount > closeCount) {
|
|
|
|
|
result += ")";
|
|
|
|
|
closeCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result + " = ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isValidQuestion(String question) {
|
|
|
|
|
return question.matches("^[0-9+\\-*/().\\s]+=$") &&
|
|
|
|
|
!question.contains("²") && !question.contains("√") &&
|
|
|
|
|
!question.contains("sin") && !question.contains("cos") && !question.contains("tan");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int countChar(String str, char ch) {
|
|
|
|
|
int count = 0;
|
|
|
|
|
for (char c : str.toCharArray()) {
|
|
|
|
|
if (c == ch) count++;
|
|
|
|
|
try {
|
|
|
|
|
return question != null &&
|
|
|
|
|
question.contains("=") &&
|
|
|
|
|
!question.contains("²") &&
|
|
|
|
|
!question.contains("√") &&
|
|
|
|
|
!question.contains("sin") &&
|
|
|
|
|
!question.contains("cos") &&
|
|
|
|
|
!question.contains("tan");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class JuniorQuestionGenerator implements IQuestionGenerator {
|
|
|
|
|
private Random random = new Random();
|
|
|
|
|
private String[] operators = {"+", "-", "*", "/"};
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String generateQuestion() {
|
|
|
|
|
StringBuilder question = new StringBuilder();
|
|
|
|
|
int operandCount = random.nextInt(3) + 2;
|
|
|
|
|
boolean hasSpecialOperator = false;
|
|
|
|
|
|
|
|
|
|
question.append(random.nextInt(100) + 1);
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < operandCount; i++) {
|
|
|
|
|
String operator;
|
|
|
|
|
if (!hasSpecialOperator && random.nextDouble() < 0.4) {
|
|
|
|
|
if (random.nextBoolean()) {
|
|
|
|
|
operator = "²";
|
|
|
|
|
hasSpecialOperator = true;
|
|
|
|
|
} else {
|
|
|
|
|
operator = "√";
|
|
|
|
|
hasSpecialOperator = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
operator = operators[random.nextInt(operators.length)];
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
StringBuilder question = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
int operand = random.nextInt(100) + 1;
|
|
|
|
|
|
|
|
|
|
if (operator.equals("²")) {
|
|
|
|
|
question.append("²");
|
|
|
|
|
} else if (operator.equals("√")) {
|
|
|
|
|
question.append(" ").append(operator).append(operand);
|
|
|
|
|
} else {
|
|
|
|
|
question.append(" ").append(operator).append(" ").append(operand);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!hasSpecialOperator) {
|
|
|
|
|
if (random.nextBoolean()) {
|
|
|
|
|
question.append("²");
|
|
|
|
|
question.append("√").append(random.nextInt(100) + 1);
|
|
|
|
|
} else {
|
|
|
|
|
question.append(" √").append(random.nextInt(100) + 1);
|
|
|
|
|
question.append(random.nextInt(100) + 1).append("²");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int extraOps = random.nextInt(2) + 1;
|
|
|
|
|
for (int i = 0; i < extraOps; i++) {
|
|
|
|
|
String operator = operators[random.nextInt(operators.length)];
|
|
|
|
|
int operand = random.nextInt(100) + 1;
|
|
|
|
|
|
|
|
|
|
if (random.nextDouble() < 0.3) {
|
|
|
|
|
if (random.nextBoolean()) {
|
|
|
|
|
question.append(" ").append(operator).append(" √").append(operand);
|
|
|
|
|
} else {
|
|
|
|
|
question.append(" ").append(operator).append(" ").append(operand).append("²");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
question.append(" ").append(operator).append(" ").append(operand);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return question.toString() + " = ";
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return "√1 + 1 = ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return question.toString() + " = ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isValidQuestion(String question) {
|
|
|
|
|
return (question.contains("²") || question.contains("√")) &&
|
|
|
|
|
!question.contains("sin") && !question.contains("cos") && !question.contains("tan");
|
|
|
|
|
try {
|
|
|
|
|
return question != null &&
|
|
|
|
|
question.contains("=") &&
|
|
|
|
|
(question.contains("²") || question.contains("√")) &&
|
|
|
|
|
!question.contains("sin") &&
|
|
|
|
|
!question.contains("cos") &&
|
|
|
|
|
!question.contains("tan");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SeniorQuestionGenerator implements IQuestionGenerator {
|
|
|
|
|
private Random random = new Random();
|
|
|
|
|
private String[] operators = {"+", "-", "*", "/"};
|
|
|
|
|
@ -125,50 +104,44 @@ class SeniorQuestionGenerator implements IQuestionGenerator {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String generateQuestion() {
|
|
|
|
|
StringBuilder question = new StringBuilder();
|
|
|
|
|
int operandCount = random.nextInt(3) + 2;
|
|
|
|
|
boolean hasTrigFunction = false;
|
|
|
|
|
|
|
|
|
|
if (random.nextDouble() < 0.6) {
|
|
|
|
|
try {
|
|
|
|
|
StringBuilder question = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
String trigFunc = trigFunctions[random.nextInt(trigFunctions.length)];
|
|
|
|
|
int angle = random.nextInt(360) + 1;
|
|
|
|
|
question.append(trigFunc).append("(").append(angle).append("°)");
|
|
|
|
|
hasTrigFunction = true;
|
|
|
|
|
} else {
|
|
|
|
|
question.append(random.nextInt(100) + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < operandCount; i++) {
|
|
|
|
|
String operator = operators[random.nextInt(operators.length)];
|
|
|
|
|
question.append(trigFunc).append("(").append(random.nextInt(360) + 1).append("°)");
|
|
|
|
|
|
|
|
|
|
if (!hasTrigFunction && random.nextDouble() < 0.5) {
|
|
|
|
|
String trigFunc = trigFunctions[random.nextInt(trigFunctions.length)];
|
|
|
|
|
int angle = random.nextInt(360) + 1;
|
|
|
|
|
question.append(" ").append(operator).append(" ").append(trigFunc).append("(").append(angle).append("°)");
|
|
|
|
|
hasTrigFunction = true;
|
|
|
|
|
} else {
|
|
|
|
|
int operand = random.nextInt(100) + 1;
|
|
|
|
|
question.append(" ").append(operator).append(" ").append(operand);
|
|
|
|
|
int extraOps = random.nextInt(2) + 1;
|
|
|
|
|
for (int i = 0; i < extraOps; i++) {
|
|
|
|
|
String operator = operators[random.nextInt(operators.length)];
|
|
|
|
|
|
|
|
|
|
if (random.nextDouble() < 0.4) {
|
|
|
|
|
trigFunc = trigFunctions[random.nextInt(trigFunctions.length)];
|
|
|
|
|
question.append(" ").append(operator).append(" ").append(trigFunc)
|
|
|
|
|
.append("(").append(random.nextInt(360) + 1).append("°)");
|
|
|
|
|
} else {
|
|
|
|
|
int operand = random.nextInt(100) + 1;
|
|
|
|
|
question.append(" ").append(operator).append(" ").append(operand);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return question.toString() + " = ";
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return "sin(45°) + 1 = ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!hasTrigFunction) {
|
|
|
|
|
String trigFunc = trigFunctions[random.nextInt(trigFunctions.length)];
|
|
|
|
|
int angle = random.nextInt(360) + 1;
|
|
|
|
|
question.append(" ").append(operators[random.nextInt(operators.length)])
|
|
|
|
|
.append(" ").append(trigFunc).append("(").append(angle).append("°)");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return question.toString() + " = ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isValidQuestion(String question) {
|
|
|
|
|
return question.contains("sin") || question.contains("cos") || question.contains("tan");
|
|
|
|
|
try {
|
|
|
|
|
return question != null &&
|
|
|
|
|
question.contains("=") &&
|
|
|
|
|
(question.contains("sin") || question.contains("cos") || question.contains("tan"));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class QuestionGeneratorFactory {
|
|
|
|
|
public static IQuestionGenerator createGenerator(UserType userType) {
|
|
|
|
|
switch (userType) {
|
|
|
|
|
|