@ -0,0 +1,55 @@
|
||||
// AbstractQuestionSetting.java
|
||||
import java.util.Random;
|
||||
|
||||
public abstract class AbstractQuestionSetting implements QuestionSetting {
|
||||
protected static final Random RANDOM = new Random();
|
||||
|
||||
public String getRandomOperator() {
|
||||
String[] operators = {"+", "-", "*", "/"};
|
||||
int index = RANDOM.nextInt(4);
|
||||
return operators[index];
|
||||
}
|
||||
|
||||
public String getRandomNumber() {
|
||||
int number = RANDOM.nextInt(1, 100);
|
||||
return String.valueOf(number);
|
||||
}
|
||||
|
||||
public int getPriority(String operator) {
|
||||
if (operator == null) {
|
||||
return -1;
|
||||
}
|
||||
if (operator.equals("²") || operator.equals("√")
|
||||
|| operator.equals("sin") || operator.equals("cos") || operator.equals("tan")) {
|
||||
return 3;
|
||||
}
|
||||
if (operator.equals("+") || operator.equals("-")) {
|
||||
return 1;
|
||||
}
|
||||
if (operator.equals("*") || operator.equals("/")) {
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String addParenthesesIfNeeded(Expression child, String parentOperator, boolean isRightChild) {
|
||||
if (child.getMainOperator() == null) {
|
||||
return child.getExpression();
|
||||
}
|
||||
|
||||
int parentPriority = getPriority(parentOperator);
|
||||
int childPriority = getPriority(child.getMainOperator());
|
||||
|
||||
if (childPriority < parentPriority) {
|
||||
return "(" + child.getExpression() + ")";
|
||||
}
|
||||
|
||||
if (isRightChild && (parentOperator.equals("-") || parentOperator.equals("/"))) {
|
||||
if (parentPriority == childPriority) {
|
||||
return "(" + child.getExpression() + ")";
|
||||
}
|
||||
}
|
||||
|
||||
return child.getExpression();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
// Expression.java
|
||||
public class Expression {
|
||||
private String expression;
|
||||
private int value;
|
||||
private String mainOperator;
|
||||
|
||||
public Expression(String expression, int value, String mainOperator) {
|
||||
this.expression = expression;
|
||||
this.value = value;
|
||||
this.mainOperator = mainOperator;
|
||||
}
|
||||
|
||||
public String getExpression() {
|
||||
return expression;
|
||||
}
|
||||
|
||||
public void setExpression(String expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getMainOperator() {
|
||||
return mainOperator;
|
||||
}
|
||||
|
||||
public void setMainOperator(String mainOperator) {
|
||||
this.mainOperator = mainOperator;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
// FileManager.java
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
|
||||
public class FileManager {
|
||||
public static final String ROOT_PATH = "./试卷/";
|
||||
|
||||
public void savePaper(User user, List<String> questions) throws IOException {
|
||||
String userDirectory = ROOT_PATH + user.getUsername();
|
||||
Path userPath = Paths.get(userDirectory);
|
||||
if (!Files.exists(userPath)) {
|
||||
Files.createDirectories(userPath);
|
||||
}
|
||||
|
||||
String fileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss")) + ".txt";
|
||||
Path filePath = userPath.resolve(fileName);
|
||||
try (PrintWriter writer = new PrintWriter(new FileWriter(filePath.toFile(), StandardCharsets.UTF_8))) {
|
||||
for (int index = 0; index < questions.size(); index++) {
|
||||
writer.printf("%d. %s%n%n", index + 1, questions.get(index));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean checkQuestion(String question, User user) throws IOException {
|
||||
String userDirectory = ROOT_PATH + user.getUsername();
|
||||
Path userPath = Paths.get(userDirectory);
|
||||
if (!Files.exists(userPath)) {
|
||||
return true;
|
||||
} else {
|
||||
File directory = new File("./试卷/" + user.getUsername());
|
||||
File[] files = directory.listFiles();
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
for (String line : Files.readAllLines(file.toPath())) {
|
||||
if (!line.isEmpty()) {
|
||||
line = line.replaceFirst("^\\d+\\.\\s*", "");
|
||||
if (line.equals(question)) {
|
||||
System.out.println("有道一样的题");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,123 @@
|
||||
// HighQuestionSetting.java
|
||||
public class HighQuestionSetting extends AbstractQuestionSetting {
|
||||
|
||||
@Override
|
||||
public String addParenthesesIfNeeded(Expression child, String parentOperator, boolean isRightChild) {
|
||||
if (child.getMainOperator() == null
|
||||
|| child.getMainOperator().equals("²") || child.getMainOperator().equals("√")
|
||||
|| child.getMainOperator().equals("sin") || child.getMainOperator().equals("cos")
|
||||
|| child.getMainOperator().equals("tan")) {
|
||||
return child.getExpression();
|
||||
}
|
||||
|
||||
int parentPriority = getPriority(parentOperator);
|
||||
int childPriority = getPriority(child.getMainOperator());
|
||||
|
||||
if (childPriority < parentPriority) {
|
||||
return "(" + child.getExpression() + ")";
|
||||
}
|
||||
|
||||
if (isRightChild && (parentOperator.equals("-") || parentOperator.equals("/"))) {
|
||||
if (parentPriority == childPriority) {
|
||||
return "(" + child.getExpression() + ")";
|
||||
}
|
||||
}
|
||||
|
||||
return child.getExpression();
|
||||
}
|
||||
|
||||
public Expression applyUnaryOperator(Expression child, String operator) {
|
||||
switch (operator) {
|
||||
case "²":
|
||||
if (child.getMainOperator() == null) {
|
||||
return new Expression(child.getExpression() + "²", child.getValue() * child.getValue(), "²");
|
||||
}
|
||||
return new Expression("(" + child.getExpression() + ")²", child.getValue() * child.getValue(), "²");
|
||||
case "√":
|
||||
if (child.getValue() < 0) {
|
||||
String numberString = getRandomNumber();
|
||||
child = new Expression(numberString, Integer.parseInt(numberString), null);
|
||||
}
|
||||
if (child.getMainOperator() == null) {
|
||||
return new Expression("√" + child.getExpression(), (int) Math.sqrt(child.getValue()), "√");
|
||||
}
|
||||
return new Expression("√(" + child.getExpression() + ")", (int) Math.sqrt(child.getValue()), "√");
|
||||
case "sin":
|
||||
return new Expression("sin(" + child.getExpression() + ")",
|
||||
(int) Math.round(Math.sin(Math.toRadians(child.getValue()))), "sin");
|
||||
case "cos":
|
||||
return new Expression("cos(" + child.getExpression() + ")",
|
||||
(int) Math.round(Math.cos(Math.toRadians(child.getValue()))), "cos");
|
||||
case "tan":
|
||||
while (child.getValue() % 180 == 90) {
|
||||
String numberString = getRandomNumber();
|
||||
child = new Expression(numberString, Integer.parseInt(numberString), null);
|
||||
}
|
||||
return new Expression("tan(" + child.getExpression() + ")",
|
||||
(int) Math.round(Math.tan(Math.toRadians(child.getValue()))), "tan");
|
||||
default:
|
||||
return child;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression setQuestion(int count) {
|
||||
Expression result = generateFirstQuestion(count);
|
||||
while (!result.getExpression().contains("sin") && !result.getExpression().contains("cos")
|
||||
&& !result.getExpression().contains("tan")) {
|
||||
result = generateFirstQuestion(count);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Expression applyProbability(Expression result) {
|
||||
if (RANDOM.nextDouble() < 0.3) {
|
||||
String[] unaryOperators = {"²", "√", "sin", "cos", "tan"};
|
||||
String unaryOperator = unaryOperators[RANDOM.nextInt(unaryOperators.length)];
|
||||
result = applyUnaryOperator(result, unaryOperator);
|
||||
result.setMainOperator(unaryOperator);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Expression generateFirstQuestion(int count) {
|
||||
if (count == 1) {
|
||||
String numberString = getRandomNumber();
|
||||
int number = Integer.parseInt(numberString);
|
||||
Expression expression = new Expression(numberString, number, null);
|
||||
expression = applyProbability(expression);
|
||||
return expression;
|
||||
}
|
||||
int leftCount = 1 + RANDOM.nextInt(count - 1);
|
||||
int rightCount = count - leftCount;
|
||||
Expression left = generateFirstQuestion(leftCount);
|
||||
Expression right = generateFirstQuestion(rightCount);
|
||||
String operator = getRandomOperator();
|
||||
int value = 0;
|
||||
switch (operator) {
|
||||
case "+":
|
||||
value = left.getValue() + right.getValue();
|
||||
break;
|
||||
case "-":
|
||||
value = left.getValue() - right.getValue();
|
||||
break;
|
||||
case "*":
|
||||
value = left.getValue() * right.getValue();
|
||||
break;
|
||||
case "/":
|
||||
if (right.getValue() == 0) {
|
||||
return generateFirstQuestion(rightCount);
|
||||
}
|
||||
value = left.getValue() / right.getValue();
|
||||
break;
|
||||
}
|
||||
|
||||
String leftExpression = addParenthesesIfNeeded(left, operator, false);
|
||||
String rightExpression = addParenthesesIfNeeded(right, operator, true);
|
||||
|
||||
Expression result = new Expression(leftExpression + " " + operator + " " + rightExpression, (int) value, operator);
|
||||
|
||||
result = applyProbability(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
import java.io.IOException;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
AllSystem allSystem = new AllSystem();
|
||||
try {
|
||||
allSystem.showHomeMenu();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,106 @@
|
||||
// MiddleQuestionSetting.java
|
||||
public class MiddleQuestionSetting extends AbstractQuestionSetting {
|
||||
|
||||
public Expression applyUnaryOperator(Expression child, String operator) {
|
||||
switch (operator) {
|
||||
case "²":
|
||||
if (child.getMainOperator() == null) {
|
||||
return new Expression(child.getExpression() + "²", child.getValue() * child.getValue(), "²");
|
||||
}
|
||||
return new Expression("(" + child.getExpression() + ")²", child.getValue() * child.getValue(), "²");
|
||||
case "√":
|
||||
if (child.getValue() < 0) {
|
||||
String numberString = getRandomNumber();
|
||||
child = new Expression(numberString, Integer.parseInt(numberString), null);
|
||||
}
|
||||
if (child.getMainOperator() == null) {
|
||||
return new Expression("√" + child.getExpression(), child.getValue() * child.getValue(), "√");
|
||||
}
|
||||
return new Expression("√(" + child.getExpression() + ")", (int) Math.sqrt(child.getValue()), "√");
|
||||
default:
|
||||
return child;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String addParenthesesIfNeeded(Expression child, String parentOperator, boolean isRightChild) {
|
||||
if (child.getMainOperator() == null || child.getMainOperator().equals("²") || child.getMainOperator().equals("√")) {
|
||||
return child.getExpression();
|
||||
}
|
||||
|
||||
int parentPriority = getPriority(parentOperator);
|
||||
int childPriority = getPriority(child.getMainOperator());
|
||||
|
||||
if (childPriority < parentPriority) {
|
||||
return "(" + child.getExpression() + ")";
|
||||
}
|
||||
|
||||
if (isRightChild && (parentOperator.equals("-") || parentOperator.equals("/"))) {
|
||||
if (parentPriority == childPriority) {
|
||||
return "(" + child.getExpression() + ")";
|
||||
}
|
||||
}
|
||||
|
||||
return child.getExpression();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression setQuestion(int count) {
|
||||
Expression result = generateFirstQuestion(count);
|
||||
while (!result.getExpression().contains("²") && !result.getExpression().contains("√")) {
|
||||
result = generateFirstQuestion(count);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Expression generateFirstQuestion(int count) {
|
||||
if (count == 1) {
|
||||
String numberString = getRandomNumber();
|
||||
int number = Integer.parseInt(numberString);
|
||||
Expression expression = new Expression(numberString, number, null);
|
||||
expression = applyProbability(expression);
|
||||
return expression;
|
||||
}
|
||||
int leftCount = 1 + RANDOM.nextInt(count - 1);
|
||||
int rightCount = count - leftCount;
|
||||
Expression left = generateFirstQuestion(leftCount);
|
||||
Expression right = generateFirstQuestion(rightCount);
|
||||
String operator = getRandomOperator();
|
||||
int value = 0;
|
||||
switch (operator) {
|
||||
case "+":
|
||||
value = left.getValue() + right.getValue();
|
||||
break;
|
||||
case "-":
|
||||
value = left.getValue() - right.getValue();
|
||||
break;
|
||||
case "*":
|
||||
value = left.getValue() * right.getValue();
|
||||
break;
|
||||
case "/":
|
||||
while (right.getValue() == 0) {
|
||||
right = generateFirstQuestion(rightCount);
|
||||
}
|
||||
value = left.getValue() / right.getValue();
|
||||
break;
|
||||
}
|
||||
|
||||
String leftExpression = addParenthesesIfNeeded(left, operator, false);
|
||||
String rightExpression = addParenthesesIfNeeded(right, operator, true);
|
||||
|
||||
Expression result = new Expression(leftExpression + " " + operator + " " + rightExpression, value, operator);
|
||||
|
||||
result = applyProbability(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Expression applyProbability(Expression result) {
|
||||
if (RANDOM.nextDouble() < 0.3) {
|
||||
String[] unaryOperators = {"²", "√"};
|
||||
String unaryOperator = unaryOperators[RANDOM.nextInt(unaryOperators.length)];
|
||||
result = applyUnaryOperator(result, unaryOperator);
|
||||
result.setMainOperator(unaryOperator);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
// PrimaryQuestionSetting.java
|
||||
public class PrimaryQuestionSetting extends AbstractQuestionSetting {
|
||||
|
||||
@Override
|
||||
public Expression setQuestion(int count) {
|
||||
if (count == 1) {
|
||||
String expression = getRandomNumber();
|
||||
return new Expression(expression, Integer.parseInt(expression), null);
|
||||
}
|
||||
int leftCount = 1 + RANDOM.nextInt(count - 1);
|
||||
int rightCount = count - leftCount;
|
||||
Expression left = setQuestion(leftCount);
|
||||
Expression right = setQuestion(rightCount);
|
||||
String operator = getRandomOperator();
|
||||
while (operator.equals("/") && right.getValue() == 0) {
|
||||
right = setQuestion(rightCount);
|
||||
}
|
||||
if (operator.equals("-") && left.getValue() < right.getValue()) {
|
||||
Expression temporary = left;
|
||||
left = right;
|
||||
right = temporary;
|
||||
}
|
||||
|
||||
String leftExpression = addParenthesesIfNeeded(left, operator, false);
|
||||
String rightExpression = addParenthesesIfNeeded(right, operator, true);
|
||||
|
||||
int value = switch (operator) {
|
||||
case "+" -> left.getValue() + right.getValue();
|
||||
case "-" -> left.getValue() - right.getValue();
|
||||
case "*" -> left.getValue() * right.getValue();
|
||||
case "/" -> left.getValue() / right.getValue();
|
||||
default -> 0;
|
||||
};
|
||||
|
||||
return new Expression(leftExpression + " " + operator + " " + rightExpression, value, operator);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
public interface QuestionSetting {
|
||||
|
||||
Expression setQuestion(int count);
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
public class QuestionSettingFactory {
|
||||
|
||||
public QuestionSetting getQuestionSetting(String type) {
|
||||
switch (type) {
|
||||
case "小学" -> {
|
||||
return new PrimaryQuestionSetting();
|
||||
}
|
||||
case "初中" -> {
|
||||
return new MiddleQuestionSetting();
|
||||
}
|
||||
case "高中" -> {
|
||||
return new HighQuestionSetting();
|
||||
}
|
||||
default -> {
|
||||
System.out.println("类型错误");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
public class User {
|
||||
|
||||
private String username; // 用户名
|
||||
private String password; // 密码
|
||||
private String userType; //(小学/初中/高中)
|
||||
|
||||
public User(String username, String password, String userType) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.userType = userType;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getUserType() {
|
||||
return userType;
|
||||
}
|
||||
|
||||
public void setUserType(String userType) {
|
||||
this.userType = userType;
|
||||
}
|
||||
|
||||
public boolean validateCredentials(String inputUsername, String inputPassword) {
|
||||
// 验证用户名和密码是否匹配
|
||||
return this.username.equals(inputUsername) && this.password.equals(inputPassword);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,63 @@
|
||||
// UserManager.java
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class UserManager {
|
||||
private Map<String, User> users = new HashMap<>();
|
||||
private User currentUser;
|
||||
private String currentType;
|
||||
|
||||
public UserManager() {
|
||||
initializeUsers();
|
||||
}
|
||||
|
||||
public boolean login(String username, String password) {
|
||||
if (users.containsKey(username)) {
|
||||
User user = users.get(username);
|
||||
if (user.validateCredentials(username, password)) {
|
||||
currentUser = user;
|
||||
currentType = user.getUserType();
|
||||
System.out.println("登录成功");
|
||||
return true;
|
||||
} else {
|
||||
System.out.println("密码错误,请重试");
|
||||
}
|
||||
} else {
|
||||
System.out.println("该账号不存在,请重试");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void logout() {
|
||||
currentUser = null;
|
||||
currentType = null;
|
||||
}
|
||||
|
||||
public boolean switchUserType(String newType) {
|
||||
if (currentUser != null) {
|
||||
currentType = newType;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isLoggedIn() {
|
||||
return currentUser != null;
|
||||
}
|
||||
|
||||
public User getCurrentUser() {
|
||||
return currentUser;
|
||||
}
|
||||
|
||||
private void initializeUsers() {
|
||||
users.put("张三1", new User("张三1", "123", "小学"));
|
||||
users.put("张三2", new User("张三2", "123", "小学"));
|
||||
users.put("张三3", new User("张三3", "123", "小学"));
|
||||
users.put("李四1", new User("李四1", "123", "初中"));
|
||||
users.put("李四2", new User("李四2", "123", "初中"));
|
||||
users.put("李四3", new User("李四3", "123", "初中"));
|
||||
users.put("王五1", new User("王五1", "123", "高中"));
|
||||
users.put("王五2", new User("王五2", "123", "高中"));
|
||||
users.put("王五3", new User("王五3", "123", "高中"));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue