11 #1

Merged
hnu202326010125 merged 3 commits from develop into main 5 months ago

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -0,0 +1,396 @@
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;
// 用户类
class User {
private String username;
private String password;
private String type;
public User(String username, String password, String type) {
this.username = username;
this.password = password;
this.type = type;
}
public String getUsername() { return username; }
public String getPassword() { return password; }
public String getType() { return type; }
}
// 抽象题目生成器
abstract class QuestionGenerator {
protected Random random = new Random();
public abstract String generateQuestion();
protected int generateNumber() {
return random.nextInt(100) + 1;
}
protected String generateOperator() {
String[] operators = {"+", "-", "*", "/"};
return operators[random.nextInt(operators.length)];
}
}
// 小学题目生成器
class PrimaryQuestionGenerator extends QuestionGenerator {
@Override
public String generateQuestion() {
int operandCount = random.nextInt(3) + 2;
StringBuilder question = new StringBuilder();
boolean hasParentheses = random.nextBoolean() && operandCount >= 3;
int parenthesesPosition = random.nextInt(operandCount - 1);
for (int i = 0; i < operandCount; i++) {
if (hasParentheses && i == parenthesesPosition) {
question.append("(");
}
question.append(generateNumber());
if (hasParentheses && i == parenthesesPosition + 1) {
question.append(")");
}
if (i < operandCount - 1) {
question.append(" ").append(generateOperator()).append(" ");
}
}
return question.toString();
}
}
// 初中题目生成器
class JuniorQuestionGenerator extends QuestionGenerator {
@Override
public String generateQuestion() {
int operandCount = random.nextInt(3) + 2;
StringBuilder question = new StringBuilder();
boolean hasSpecialOperator = false;
int specialOperatorPosition = random.nextInt(operandCount);
for (int i = 0; i < operandCount; i++) {
if (i == specialOperatorPosition && !hasSpecialOperator) {
if (random.nextBoolean()) {
question.append(generateNumber()).append("^2"); // 改为^2表示平方
hasSpecialOperator = true;
} else {
question.append("√").append(generateNumber());
hasSpecialOperator = true;
}
} else {
question.append(generateNumber());
}
if (i < operandCount - 1) {
question.append(" ").append(generateOperator()).append(" ");
}
}
if (!hasSpecialOperator) {
if (random.nextBoolean()) {
question.append(" ").append(generateOperator()).append(" ").append(generateNumber()).append("^2");
} else {
question.append(" ").append(generateOperator()).append(" ").append("√").append(generateNumber());
}
}
return question.toString();
}
}
// 高中题目生成器
class SeniorQuestionGenerator extends QuestionGenerator {
private final String[] trigFunctions = {"sin", "cos", "tan"};
@Override
public String generateQuestion() {
int operandCount = random.nextInt(3) + 2;
StringBuilder question = new StringBuilder();
boolean hasTrigFunction = false;
int trigPosition = random.nextInt(operandCount);
for (int i = 0; i < operandCount; i++) {
if (i == trigPosition && !hasTrigFunction) {
String trigFunction = trigFunctions[random.nextInt(trigFunctions.length)];
question.append(trigFunction).append("(").append(generateNumber()).append(")");
hasTrigFunction = true;
} else {
question.append(generateNumber());
}
if (i < operandCount - 1) {
question.append(" ").append(generateOperator()).append(" ");
}
}
if (!hasTrigFunction) {
String trigFunction = trigFunctions[random.nextInt(trigFunctions.length)];
question.append(" ").append(generateOperator()).append(" ").append(trigFunction).append("(").append(generateNumber()).append(")");
}
return question.toString();
}
}
// 用户认证管理
class AuthenticationManager {
private Map<String, User> users;
public AuthenticationManager() {
users = new HashMap<String, User>();
initializeUsers();
}
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", "高中"));
}
public User authenticate(String username, String password) {
User user = users.get(username);
if (user != null && user.getPassword().equals(password)) {
return user;
}
return null;
}
}
// 文件管理类
class FileManager {
private static final String BASE_DIR = "exams";
public FileManager() {
File baseDir = new File(BASE_DIR);
if (!baseDir.exists()) {
baseDir.mkdir();
}
}
public String generateFilename() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
return sdf.format(new Date()) + ".txt";
}
public String getUserDir(String username) {
return BASE_DIR + File.separator + username;
}
public void saveQuestions(String username, List<String> questions) throws IOException {
String userDir = getUserDir(username);
File dir = new File(userDir);
if (!dir.exists()) {
dir.mkdir();
}
String filename = generateFilename();
String filepath = userDir + File.separator + filename;
// 使用GBK编码保存文件
try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(filepath), "GBK"))) {
for (int i = 0; i < questions.size(); i++) {
writer.println((i + 1) + ". " + questions.get(i));
if (i < questions.size() - 1) {
writer.println();
}
}
}
System.out.println("题目已保存到: " + filepath);
}
public boolean checkDuplicate(String username, String question) {
String userDir = getUserDir(username);
File dir = new File(userDir);
if (!dir.exists()) {
return false;
}
File[] files = dir.listFiles();
if (files == null) {
return false;
}
for (File file : files) {
try (Scanner scanner = new Scanner(file, "GBK")) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.contains(question)) {
return true;
}
}
} catch (IOException e) {
// 忽略读取错误
}
}
return false;
}
}
// 主程序类
public class MathExamGenerator {
private AuthenticationManager authManager;
private FileManager fileManager;
private User currentUser;
private String currentType;
private Scanner scanner;
public MathExamGenerator() {
authManager = new AuthenticationManager();
fileManager = new FileManager();
scanner = new Scanner(System.in);
}
public void start() {
System.out.println("===== 中小学数学卷子自动生成程序 =====");
while (true) {
if (currentUser == null) {
login();
} else {
generateExam();
}
}
}
private void login() {
System.out.println("请输入用户名和密码(用空格隔开):");
String input = scanner.nextLine().trim();
if (input.equalsIgnoreCase("exit")) {
System.exit(0);
}
String[] parts = input.split(" ");
if (parts.length != 2) {
System.out.println("请输入正确的用户名、密码");
return;
}
String username = parts[0];
String password = parts[1];
currentUser = authManager.authenticate(username, password);
if (currentUser != null) {
currentType = currentUser.getType();
System.out.println("当前选择为" + currentType + "出题");
} else {
System.out.println("请输入正确的用户名、密码");
}
}
private void generateExam() {
System.out.println("准备生成" + currentType + "数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录:");
String input = scanner.nextLine().trim();
if (input.equals("-1")) {
currentUser = null;
return;
}
if (input.startsWith("切换为")) {
handleTypeSwitch(input);
return;
}
int questionCount;
try {
questionCount = Integer.parseInt(input);
} catch (NumberFormatException e) {
System.out.println("请输入有效的数字10-30或-1退出");
return;
}
if (questionCount != -1 && (questionCount < 10 || questionCount > 30)) {
System.out.println("题目数量应在10-30之间");
return;
}
List<String> questions = generateQuestions(questionCount);
try {
fileManager.saveQuestions(currentUser.getUsername(), questions);
} catch (IOException e) {
System.out.println("保存文件时出错: " + e.getMessage());
}
}
private void handleTypeSwitch(String input) {
Pattern pattern = Pattern.compile("切换为(小学|初中|高中)");
java.util.regex.Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
currentType = matcher.group(1);
System.out.println("准备生成" + currentType + "数学题目,请输入生成题目数量");
} else {
System.out.println("请输入小学、初中和高中三个选项中的一个");
}
}
private List<String> generateQuestions(int count) {
QuestionGenerator generator = getQuestionGenerator();
List<String> questions = new ArrayList<String>();
Set<String> generatedQuestions = new HashSet<String>();
int maxAttempts = count * 10;
int attempts = 0;
while (questions.size() < count && attempts < maxAttempts) {
String question = generator.generateQuestion();
if (!generatedQuestions.contains(question)) {
if (!fileManager.checkDuplicate(currentUser.getUsername(), question)) {
questions.add(question);
generatedQuestions.add(question);
}
}
attempts++;
}
if (questions.size() < count) {
System.out.println("警告:由于题目重复,只生成了 " + questions.size() + " 道题目");
}
return questions;
}
private QuestionGenerator getQuestionGenerator() {
if ("小学".equals(currentType)) {
return new PrimaryQuestionGenerator();
} else if ("初中".equals(currentType)) {
return new JuniorQuestionGenerator();
} else if ("高中".equals(currentType)) {
return new SeniorQuestionGenerator();
} else {
return new PrimaryQuestionGenerator();
}
}
public static void main(String[] args) {
MathExamGenerator generator = new MathExamGenerator();
generator.start();
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -0,0 +1,21 @@
1. 71 / 95 + 37 - 82
2. 64 / 83
3. 50 * 54 / 54
4. (27 - 93) - 95
5. 20 - (78 * 27) - 38
6. 37 * 75 + (50 + 30)
7. 23 / 1 * 78
8. 54 - 25 / 76
9. 90 * (79 / 42) + 41
10. 78 + 15 * 6 + 96
11. 74 + 63 - 97 - 34

@ -0,0 +1,21 @@
1. 87 + 84^2
2. 87 * 78 - 。フ10
3. 。フ14 * 25
4. 。フ37 / 4 / 25 / 19
5. 34 / 69^2 * 90
6. 56 * 45 + 81^2 * 99
7. 59 / 44 / 。フ70 / 47
8. 17^2 + 77 - 49
9. 。フ48 / 95 * 98
10. 47 / 20 + 49 / 。フ34
11. 62^2 + 5

@ -0,0 +1,21 @@
1. 40 / (46 * 2) * 92
2. 93 * 71
3. (56 / 61) / 64 + 10
4. (34 / 70) / 88
5. 18 - 96 - 53
6. 88 + 55 + 50
7. (42 * 21) * 64
8. 41 / 35
9. (15 - 29) + 85
10. 10 - 8 - 5 / 76
11. 72 - 1 * 58 + 92

@ -0,0 +1,21 @@
1. 71 * 100 * 。フ96 - 86
2. 50 * 86^2 * 71 * 15
3. 84 + 95^2 + 66 - 60
4. 23 / 32 + 21^2 - 31
5. 85 + 93 + 。フ30
6. 31 + 58 / 。フ96
7. 16 / 。フ13 - 87
8. 93 / 。フ92
9. 26 / 86 * 85 - 。フ33
10. 。フ83 / 95 + 53 * 30
11. 95^2 * 60

@ -0,0 +1,21 @@
1. 55 - 17 * sin(100) + 44
2. sin(97) - 23 / 44 + 40
3. 62 + tan(16) + 74 - 6
4. sin(53) - 31 * 58 / 75
5. 52 + cos(93)
6. 70 - tan(12) + 28
7. 55 + tan(7) * 80
8. 93 / cos(61)
9. 89 - tan(75)
10. 47 + 50 * 72 + sin(25)
11. tan(50) - 13
Loading…
Cancel
Save