luoyuehang 7 months ago committed by Gitea
parent ec08b3bfaf
commit 15b3309eb7

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. 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

@ -1,454 +0,0 @@
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <filesystem>
#include <cmath>
using namespace std;
// 用户账户结构
class User {
public:
string username;
string password;
string userType; // "小学", "初中", "高中"
};
// 题目生成接口
class QuestionGenerator {
public:
virtual string generateQuestion() = 0;
virtual bool meetsRequirements(const string& question) = 0;
virtual ~QuestionGenerator() {}
};
// 小学题目生成器
class PrimaryQuestionGenerator : public QuestionGenerator {
public:
string generateQuestion() override;
bool meetsRequirements(const string& question) override;
};
// 初中题目生成器
class JuniorQuestionGenerator : public QuestionGenerator {
public:
string generateQuestion() override;
bool meetsRequirements(const string& question) override;
};
// 高中题目生成器
class SeniorQuestionGenerator : public QuestionGenerator {
public:
string generateQuestion() override;
bool meetsRequirements(const string& question) override;
};
// 文件管理器类
class FileManager {
public:
static bool createUserFolder(const string& username);
static string generateFileName(const string& username);
static void saveQuestionsToFile(const string& filename, const vector<string>& questions);
static set<string> loadExistingQuestions(const string& username);
};
// 用户认证类
class UserAuthenticator {
private:
vector<User> presetUsers;
public:
UserAuthenticator();
bool authenticate(const string& username, const string& password, User& user);
};
// 应用程序主类
class MathTestGeneratorApp {
private:
User currentUser;
bool isLoggedIn;
set<string> generatedQuestions;
map<string, QuestionGenerator*> generators;
QuestionGenerator* currentGenerator;
public:
MathTestGeneratorApp();
~MathTestGeneratorApp();
void run();
void login();
void generatePaper();
void switchUserType(const string& newType);
bool isQuestionDuplicate(const string& question);
void addQuestionToHistory(const string& question);
string getCurrentTimeString();
};
// 预设账户
UserAuthenticator::UserAuthenticator() {
presetUsers = {
{"张三1", "123", "小学"},
{"张三2", "123", "小学"},
{"张三3", "123", "小学"},
{"李四1", "123", "初中"},
{"李四2", "123", "初中"},
{"李四3", "123", "初中"},
{"王五1", "123", "高中"},
{"王五2", "123", "高中"},
{"王五3", "123", "高中"}
};
}
bool UserAuthenticator::authenticate(const string& username, const string& password, User& user) {
for (const auto& u : presetUsers) {
if (u.username == username && u.password == password) {
user = u;
return true;
}
}
return false;
}
// 小学题目生成实现
string PrimaryQuestionGenerator::generateQuestion() {
int numOperands = rand() % 4 + 2; // 2-5个操作数
stringstream question;
vector<string> operators = {"+", "-", "*", "/"};
bool hasParentheses = (rand() % 2 == 0);
for (int i = 0; i < numOperands; i++) {
int operand = rand() % 100 + 1;
if (i == 0) {
if (hasParentheses && numOperands > 2 && rand() % 2 == 0) {
question << "(";
}
question << operand;
} else {
string op = operators[rand() % operators.size()];
question << " " << op << " " << operand;
if (hasParentheses && i == 1 && rand() % 2 == 0) {
question << ")";
}
}
}
question << " = ";
return question.str();
}
bool PrimaryQuestionGenerator::meetsRequirements(const string& question) {
// 小学题目只能包含 + - * / 和括号
for (char c : question) {
if (isalpha(c) && c != 's' && c != 'i' && c != 'n' && c != 'c' && c != 'o' && c != 't' && c != 'a') {
// 简单检查,实际需要更复杂的逻辑
continue;
}
}
return true;
}
// 初中题目生成实现
string JuniorQuestionGenerator::generateQuestion() {
int numOperands = rand() % 4 + 2; // 2-5个操作数
stringstream question;
vector<string> operators = {"+", "-", "*", "/", "²", ""};
bool hasSquareOrRoot = false;
for (int i = 0; i < numOperands; i++) {
int operand = rand() % 100 + 1;
if (i == 0) {
question << operand;
} else {
string op = operators[rand() % operators.size()];
if (op == "²") {
question << "² " << operators[rand() % 4] << " " << operand;
hasSquareOrRoot = true;
} else if (op == "") {
question << "" << operand << " " << operators[rand() % 4] << " " << operand;
hasSquareOrRoot = true;
} else {
question << " " << op << " " << operand;
}
}
}
// 确保至少有一个平方或开根号
if (!hasSquareOrRoot) {
int pos = rand() % numOperands;
// 简化处理,在实际应用中需要更复杂的逻辑
string temp = question.str();
size_t found = temp.find_last_of(" ");
if (found != string::npos) {
question.str("");
question << temp.substr(0, found) << "²" << temp.substr(found);
}
}
question << " = ";
return question.str();
}
bool JuniorQuestionGenerator::meetsRequirements(const string& question) {
// 检查是否包含平方或开根号
return question.find("²") != string::npos || question.find("") != string::npos;
}
// 高中题目生成实现
string SeniorQuestionGenerator::generateQuestion() {
int numOperands = rand() % 4 + 2; // 2-5个操作数
stringstream question;
vector<string> operators = {"+", "-", "*", "/", "sin", "cos", "tan"};
bool hasTrigonometry = false;
for (int i = 0; i < numOperands; i++) {
int operand = rand() % 100 + 1;
if (i == 0) {
if (rand() % 2 == 0) {
string trig = operators[4 + rand() % 3]; // sin, cos, tan
question << trig << "(" << operand << ")";
hasTrigonometry = true;
} else {
question << operand;
}
} else {
string op = operators[rand() % operators.size()];
if (op == "sin" || op == "cos" || op == "tan") {
question << " " << op << "(" << operand << ")";
hasTrigonometry = true;
} else {
question << " " << op << " " << operand;
}
}
}
// 确保至少有一个三角函数
if (!hasTrigonometry) {
string trig = operators[4 + rand() % 3];
int operand = rand() % 100 + 1;
question << " " << trig << "(" << operand << ")";
}
question << " = ";
return question.str();
}
bool SeniorQuestionGenerator::meetsRequirements(const string& question) {
// 检查是否包含三角函数
return question.find("sin") != string::npos ||
question.find("cos") != string::npos ||
question.find("tan") != string::npos;
}
// 文件管理实现
bool FileManager::createUserFolder(const string& username) {
try {
filesystem::create_directory(username);
return true;
} catch (...) {
return false;
}
}
string FileManager::generateFileName(const string& username) {
time_t now = time(0);
tm* localTime = localtime(&now);
stringstream ss;
ss << username << "/"
<< localTime->tm_year + 1900 << "-"
<< setw(2) << setfill('0') << localTime->tm_mon + 1 << "-"
<< setw(2) << setfill('0') << localTime->tm_mday << "-"
<< setw(2) << setfill('0') << localTime->tm_hour << "-"
<< setw(2) << setfill('0') << localTime->tm_min << "-"
<< setw(2) << setfill('0') << localTime->tm_sec << ".txt";
return ss.str();
}
void FileManager::saveQuestionsToFile(const string& filename, const vector<string>& questions) {
ofstream file(filename);
if (file.is_open()) {
for (size_t i = 0; i < questions.size(); i++) {
file << i + 1 << ". " << questions[i] << endl;
if (i < questions.size() - 1) {
file << endl; // 题目之间空一行
}
}
file.close();
}
}
set<string> FileManager::loadExistingQuestions(const string& username) {
set<string> questions;
// 简化实现,实际需要读取所有文件并提取题目
return questions;
}
// 应用程序实现
MathTestGeneratorApp::MathTestGeneratorApp() : isLoggedIn(false) {
generators["小学"] = new PrimaryQuestionGenerator();
generators["初中"] = new JuniorQuestionGenerator();
generators["高中"] = new SeniorQuestionGenerator();
currentGenerator = nullptr;
}
MathTestGeneratorApp::~MathTestGeneratorApp() {
for (auto& pair : generators) {
delete pair.second;
}
}
void MathTestGeneratorApp::run() {
srand(time(0));
while (true) {
if (!isLoggedIn) {
login();
} else {
cout << "准备生成" << currentUser.userType << "数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录";
string input;
cin >> input;
if (input == "-1") {
isLoggedIn = false;
cout << "退出当前用户,重新登录..." << endl;
continue;
}
if (input.find("切换为") == 0) {
string newType = input.substr(3); // 跳过"切换为"
switchUserType(newType);
continue;
}
try {
int questionCount = stoi(input);
if (questionCount >= 10 && questionCount <= 30) {
generatePaper();
} else {
cout << "题目数量应在10-30之间" << endl;
}
} catch (const exception& e) {
cout << "请输入有效的数字" << endl;
}
}
}
}
void MathTestGeneratorApp::login() {
cout << "请输入用户名和密码(用空格隔开):";
string username, password;
cin >> username >> password;
UserAuthenticator authenticator;
if (authenticator.authenticate(username, password, currentUser)) {
isLoggedIn = true;
currentGenerator = generators[currentUser.userType];
cout << "当前选择为" << currentUser.userType << "出题" << endl;
// 加载已存在的题目用于查重
generatedQuestions = FileManager::loadExistingQuestions(currentUser.username);
} else {
cout << "请输入正确的用户名、密码" << endl;
}
}
void MathTestGeneratorApp::generatePaper() {
cout << "准备生成" << currentUser.userType << "数学题目,请输入生成题目数量:";
string input;
cin >> input;
try {
int questionCount = stoi(input);
if (questionCount < 10 || questionCount > 30) {
cout << "题目数量应在10-30之间" << endl;
return;
}
// 创建用户文件夹
FileManager::createUserFolder(currentUser.username);
// 生成题目
vector<string> questions;
for (int i = 0; i < questionCount; i++) {
string question;
int attempts = 0;
do {
question = currentGenerator->generateQuestion();
attempts++;
} while ((isQuestionDuplicate(question) || !currentGenerator->meetsRequirements(question)) && attempts < 100);
if (attempts >= 100) {
cout << "警告:无法生成符合要求的不重复题目" << endl;
}
questions.push_back(question);
addQuestionToHistory(question);
}
// 保存到文件
string filename = FileManager::generateFileName(currentUser.username);
FileManager::saveQuestionsToFile(filename, questions);
cout << "题目已生成到文件:" << filename << endl;
} catch (const exception& e) {
cout << "请输入有效的数字" << endl;
}
}
void MathTestGeneratorApp::switchUserType(const string& newType) {
if (newType == "小学" || newType == "初中" || newType == "高中") {
currentUser.userType = newType;
currentGenerator = generators[newType];
cout << "准备生成" << currentUser.userType << "数学题目,请输入生成题目数量:";
} else {
cout << "请输入小学、初中和高中三个选项中的一个" << endl;
}
}
bool MathTestGeneratorApp::isQuestionDuplicate(const string& question) {
return generatedQuestions.find(question) != generatedQuestions.end();
}
void MathTestGeneratorApp::addQuestionToHistory(const string& question) {
generatedQuestions.insert(question);
}
string MathTestGeneratorApp::getCurrentTimeString() {
time_t now = time(0);
tm* localTime = localtime(&now);
stringstream ss;
ss << localTime->tm_year + 1900 << "-"
<< setw(2) << setfill('0') << localTime->tm_mon + 1 << "-"
<< setw(2) << setfill('0') << localTime->tm_mday << "-"
<< setw(2) << setfill('0') << localTime->tm_hour << "-"
<< setw(2) << setfill('0') << localTime->tm_min << "-"
<< setw(2) << setfill('0') << localTime->tm_sec;
return ss.str();
}
int main() {
MathTestGeneratorApp app;
app.run();
return 0;
}
Loading…
Cancel
Save