Binary file not shown.
@ -0,0 +1,40 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class AccountManager {
|
||||
private Map<String, User> users;
|
||||
|
||||
public AccountManager() {
|
||||
initializeUsers();
|
||||
}
|
||||
|
||||
private void initializeUsers() {
|
||||
users = new HashMap<>();
|
||||
|
||||
addUser("张三1", "123", UserType.PRIMARY);
|
||||
addUser("张三2", "123", UserType.PRIMARY);
|
||||
addUser("张三3", "123", UserType.PRIMARY);
|
||||
|
||||
addUser("李四1", "123", UserType.JUNIOR);
|
||||
addUser("李四2", "123", UserType.JUNIOR);
|
||||
addUser("李四3", "123", UserType.JUNIOR);
|
||||
|
||||
addUser("王五1", "123", UserType.SENIOR);
|
||||
addUser("王五2", "123", UserType.SENIOR);
|
||||
addUser("王五3", "123", UserType.SENIOR);
|
||||
}
|
||||
|
||||
private void addUser(String username, String password, UserType type) {
|
||||
users.put(username + ":" + password, new User(username, password, type));
|
||||
}
|
||||
|
||||
public User authenticate(String username, String password) {
|
||||
String key = username + ":" + password;
|
||||
return users.get(key);
|
||||
}
|
||||
|
||||
public boolean isValidUserType(String typeName) {
|
||||
return UserType.fromChineseName(typeName) != null;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@ -0,0 +1,91 @@
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
public class Filemanager {
|
||||
private static final String BASE_DIR = "数学试卷";
|
||||
|
||||
public Filemanager() {
|
||||
createBaseDirectory();
|
||||
}
|
||||
|
||||
private void createBaseDirectory() {
|
||||
File baseDir = new File(BASE_DIR);
|
||||
if (!baseDir.exists()) {
|
||||
baseDir.mkdir();
|
||||
}
|
||||
}
|
||||
|
||||
public void savePaper(String username, String[] questions) throws IOException {
|
||||
String userDirPath = BASE_DIR + File.separator + username;
|
||||
File userDir = new File(userDirPath);
|
||||
if (!userDir.exists()) {
|
||||
userDir.mkdir();
|
||||
}
|
||||
|
||||
String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
|
||||
String fileName = timestamp + ".txt";
|
||||
String filePath = userDirPath + File.separator + fileName;
|
||||
|
||||
try (PrintWriter writer = new PrintWriter(new FileWriter(filePath))) {
|
||||
for (int i = 0; i < questions.length; i++) {
|
||||
writer.println(questions[i]);
|
||||
if (i < questions.length - 1) {
|
||||
writer.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("试卷已保存到: " + new File(filePath).getAbsolutePath());
|
||||
}
|
||||
|
||||
public Set<String> loadAllQuestions(String username) {
|
||||
Set<String> allQuestions = new HashSet<>();
|
||||
String userDirPath = BASE_DIR + File.separator + username;
|
||||
File userDir = new File(userDirPath);
|
||||
|
||||
if (!userDir.exists() || !userDir.isDirectory()) {
|
||||
return allQuestions;
|
||||
}
|
||||
|
||||
File[] files = userDir.listFiles((dir, name) -> name.endsWith(".txt"));
|
||||
if (files == null) {
|
||||
return allQuestions;
|
||||
}
|
||||
|
||||
for (File file : files) {
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||
String line;
|
||||
StringBuilder question = new StringBuilder();
|
||||
boolean inQuestion = false;
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (line.matches("^\\d+\\.\\s.*")) {
|
||||
if (inQuestion) {
|
||||
allQuestions.add(question.toString().trim());
|
||||
question.setLength(0);
|
||||
}
|
||||
String questionContent = line.substring(line.indexOf(". ") + 2);
|
||||
question.append(questionContent);
|
||||
inQuestion = true;
|
||||
} else if (inQuestion && !line.trim().isEmpty()) {
|
||||
question.append(" ").append(line.trim());
|
||||
}
|
||||
}
|
||||
|
||||
if (inQuestion && question.length() > 0) {
|
||||
allQuestions.add(question.toString().trim());
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("读取文件失败: " + file.getName());
|
||||
}
|
||||
}
|
||||
|
||||
return allQuestions;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,48 @@
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
public class PaperGenerator {
|
||||
private IQuestionGenerator questionGenerator;
|
||||
private Set<String> generatedQuestions;
|
||||
private User currentUser;
|
||||
|
||||
public PaperGenerator(User user) {
|
||||
this.currentUser = user;
|
||||
this.questionGenerator = QuestionGeneratorFactory.createGenerator(user.getUserType());
|
||||
this.generatedQuestions = new HashSet<>();
|
||||
}
|
||||
|
||||
public void setUserType(UserType userType) {
|
||||
this.questionGenerator = QuestionGeneratorFactory.createGenerator(userType);
|
||||
}
|
||||
|
||||
public String[] generatePaper(int questionCount, Filemanager fileManager) {
|
||||
if (questionCount < 10 || questionCount > 30) {
|
||||
throw new IllegalArgumentException("题目数量必须在10-30之间");
|
||||
}
|
||||
|
||||
String[] questions = new String[questionCount];
|
||||
Set<String> allHistoryQuestions = fileManager.loadAllQuestions(currentUser.getUsername());
|
||||
generatedQuestions.addAll(allHistoryQuestions);
|
||||
|
||||
for (int i = 0; i < questionCount; i++) {
|
||||
String question;
|
||||
int attempt = 0;
|
||||
int maxAttempts = 100;
|
||||
|
||||
do {
|
||||
question = questionGenerator.generateQuestion();
|
||||
attempt++;
|
||||
if (attempt >= maxAttempts) {
|
||||
throw new RuntimeException("无法生成不重复的题目,请尝试清理历史文件");
|
||||
}
|
||||
} while (generatedQuestions.contains(question) || !questionGenerator.isValidQuestion(question));
|
||||
|
||||
generatedQuestions.add(question);
|
||||
questions[i] = (i + 1) + ". " + question;
|
||||
}
|
||||
|
||||
return questions;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@ -0,0 +1,185 @@
|
||||
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;
|
||||
|
||||
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(" ").append(operator).append(" ").append(operand);
|
||||
}
|
||||
}
|
||||
|
||||
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++;
|
||||
}
|
||||
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)];
|
||||
}
|
||||
|
||||
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("²");
|
||||
} else {
|
||||
question.append(" √").append(random.nextInt(100) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return question.toString() + " = ";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValidQuestion(String question) {
|
||||
return (question.contains("²") || question.contains("√")) &&
|
||||
!question.contains("sin") && !question.contains("cos") && !question.contains("tan");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class SeniorQuestionGenerator implements IQuestionGenerator {
|
||||
private Random random = new Random();
|
||||
private String[] operators = {"+", "-", "*", "/"};
|
||||
private String[] trigFunctions = {"sin", "cos", "tan"};
|
||||
|
||||
@Override
|
||||
public String generateQuestion() {
|
||||
StringBuilder question = new StringBuilder();
|
||||
int operandCount = random.nextInt(3) + 2;
|
||||
boolean hasTrigFunction = false;
|
||||
|
||||
if (random.nextDouble() < 0.6) {
|
||||
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)];
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class QuestionGeneratorFactory {
|
||||
public static IQuestionGenerator createGenerator(UserType userType) {
|
||||
switch (userType) {
|
||||
case PRIMARY:
|
||||
return new PrimaryQuestionGenerator();
|
||||
case JUNIOR:
|
||||
return new JuniorQuestionGenerator();
|
||||
case SENIOR:
|
||||
return new SeniorQuestionGenerator();
|
||||
default:
|
||||
throw new IllegalArgumentException("不支持的题目类型");
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,55 @@
|
||||
|
||||
public class User {
|
||||
private String username;
|
||||
private String password;
|
||||
private UserType userType;
|
||||
|
||||
public User(String username, String password, UserType userType) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.userType = userType;
|
||||
}
|
||||
|
||||
public String getUsername() { return username; }
|
||||
public String getPassword() { return password; }
|
||||
public UserType getUserType() { return userType; }
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null || getClass() != obj.getClass()) return false;
|
||||
User user = (User) obj;
|
||||
return username.equals(user.username) && password.equals(user.password);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return username.hashCode() + password.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
enum UserType {
|
||||
PRIMARY("小学"),
|
||||
JUNIOR("初中"),
|
||||
SENIOR("高中");
|
||||
|
||||
private final String chineseName;
|
||||
|
||||
UserType(String chineseName) {
|
||||
this.chineseName = chineseName;
|
||||
}
|
||||
|
||||
public String getChineseName() {
|
||||
return chineseName;
|
||||
}
|
||||
|
||||
public static UserType fromChineseName(String name) {
|
||||
for (UserType type : values()) {
|
||||
if (type.chineseName.equals(name)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Loading…
Reference in new issue