|
|
|
|
@ -0,0 +1,275 @@
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
class Question {
|
|
|
|
|
private String content;
|
|
|
|
|
private String answer;
|
|
|
|
|
|
|
|
|
|
public Question(String content, String answer) {
|
|
|
|
|
this.content = content;
|
|
|
|
|
this.answer = answer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getContent() {
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getAnswer() {
|
|
|
|
|
return answer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
|
if (this == o) return true;
|
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
|
Question question = (Question) o;
|
|
|
|
|
return content.equals(question.content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int hashCode() {
|
|
|
|
|
return Objects.hash(content);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class User {
|
|
|
|
|
private String username;
|
|
|
|
|
private String password;
|
|
|
|
|
private String level; // 小学、初中、高中
|
|
|
|
|
|
|
|
|
|
public User(String username, String password, String level) {
|
|
|
|
|
this.username = username;
|
|
|
|
|
this.password = password;
|
|
|
|
|
this.level = level;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getUsername() {
|
|
|
|
|
return username;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getPassword() {
|
|
|
|
|
return password;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getLevel() {
|
|
|
|
|
return level;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setLevel(String level) {
|
|
|
|
|
this.level = level;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class MathTestGenerator {
|
|
|
|
|
private static List<User> users = new ArrayList<>();
|
|
|
|
|
private static User currentUser = null;
|
|
|
|
|
private static final String BASE_DIR = "math_questions/";
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
users.add(new User("张三1", "123", "小学"));
|
|
|
|
|
users.add(new User("张三2", "123", "小学"));
|
|
|
|
|
users.add(new User("张三3", "123", "小学"));
|
|
|
|
|
|
|
|
|
|
users.add(new User("李四1", "123", "初中"));
|
|
|
|
|
users.add(new User("李四2", "123", "初中"));
|
|
|
|
|
users.add(new User("李四3", "123", "初中"));
|
|
|
|
|
|
|
|
|
|
users.add(new User("王五1", "123", "高中"));
|
|
|
|
|
users.add(new User("王五2", "123", "高中"));
|
|
|
|
|
users.add(new User("王五3", "123", "高中"));
|
|
|
|
|
|
|
|
|
|
new File(BASE_DIR).mkdirs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Scanner scanner = new Scanner(System.in);
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
if (currentUser == null) {
|
|
|
|
|
System.out.print("请输入用户名和密码(用空格隔开):");
|
|
|
|
|
String input = scanner.nextLine().trim();
|
|
|
|
|
String[] parts = input.split(" ");
|
|
|
|
|
|
|
|
|
|
if (parts.length != 2) {
|
|
|
|
|
System.out.println("请输入正确的用户名、密码");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
User user = authenticate(parts[0], parts[1]);
|
|
|
|
|
if (user != null) {
|
|
|
|
|
currentUser = user;
|
|
|
|
|
System.out.println("当前选择为 " + currentUser.getLevel() + " 出题");
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("请输入正确的用户名、密码");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
System.out.print("准备生成 " + currentUser.getLevel() + " 数学题目,请输入生成题目数量(输入-1将退出当前用户,重新登录):");
|
|
|
|
|
String input = scanner.nextLine().trim();
|
|
|
|
|
|
|
|
|
|
if (input.startsWith("切换为")) {
|
|
|
|
|
String level = input.substring(3).trim();
|
|
|
|
|
if (level.equals("小学") || level.equals("初中") || level.equals("高中")) {
|
|
|
|
|
currentUser.setLevel(level);
|
|
|
|
|
System.out.println("系统提示:准备生成 " + currentUser.getLevel() + " 数学题目,请输入生成题目数量");
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("请输入小学、初中和高中三个选项中的一个");
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
int count = Integer.parseInt(input);
|
|
|
|
|
if (count == -1) {
|
|
|
|
|
currentUser = null;
|
|
|
|
|
System.out.println("已退出当前用户");
|
|
|
|
|
} else if (count >= 10 && count <= 30) {
|
|
|
|
|
generateQuestions(count);
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("题目数量必须在10-30之间,请重新输入");
|
|
|
|
|
}
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
System.out.println("请输入有效的数字");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static User authenticate(String username, String password) {
|
|
|
|
|
for (User user : users) {
|
|
|
|
|
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void generateQuestions(int count) {
|
|
|
|
|
String level = currentUser.getLevel();
|
|
|
|
|
Set<Question> allQuestions = new HashSet<>();
|
|
|
|
|
Set<Question> existingQuestions = loadExistingQuestions();
|
|
|
|
|
|
|
|
|
|
while (allQuestions.size() < count) {
|
|
|
|
|
Question question = generateQuestion(level);
|
|
|
|
|
if (!existingQuestions.contains(question) && !allQuestions.contains(question)) {
|
|
|
|
|
allQuestions.add(question);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveQuestions(allQuestions);
|
|
|
|
|
System.out.println("已生成" + count + "道" + level + "数学题目,并保存到文件");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Question generateQuestion(String level) {
|
|
|
|
|
Random random = new Random();
|
|
|
|
|
int operandCount = random.nextInt(5) + 1; // 1-5个操作数
|
|
|
|
|
List<Integer> operands = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < operandCount; i++) {
|
|
|
|
|
operands.add(random.nextInt(100) + 1); // 1-100的数字
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<Character> operators = new ArrayList<>();
|
|
|
|
|
if (level.equals("小学")) {
|
|
|
|
|
char[] ops = {'+', '-', '*', '/'};
|
|
|
|
|
for (int i = 0; i < operandCount - 1; i++) {
|
|
|
|
|
operators.add(ops[random.nextInt(4)]);
|
|
|
|
|
}
|
|
|
|
|
} else if (level.equals("初中")) {
|
|
|
|
|
char[] ops = {'+', '-', '*', '/', '^'};
|
|
|
|
|
for (int i = 0; i < operandCount - 1; i++) {
|
|
|
|
|
operators.add(ops[random.nextInt(5)]);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
char[] ops = {'+', '-', '*', '/', '^', 's', 'c', 'l'}; // s:sin, c:cos, l:log
|
|
|
|
|
for (int i = 0; i < operandCount - 1; i++) {
|
|
|
|
|
operators.add(ops[random.nextInt(8)]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuilder questionStr = new StringBuilder();
|
|
|
|
|
questionStr.append(operands.get(0));
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < operators.size(); i++) {
|
|
|
|
|
char op = operators.get(i);
|
|
|
|
|
switch (op) {
|
|
|
|
|
case '^':
|
|
|
|
|
questionStr.append("^").append(operands.get(i + 1));
|
|
|
|
|
break;
|
|
|
|
|
case 's':
|
|
|
|
|
questionStr.append("+sin(").append(operands.get(i + 1)).append(")");
|
|
|
|
|
break;
|
|
|
|
|
case 'c':
|
|
|
|
|
questionStr.append("+cos(").append(operands.get(i + 1)).append(")");
|
|
|
|
|
break;
|
|
|
|
|
case 'l':
|
|
|
|
|
questionStr.append("+log(").append(operands.get(i + 1)).append(")");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
questionStr.append(op).append(operands.get(i + 1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
questionStr.append(" = ?");
|
|
|
|
|
|
|
|
|
|
String answer = calculateAnswer(operands, operators, level);
|
|
|
|
|
|
|
|
|
|
return new Question(questionStr.toString(), answer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String calculateAnswer(List<Integer> operands, List<Character> operators, String level) {
|
|
|
|
|
return "计算结果";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Set<Question> loadExistingQuestions() {
|
|
|
|
|
Set<Question> questions = new HashSet<>();
|
|
|
|
|
String userDir = BASE_DIR + currentUser.getUsername() + "/";
|
|
|
|
|
File dir = new File(userDir);
|
|
|
|
|
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
|
return questions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File[] files = dir.listFiles((d, name) -> name.endsWith(".txt"));
|
|
|
|
|
if (files == null) {
|
|
|
|
|
return questions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (File file : files) {
|
|
|
|
|
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
|
|
|
|
String line;
|
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
|
line = line.trim();
|
|
|
|
|
if (line.matches("\\d+\\. .+ = \\?")) {
|
|
|
|
|
String content = line.substring(line.indexOf(". ") + 2);
|
|
|
|
|
questions.add(new Question(content, ""));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return questions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void saveQuestions(Set<Question> questions) {
|
|
|
|
|
String userDir = BASE_DIR + currentUser.getUsername() + "/";
|
|
|
|
|
new File(userDir).mkdirs();
|
|
|
|
|
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
|
|
|
|
|
String fileName = userDir + sdf.format(new Date()) + ".txt";
|
|
|
|
|
|
|
|
|
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
|
|
|
|
|
List<Question> questionList = new ArrayList<>(questions);
|
|
|
|
|
for (int i = 0; i < questionList.size(); i++) {
|
|
|
|
|
bw.write((i + 1) + ". " + questionList.get(i).getContent());
|
|
|
|
|
bw.newLine();
|
|
|
|
|
if (i != questionList.size() - 1) {
|
|
|
|
|
bw.newLine();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|