2 #2

Merged
hnu202326010103 merged 0 commits from develop into main 4 months ago

@ -1,2 +0,0 @@
# MathsQuestionSystem

@ -0,0 +1,177 @@
#include "Application.h"
#include "UserManager.h"
#include "QuestionGenerator.h"
#include "FileManager.h"
#include <iostream>
#include <cctype>
#include <cstdlib>
Application::Application()
: user_manager_(NULL),
question_generator_(NULL),
file_manager_(NULL),
question_checker_(NULL),
session_manager_(NULL) {
InitializeComponents();
}
Application::~Application() {
Cleanup();
}
void Application::InitializeComponents() {
user_manager_ = new UserManager();
question_generator_ = new QuestionGenerator();
file_manager_ = new FileManager();
question_checker_ = new QuestionChecker(file_manager_);
session_manager_ = new SessionManager(user_manager_,
question_generator_,
question_checker_);
}
int Application::Run() {
DisplayWelcome();
while (true) {
if (!session_manager_->IsLoggedIn()) {
HandleLogin();
continue;
}
DisplayCurrentStatus();
DisplayMainMenu();
std::string choice;
std::cin >> choice;
HandleUserChoice(choice);
}
return 0;
}
void Application::DisplayWelcome() const {
std::cout << "中小学数学卷子自动生成程序" << std::endl;
std::cout << "=================================" << std::endl;
}
void Application::HandleLogin() {
std::string username, password;
std::cout << "请输入用户名和密码(用空格隔开): ";
std::cin >> username >> password;
if (session_manager_->Login(username, password)) {
UserType current_type = session_manager_->GetCurrentUserType();
std::cout << "登录成功!当前选择为" << current_type.GetName() << "出题" << std::endl;
} else {
std::cout << "请输入正确的用户名、密码" << std::endl;
}
}
void Application::DisplayMainMenu() const {
std::cout << "\n请选择操作:" << std::endl;
std::cout << "1. 生成题目" << std::endl;
std::cout << "2. 切换类型" << std::endl;
std::cout << "3. 退出程序" << std::endl;
std::cout << "-1. 退出当前用户" << std::endl;
std::cout << "请输入选择: ";
}
void Application::DisplayCurrentStatus() const {
std::cout << "\n当前用户: " << session_manager_->GetCurrentUsername() << std::endl;
std::cout << "当前类型: " << session_manager_->GetCurrentUserType().GetName() << std::endl;
std::cout << "已生成题目: " << question_checker_->GetUserQuestionCount(
session_manager_->GetCurrentUsername()) << "" << std::endl;
}
void Application::HandleGenerateQuestions() {
std::cout << "准备生成" << session_manager_->GetCurrentUserType().GetName()
<< "数学题目请输入生成题目数量10-30: ";
std::string input;
std::cin >> input;
if (!IsNumber(input)) {
std::cout << "请输入有效的数字" << std::endl;
return;
}
int count = StringToInt(input);
if (count < 10 || count > 30) {
std::cout << "题目数量应在10到30之间" << std::endl;
return;
}
std::vector<std::string> questions = session_manager_->GenerateQuestions(count);
if (file_manager_->SaveQuestions(questions, session_manager_->GetCurrentUsername(),
session_manager_->GetCurrentUserType())) {
std::cout << "成功生成" << questions.size() << "道不重复的"
<< session_manager_->GetCurrentUserType().GetName() << "数学题目!" << std::endl;
if (questions.size() < count) {
std::cout << "注意:由于题目重复,实际生成" << questions.size()
<< "道题目(要求" << count << "道)" << std::endl;
}
}
}
void Application::HandleSwitchType() {
std::cout << "请输入要切换的类型(小学/初中/高中): ";
std::string target_type;
std::cin >> target_type;
session_manager_->SwitchUserType(target_type);
}
void Application::HandleUserChoice(const std::string& choice) {
if (choice == "1") {
HandleGenerateQuestions();
} else if (choice == "2") {
HandleSwitchType();
} else if (choice == "-1") {
session_manager_->Logout();
std::cout << "已退出当前用户" << std::endl;
} else if (choice == "3") {
std::cout << "感谢使用,再见!" << std::endl;
std::exit(0);
} else {
std::cout << "无效选择,请重新输入" << std::endl;
}
}
void Application::Cleanup() {
delete session_manager_;
delete question_checker_;
delete file_manager_;
delete question_generator_;
delete user_manager_;
}
bool Application::IsNumber(const std::string& str) const {
if (str.empty()) return false;
for (size_t i = 0; i < str.length(); ++i) {
char c = str[i];
if (!std::isdigit(c) && !(i == 0 && c == '-')) {
return false;
}
}
return true;
}
int Application::StringToInt(const std::string& str) const {
int result = 0;
int sign = 1;
size_t start = 0;
if (str[0] == '-') {
sign = -1;
start = 1;
}
for (size_t i = start; i < str.length(); ++i) {
if (str[i] >= '0' && str[i] <= '9') {
result = result * 10 + (str[i] - '0');
}
}
return result * sign;
}

@ -0,0 +1,39 @@
#ifndef PROJECT_APPLICATION_H_
#define PROJECT_APPLICATION_H_
#include "IUserManager.h"
#include "IQuestionGenerator.h"
#include "IFileManager.h"
#include "QuestionChecker.h"
#include "SessionManager.h"
#include <string>
class Application {
public:
Application();
~Application();
int Run();
private:
void InitializeComponents();
void DisplayWelcome() const;
void HandleLogin();
void DisplayMainMenu() const;
void DisplayCurrentStatus() const;
void HandleGenerateQuestions();
void HandleSwitchType();
void HandleUserChoice(const std::string& choice);
void Cleanup();
bool IsNumber(const std::string& str) const;
int StringToInt(const std::string& str) const;
IUserManager* user_manager_;
IQuestionGenerator* question_generator_;
IFileManager* file_manager_;
QuestionChecker* question_checker_;
SessionManager* session_manager_;
};
#endif // PROJECT_APPLICATION_H_

Binary file not shown.

@ -5,41 +5,42 @@
#include <cstdlib>
#include <direct.h>
using namespace std;
FileManager::FileManager() {}
bool FileManager::saveQuestions(const vector<string>& questions, const string& username, const UserType& type) {
string filename = username + "_" + getCurrentTimestamp() + ".txt";
ofstream file(filename.c_str());
if (!file.is_open()) {
cout << "无法创建文件: " << filename << endl;
return false;
}
for (size_t i = 0; i < questions.size(); i++) {
file << (i + 1) << ". " << questions[i] << endl;
if (i < questions.size() - 1) {
file << endl;
}
bool FileManager::SaveQuestions(const std::vector<std::string>& questions,
const std::string& username,
const UserType& type) {
std::string filename = username + "_" + GetCurrentTimestamp() + ".txt";
std::ofstream file(filename.c_str());
if (!file.is_open()) {
std::cout << "创建文件失败: " << filename << std::endl;
return false;
}
for (size_t i = 0; i < questions.size(); ++i) {
file << (i + 1) << ". " << questions[i] << std::endl;
if (i < questions.size() - 1) {
file << std::endl;
}
file.close();
cout << "题目已保存到: " << filename << endl;
return true;
}
file.close();
std::cout << "题目保存至: " << filename << std::endl;
return true;
}
vector<string> FileManager::loadExistingQuestions(const string& username) {
return vector<string>();
std::vector<std::string> FileManager::LoadExistingQuestions(const std::string& username) {
// TODO: 实现历史题目加载
return std::vector<std::string>();
}
string FileManager::getCurrentTimestamp() const {
time_t now = time(NULL);
struct tm* tstruct = localtime(&now);
char buf[80];
strftime(buf, sizeof(buf), "%Y-%m-%d-%H-%M-%S", tstruct);
return string(buf);
std::string FileManager::GetCurrentTimestamp() const {
std::time_t now = std::time(NULL);
struct std::tm* tstruct = std::localtime(&now);
char buf[80];
std::strftime(buf, sizeof(buf), "%Y-%m-%d-%H-%M-%S", tstruct);
return std::string(buf);
}

@ -1,20 +1,24 @@
#ifndef FILEMANAGER_H
#define FILEMANAGER_H
#ifndef PROJECT_FILEMANAGER_H_
#define PROJECT_FILEMANAGER_H_
#include "IFileManager.h"
#include <string>
#include <vector>
class FileManager : public IFileManager {
public:
FileManager();
virtual ~FileManager() {}
virtual bool saveQuestions(const std::vector<std::string>& questions, const std::string& username, const UserType& type);
virtual std::vector<std::string> loadExistingQuestions(const std::string& username);
public:
FileManager();
virtual ~FileManager() {}
virtual bool SaveQuestions(const std::vector<std::string>& questions,
const std::string& username,
const UserType& type);
virtual std::vector<std::string> LoadExistingQuestions(
const std::string& username);
private:
std::string getCurrentTimestamp() const;
private:
std::string GetCurrentTimestamp() const;
};
#endif
#endif

Binary file not shown.

@ -1,15 +1,20 @@
#ifndef IFILEMANAGER_H
#define IFILEMANAGER_H
#ifndef PROJECT_IFILEMANAGER_H_
#define PROJECT_IFILEMANAGER_H_
#include <string>
#include <vector>
#include "UserType.h"
class IFileManager {
public:
virtual ~IFileManager() {}
virtual bool saveQuestions(const std::vector<std::string>& questions, const std::string& username, const UserType& type) = 0;
virtual std::vector<std::string> loadExistingQuestions(const std::string& username) = 0;
public:
virtual ~IFileManager() {}
virtual bool SaveQuestions(const std::vector<std::string>& questions,
const std::string& username,
const UserType& type) = 0;
virtual std::vector<std::string> LoadExistingQuestions(
const std::string& username) = 0;
};
#endif
#endif

@ -1,15 +1,19 @@
#ifndef IQUESTIONGENERATOR_H
#define IQUESTIONGENERATOR_H
#ifndef PROJECT_IQUESTIONGENERATOR_H_
#define PROJECT_IQUESTIONGENERATOR_H_
#include <string>
#include <vector>
#include "UserType.h"
class IQuestionGenerator {
public:
virtual ~IQuestionGenerator() {}
virtual std::vector<std::string> generateQuestions(const UserType& type, int count, const std::string& username) = 0;
virtual void addExistingQuestions(const std::vector<std::string>& questions) = 0;
public:
virtual ~IQuestionGenerator() {}
virtual std::vector<std::string> GenerateQuestions(const UserType& type,
int count,
const std::string& username) = 0;
virtual void AddExistingQuestions(const std::vector<std::string>& questions) = 0;
};
#endif
#endif

@ -1,18 +1,20 @@
#ifndef IUSERMANAGER_H
#define IUSERMANAGER_H
#ifndef PROJECT_IUSERMANAGER_H_
#define PROJECT_IUSERMANAGER_H_
#include <string>
#include "UserType.h"
class IUserManager {
public:
virtual ~IUserManager() {}
virtual bool authenticate(const std::string& username, const std::string& password) = 0;
virtual UserType getCurrentUserType() const = 0;
virtual std::string getCurrentUsername() const = 0;
virtual void logout() = 0;
virtual bool isLoggedIn() const = 0;
public:
virtual ~IUserManager() {}
virtual bool Authenticate(const std::string& username,
const std::string& password) = 0;
virtual UserType GetCurrentUserType() const = 0;
virtual std::string GetCurrentUsername() const = 0;
virtual void Logout() = 0;
virtual bool IsLoggedIn() const = 0;
};
#endif
#endif

@ -4,8 +4,8 @@
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
OBJ = QuestionGenerator.o UserManager.o User.o UserType.o FileManager.o main.o QuestionChecker.o SessionManager.o
LINKOBJ = QuestionGenerator.o UserManager.o User.o UserType.o FileManager.o main.o QuestionChecker.o SessionManager.o
OBJ = QuestionGenerator.o UserManager.o User.o UserType.o FileManager.o main.o QuestionChecker.o SessionManager.o Application.o
LINKOBJ = QuestionGenerator.o UserManager.o User.o UserType.o FileManager.o main.o QuestionChecker.o SessionManager.o Application.o
LIBS = -L"D:/³ÌÐòÉè¼Æ/Dev-Cpp/MinGW64/lib" -L"D:/³ÌÐòÉè¼Æ/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib" -static-libgcc
INCS = -I"D:/³ÌÐòÉè¼Æ/Dev-Cpp/MinGW64/include" -I"D:/³ÌÐòÉè¼Æ/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"D:/³ÌÐòÉè¼Æ/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include"
CXXINCS = -I"D:/³ÌÐòÉè¼Æ/Dev-Cpp/MinGW64/include" -I"D:/³ÌÐòÉè¼Æ/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"D:/³ÌÐòÉè¼Æ/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"D:/³ÌÐòÉè¼Æ/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++"
@ -47,3 +47,6 @@ QuestionChecker.o: QuestionChecker.cpp
SessionManager.o: SessionManager.cpp
$(CPP) -c SessionManager.cpp -o SessionManager.o $(CXXFLAGS)
Application.o: Application.cpp
$(CPP) -c Application.cpp -o Application.o $(CXXFLAGS)

@ -4,31 +4,34 @@
QuestionChecker::QuestionChecker(IFileManager* file_manager)
: file_manager_(file_manager) {}
bool QuestionChecker::isDuplicate(const std::string& question, const std::string& username) {
// ¼ì²é±¾´Î»á»°ÊÇ·ñÖØ¸´
if (current_session_questions_.find(question) != current_session_questions_.end()) {
return true;
}
bool QuestionChecker::IsDuplicate(const std::string& question,
const std::string& username) {
// 检查当前会话中是否重复
if (current_session_questions_.find(question) != current_session_questions_.end()) {
return true;
}
std::vector<std::string> history_questions = file_manager_->loadExistingQuestions(username);
for (std::vector<std::string>::iterator it = history_questions.begin();
it != history_questions.end(); ++it) {
if (*it == question) {
return true;
}
// 检查历史题目中是否重复
std::vector<std::string> history_questions = file_manager_->LoadExistingQuestions(username);
for (std::vector<std::string>::iterator it = history_questions.begin();
it != history_questions.end(); ++it) {
if (*it == question) {
return true;
}
return false;
}
return false;
}
void QuestionChecker::addGeneratedQuestion(const std::string& question, const std::string& username) {
current_session_questions_.insert(question);
void QuestionChecker::AddGeneratedQuestion(const std::string& question,
const std::string& username) {
current_session_questions_.insert(question);
}
void QuestionChecker::clearUserCache(const std::string& username) {
current_session_questions_.clear();
void QuestionChecker::ClearUserCache(const std::string& username) {
current_session_questions_.clear();
}
int QuestionChecker::getUserQuestionCount(const std::string& username) const {
return current_session_questions_.size();
int QuestionChecker::GetUserQuestionCount(const std::string& username) const {
return current_session_questions_.size();
}

@ -1,5 +1,5 @@
#ifndef QUESTIONCHECKER_H
#define QUESTIONCHECKER_H
#ifndef PROJECT_QUESTIONCHECKER_H_
#define PROJECT_QUESTIONCHECKER_H_
#include <string>
#include <set>
@ -7,17 +7,17 @@
#include "IFileManager.h"
class QuestionChecker {
public:
QuestionChecker(IFileManager* file_manager);
bool isDuplicate(const std::string& question, const std::string& username);
void addGeneratedQuestion(const std::string& question, const std::string& username);
void clearUserCache(const std::string& username);
int getUserQuestionCount(const std::string& username) const;
public:
explicit QuestionChecker(IFileManager* file_manager);
bool IsDuplicate(const std::string& question, const std::string& username);
void AddGeneratedQuestion(const std::string& question, const std::string& username);
void ClearUserCache(const std::string& username);
int GetUserQuestionCount(const std::string& username) const;
private:
IFileManager* file_manager_;
std::set<std::string> current_session_questions_;
private:
IFileManager* file_manager_;
std::set<std::string> current_session_questions_;
};
#endif
#endif // PROJECT_QUESTIONCHECKER_H_

Binary file not shown.

@ -4,100 +4,103 @@
#include <ctime>
#include <cstdlib>
using namespace std;
QuestionGenerator::QuestionGenerator() {
srand(static_cast<unsigned int>(time(NULL)));
std::srand(static_cast<unsigned int>(std::time(NULL)));
}
vector<string> QuestionGenerator::generateQuestions(const UserType& type, int count, const string& username) {
vector<string> questions;
std::vector<std::string> QuestionGenerator::GenerateQuestions(const UserType& type,
int count,
const std::string& username) {
std::vector<std::string> questions;
int generated_count = 0;
while (generated_count < count) {
std::string question;
for (int i = 0; i < count; ) {
string question;
if (type == UserType::PRIMARY) {
question = generatePrimaryQuestion();
} else if (type == UserType::JUNIOR) {
question = generateJuniorQuestion();
} else if (type == UserType::SENIOR) {
question = generateSeniorQuestion();
}
// 查重:检查是否已存在
if (existingQuestions.find(question) == existingQuestions.end()) {
questions.push_back(question);
existingQuestions.insert(question);
i++;
}
if (type.GetValue() == 0) { // PRIMARY
question = GeneratePrimaryQuestion();
} else if (type.GetValue() == 1) { // JUNIOR
question = GenerateJuniorQuestion();
} else if (type.GetValue() == 2) { // SENIOR
question = GenerateSeniorQuestion();
}
return questions;
// 查重:检查是否已存在
if (existing_questions_.find(question) == existing_questions_.end()) {
questions.push_back(question);
existing_questions_.insert(question);
++generated_count;
}
}
return questions;
}
void QuestionGenerator::addExistingQuestions(const vector<string>& questions) {
for (size_t i = 0; i < questions.size(); i++) {
existingQuestions.insert(questions[i]);
}
void QuestionGenerator::AddExistingQuestions(const std::vector<std::string>& questions) {
// 使用传统迭代器替代 range-based for
for (std::vector<std::string>::const_iterator it = questions.begin();
it != questions.end(); ++it) {
existing_questions_.insert(*it);
}
}
string QuestionGenerator::generatePrimaryQuestion() {
int operandCount = 2 + rand() % 3; // 2-4个操作数
stringstream ss;
for (int i = 0; i < operandCount; i++) {
ss << (rand() % 100 + 1);
if (i < operandCount - 1) {
char ops[] = {'+', '-', '*', '/'};
ss << " " << ops[rand() % 4] << " ";
}
std::string QuestionGenerator::GeneratePrimaryQuestion() {
int operand_count = 2 + std::rand() % 3; // 2-4个操作数
std::stringstream ss;
for (int i = 0; i < operand_count; ++i) {
ss << (std::rand() % 100 + 1);
if (i < operand_count - 1) {
const char ops[] = {'+', '-', '*', '/'};
ss << " " << ops[std::rand() % 4] << " ";
}
ss << " = ";
return ss.str();
}
ss << " = ";
return ss.str();
}
string QuestionGenerator::generateJuniorQuestion() {
stringstream ss;
// 确保至少有一个平方或开根号
if (rand() % 2 == 0) {
ss << (rand() % 20 + 1) << "的平方";
} else {
ss << "根号" << (rand() % 100 + 1);
}
// 可能添加普通运算
if (rand() % 2 == 0) {
char ops[] = {'+', '-', '*', '/'};
ss << " " << ops[rand() % 4] << " " << (rand() % 100 + 1);
}
ss << " = ";
return ss.str();
std::string QuestionGenerator::GenerateJuniorQuestion() {
std::stringstream ss;
// 确保至少有一个平方或开根号
if (std::rand() % 2 == 0) {
ss << (std::rand() % 20 + 1) << "的平方";
} else {
ss << "根号" << (std::rand() % 100 + 1);
}
// 可能添加普通运算
if (std::rand() % 2 == 0) {
const char ops[] = {'+', '-', '*', '/'};
ss << " " << ops[std::rand() % 4] << " " << (std::rand() % 100 + 1);
}
ss << " = ";
return ss.str();
}
string QuestionGenerator::generateSeniorQuestion() {
stringstream ss;
// 确保至少有一个三角函数
const char* trig[] = {"sin", "cos", "tan"};
ss << trig[rand() % 3] << "(" << (rand() % 100) << "度)";
// 可能添加普通运算
if (rand() % 2 == 0) {
char ops[] = {'+', '-', '*', '/'};
ss << " " << ops[rand() % 4] << " " << (rand() % 100 + 1);
}
ss << " = ";
return ss.str();
std::string QuestionGenerator::GenerateSeniorQuestion() {
std::stringstream ss;
// 确保至少有一个三角函数
const char* trig[] = {"sin", "cos", "tan"};
ss << trig[std::rand() % 3] << "(" << (std::rand() % 100) << "度)";
// 可能添加普通运算
if (std::rand() % 2 == 0) {
const char ops[] = {'+', '-', '*', '/'};
ss << " " << ops[std::rand() % 4] << " " << (std::rand() % 100 + 1);
}
ss << " = ";
return ss.str();
}
int QuestionGenerator::getRandomNumber(int min, int max) {
return rand() % (max - min + 1) + min;
int QuestionGenerator::GetRandomNumber(int min, int max) {
return std::rand() % (max - min + 1) + min;
}
char QuestionGenerator::getRandomOperator() {
char operators[] = {'+', '-', '*', '/'};
return operators[rand() % 4];
char QuestionGenerator::GetRandomOperator() {
const char operators[] = {'+', '-', '*', '/'};
return operators[std::rand() % 4];
}

@ -1,26 +1,29 @@
#ifndef QUESTIONGENERATOR_H
#define QUESTIONGENERATOR_H
#ifndef PROJECT_QUESTIONGENERATOR_H_
#define PROJECT_QUESTIONGENERATOR_H_
#include "IQuestionGenerator.h"
#include <set>
#include <string>
class QuestionGenerator : public IQuestionGenerator {
public:
QuestionGenerator();
virtual ~QuestionGenerator() {}
virtual std::vector<std::string> generateQuestions(const UserType& type, int count, const std::string& username);
virtual void addExistingQuestions(const std::vector<std::string>& questions);
public:
QuestionGenerator();
virtual ~QuestionGenerator() {}
virtual std::vector<std::string> GenerateQuestions(const UserType& type,
int count,
const std::string& username);
virtual void AddExistingQuestions(const std::vector<std::string>& questions);
private:
std::string generatePrimaryQuestion();
std::string generateJuniorQuestion();
std::string generateSeniorQuestion();
int getRandomNumber(int min, int max);
char getRandomOperator();
std::set<std::string> existingQuestions;
private:
std::string GeneratePrimaryQuestion();
std::string GenerateJuniorQuestion();
std::string GenerateSeniorQuestion();
int GetRandomNumber(int min, int max);
char GetRandomOperator();
std::set<std::string> existing_questions_;
};
#endif
#endif

Binary file not shown.

@ -7,69 +7,70 @@ SessionManager::SessionManager(IUserManager* user_manager,
: user_manager_(user_manager),
question_generator_(question_generator),
question_checker_(question_checker),
current_type_(UserType::PRIMARY), // 使用具体的UserType常量初始化
current_type_(UserType::kPrimary),
type_switched_(false) {}
bool SessionManager::switchUserType(const std::string& target_type) {
if (target_type == "小学") {
current_type_ = UserType::PRIMARY;
type_switched_ = true;
std::cout << "已切换到小学出题模式" << std::endl;
return true;
} else if (target_type == "初中") {
current_type_ = UserType::JUNIOR;
type_switched_ = true;
std::cout << "已切换到初中出题模式" << std::endl;
return true;
} else if (target_type == "高中") {
current_type_ = UserType::SENIOR;
type_switched_ = true;
std::cout << "已切换到高中出题模式" << std::endl;
return true;
}
std::cout << "切换失败!请输入'小学'、'初中'或'高中'" << std::endl;
return false;
bool SessionManager::SwitchUserType(const std::string& target_type) {
if (target_type == "小学") {
current_type_ = UserType::kPrimary;
type_switched_ = true;
std::cout << "已切换到小学出题模式" << std::endl;
return true;
} else if (target_type == "初中") {
current_type_ = UserType::kJunior;
type_switched_ = true;
std::cout << "已切换到初中出题模式" << std::endl;
return true;
} else if (target_type == "高中") {
current_type_ = UserType::kSenior;
type_switched_ = true;
std::cout << "已切换到高中出题模式" << std::endl;
return true;
}
std::cout << "切换失败!请输入'小学'、'初中'或'高中'" << std::endl;
return false;
}
UserType SessionManager::getCurrentUserType() const {
if (type_switched_) {
return current_type_;
}
return user_manager_->getCurrentUserType();
UserType SessionManager::GetCurrentUserType() const {
if (type_switched_) {
return current_type_;
}
return user_manager_->GetCurrentUserType();
}
std::string SessionManager::getCurrentUsername() const {
return user_manager_->getCurrentUsername();
std::string SessionManager::GetCurrentUsername() const {
return user_manager_->GetCurrentUsername();
}
bool SessionManager::isLoggedIn() const {
return user_manager_->isLoggedIn();
bool SessionManager::IsLoggedIn() const {
return user_manager_->IsLoggedIn();
}
bool SessionManager::login(const std::string& username, const std::string& password) {
type_switched_ = false;
return user_manager_->authenticate(username, password);
bool SessionManager::Login(const std::string& username, const std::string& password) {
type_switched_ = false;
return user_manager_->Authenticate(username, password);
}
void SessionManager::logout() {
type_switched_ = false;
user_manager_->logout();
question_checker_->clearUserCache(getCurrentUsername());
void SessionManager::Logout() {
type_switched_ = false;
user_manager_->Logout();
question_checker_->ClearUserCache(GetCurrentUsername());
}
std::vector<std::string> SessionManager::generateQuestions(int count) {
std::vector<std::string> valid_questions;
std::string username = getCurrentUsername();
UserType type = getCurrentUserType();
// 直接生成题目依赖QuestionGenerator内部的查重
valid_questions = question_generator_->generateQuestions(type, count, username);
// 记录到查重
for (std::vector<std::string>::iterator it = valid_questions.begin();
it != valid_questions.end(); ++it) {
question_checker_->addGeneratedQuestion(*it, username);
}
return valid_questions;
std::vector<std::string> SessionManager::GenerateQuestions(int count) {
std::vector<std::string> valid_questions;
std::string username = GetCurrentUsername();
UserType type = GetCurrentUserType();
// 直接生成题目
valid_questions = question_generator_->GenerateQuestions(type, count, username);
// 记录到查重器 - 使用传统迭代
for (std::vector<std::string>::iterator it = valid_questions.begin();
it != valid_questions.end(); ++it) {
question_checker_->AddGeneratedQuestion(*it, username);
}
return valid_questions;
}

@ -1,30 +1,33 @@
#ifndef SESSIONMANAGER_H
#define SESSIONMANAGER_H
#ifndef PROJECT_SESSIONMANAGER_H_
#define PROJECT_SESSIONMANAGER_H_
#include <string>
#include <vector>
#include "UserType.h"
#include "IUserManager.h"
#include "IQuestionGenerator.h"
#include "QuestionChecker.h"
class SessionManager {
public:
SessionManager(IUserManager* user_manager, IQuestionGenerator* question_generator, QuestionChecker* question_checker);
bool switchUserType(const std::string& target_type);
UserType getCurrentUserType() const;
std::string getCurrentUsername() const;
bool isLoggedIn() const;
bool login(const std::string& username, const std::string& password);
void logout();
std::vector<std::string> generateQuestions(int count);
public:
SessionManager(IUserManager* user_manager,
IQuestionGenerator* question_generator,
QuestionChecker* question_checker);
bool SwitchUserType(const std::string& target_type);
UserType GetCurrentUserType() const;
std::string GetCurrentUsername() const;
bool IsLoggedIn() const;
bool Login(const std::string& username, const std::string& password);
void Logout();
std::vector<std::string> GenerateQuestions(int count);
private:
IUserManager* user_manager_;
IQuestionGenerator* question_generator_;
QuestionChecker* question_checker_;
UserType current_type_;
bool type_switched_;
private:
IUserManager* user_manager_;
IQuestionGenerator* question_generator_;
QuestionChecker* question_checker_;
UserType current_type_;
bool type_switched_;
};
#endif
#endif

Binary file not shown.

@ -1,8 +1,8 @@
#include "User.h"
User::User(const std::string& username, const std::string& password, const UserType& type)
: username(username), password(password), type(type) {}
: username_(username), password_(password), type_(type) {}
bool User::authenticate(const std::string& inputPassword) const {
return password == inputPassword;
bool User::Authenticate(const std::string& input_password) const {
return password_ == input_password;
}

@ -1,23 +1,23 @@
#ifndef USER_H
#define USER_H
#ifndef PROJECT_USER_H_
#define PROJECT_USER_H_
#include <string>
#include "UserType.h"
class User {
public:
User(const std::string& username, const std::string& password, const UserType& type);
std::string getUsername() const { return username; }
std::string getPassword() const { return password; }
UserType getType() const { return type; }
bool authenticate(const std::string& inputPassword) const;
public:
User(const std::string& username, const std::string& password, const UserType& type);
std::string GetUsername() const { return username_; }
std::string GetPassword() const { return password_; }
UserType GetType() const { return type_; }
bool Authenticate(const std::string& input_password) const;
private:
std::string username;
std::string password;
UserType type;
private:
std::string username_;
std::string password_;
UserType type_;
};
#endif
#endif // PROJECT_USER_H_

Binary file not shown.

@ -1,53 +1,54 @@
#include "UserManager.h"
#include <iostream>
UserManager::UserManager() : currentUser(NULL), loggedIn(false) {
initializeUsers();
UserManager::UserManager() : current_user_(NULL), logged_in_(false) {
InitializeUsers();
}
void UserManager::initializeUsers() {
presetUsers.push_back(User("张三1", "123", UserType::PRIMARY));
presetUsers.push_back(User("张三2", "123", UserType::PRIMARY));
presetUsers.push_back(User("张三3", "123", UserType::PRIMARY));
presetUsers.push_back(User("李四1", "123", UserType::JUNIOR));
presetUsers.push_back(User("李四2", "123", UserType::JUNIOR));
presetUsers.push_back(User("李四3", "123", UserType::JUNIOR));
presetUsers.push_back(User("王五1", "123", UserType::SENIOR));
presetUsers.push_back(User("王五2", "123", UserType::SENIOR));
presetUsers.push_back(User("王五3", "123", UserType::SENIOR));
void UserManager::InitializeUsers() {
preset_users_.push_back(User("张三1", "123", UserType::kPrimary));
preset_users_.push_back(User("张三2", "123", UserType::kPrimary));
preset_users_.push_back(User("张三3", "123", UserType::kPrimary));
preset_users_.push_back(User("李四1", "123", UserType::kJunior));
preset_users_.push_back(User("李四2", "123", UserType::kJunior));
preset_users_.push_back(User("李四3", "123", UserType::kJunior));
preset_users_.push_back(User("王五1", "123", UserType::kSenior));
preset_users_.push_back(User("王五2", "123", UserType::kSenior));
preset_users_.push_back(User("王五3", "123", UserType::kSenior));
}
bool UserManager::authenticate(const std::string& username, const std::string& password) {
for (size_t i = 0; i < presetUsers.size(); i++) {
if (presetUsers[i].getUsername() == username &&
presetUsers[i].authenticate(password)) {
currentUser = &presetUsers[i];
loggedIn = true;
return true;
}
bool UserManager::Authenticate(const std::string& username, const std::string& password) {
// 使用传统迭代器替代 range-based for
for (std::vector<User>::iterator it = preset_users_.begin();
it != preset_users_.end(); ++it) {
if (it->GetUsername() == username && it->Authenticate(password)) {
current_user_ = &(*it); // 获取指针
logged_in_ = true;
return true;
}
return false;
}
return false;
}
UserType UserManager::getCurrentUserType() const {
if (currentUser) {
return currentUser->getType();
}
return UserType::PRIMARY;
UserType UserManager::GetCurrentUserType() const {
if (current_user_ != NULL) { // 使用 NULL 替代 nullptr
return current_user_->GetType();
}
return UserType::kPrimary;
}
std::string UserManager::getCurrentUsername() const {
if (currentUser) {
return currentUser->getUsername();
}
return "";
std::string UserManager::GetCurrentUsername() const {
if (current_user_ != NULL) { // 使用 NULL 替代 nullptr
return current_user_->GetUsername();
}
return "";
}
void UserManager::logout() {
currentUser = NULL;
loggedIn = false;
void UserManager::Logout() {
current_user_ = NULL; // 使用 NULL 替代 nullptr
logged_in_ = false;
}
bool UserManager::isLoggedIn() const {
return loggedIn;
bool UserManager::IsLoggedIn() const {
return logged_in_;
}

@ -1,27 +1,27 @@
#ifndef USERMANAGER_H
#define USERMANAGER_H
#ifndef PROJECT_USERMANAGER_H_
#define PROJECT_USERMANAGER_H_
#include "IUserManager.h"
#include <vector>
#include "User.h"
class UserManager : public IUserManager {
public:
UserManager();
virtual ~UserManager() {}
virtual bool authenticate(const std::string& username, const std::string& password);
virtual UserType getCurrentUserType() const;
virtual std::string getCurrentUsername() const;
virtual void logout();
virtual bool isLoggedIn() const;
public:
UserManager();
virtual ~UserManager() {}
virtual bool Authenticate(const std::string& username, const std::string& password);
virtual UserType GetCurrentUserType() const;
virtual std::string GetCurrentUsername() const;
virtual void Logout();
virtual bool IsLoggedIn() const;
private:
std::vector<User> presetUsers;
const User* currentUser;
bool loggedIn;
void initializeUsers();
private:
void InitializeUsers();
std::vector<User> preset_users_;
const User* current_user_;
bool logged_in_;
};
#endif
#endif // PROJECT_USERMANAGER_H_

Binary file not shown.

@ -1,5 +1,5 @@
#include "UserType.h"
const UserType UserType::PRIMARY(0, "小学");
const UserType UserType::JUNIOR(1, "初中");
const UserType UserType::SENIOR(2, "高中");
const UserType UserType::kPrimary(0, "小学");
const UserType UserType::kJunior(1, "初中");
const UserType UserType::kSenior(2, "高中");

@ -1,28 +1,28 @@
#ifndef USERTYPE_H
#define USERTYPE_H
#ifndef PROJECT_USERTYPE_H_
#define PROJECT_USERTYPE_H_
#include <string>
class UserType {
public:
static const UserType PRIMARY;
static const UserType JUNIOR;
static const UserType SENIOR;
UserType() : value(-1), name("δ֪") {} // Ìí¼ÓĬÈϹ¹Ô캯Êý
UserType(const UserType& other) : value(other.value), name(other.name) {}
std::string getName() const { return name; }
int getValue() const { return value; }
bool operator==(const UserType& other) const { return value == other.value; }
bool operator!=(const UserType& other) const { return value != other.value; }
private:
UserType(int val, const std::string& n) : value(val), name(n) {}
int value;
std::string name;
public:
static const UserType kPrimary;
static const UserType kJunior;
static const UserType kSenior;
UserType() : value_(-1), name_("Unknown") {}
UserType(const UserType& other) : value_(other.value_), name_(other.name_) {}
std::string GetName() const { return name_; }
int GetValue() const { return value_; }
bool operator==(const UserType& other) const { return value_ == other.value_; }
bool operator!=(const UserType& other) const { return value_ != other.value_; }
private:
UserType(int val, const std::string& n) : value_(val), name_(n) {}
int value_;
std::string name_;
};
#endif
#endif

Binary file not shown.

@ -1,190 +1,6 @@
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <cstdlib>
#include "IUserManager.h"
#include "IQuestionGenerator.h"
#include "IFileManager.h"
#include "QuestionChecker.h"
#include "SessionManager.h"
#include "UserManager.h"
#include "QuestionGenerator.h"
#include "FileManager.h"
using namespace std;
bool isNumber(const string& str);
int stringToInt(const string& str);
void displayWelcome();
void handleLogin(SessionManager* session_manager);
void displayMainMenu();
void handleGenerateQuestions(SessionManager* session_manager, IFileManager* file_manager);
void handleSwitchType(SessionManager* session_manager);
void handleUserChoice(SessionManager* session_manager, IFileManager* file_manager, const string& choice);
void cleanupResources(SessionManager* session_manager, QuestionChecker* question_checker,
IFileManager* file_manager, IQuestionGenerator* question_generator,
IUserManager* user_manager);
bool isNumber(const string& str) {
if (str.empty()) return false;
for (size_t i = 0; i < str.length(); i++) {
if (!isdigit(str[i]) && !(i == 0 && str[i] == '-')) {
return false;
}
}
return true;
}
int stringToInt(const string& str) {
int result = 0;
int sign = 1;
size_t start = 0;
if (str[0] == '-') {
sign = -1;
start = 1;
}
for (size_t i = start; i < str.length(); i++) {
if (str[i] >= '0' && str[i] <= '9') {
result = result * 10 + (str[i] - '0');
}
}
return result * sign;
}
void displayWelcome() {
cout << "中小学数学卷子自动生成程序" << endl;
cout << "=================================" << endl;
}
void handleLogin(SessionManager* session_manager) {
string username, password;
cout << "请输入用户名和密码(用空格隔开): ";
cin >> username >> password;
if (session_manager->login(username, password)) {
UserType current_type = session_manager->getCurrentUserType();
cout << "登录成功!当前选择为" << current_type.getName() << "出题" << endl;
} else {
cout << "请输入正确的用户名、密码" << endl;
}
}
void displayMainMenu() {
cout << "\n请选择操作:" << endl;
cout << "1. 生成题目" << endl;
cout << "2. 切换类型" << endl;
cout << "3. 退出程序" << endl;
cout << "-1. 退出当前用户" << endl;
cout << "请输入选择: ";
}
void displayCurrentStatus(SessionManager* session_manager, QuestionChecker* question_checker) {
cout << "\n当前用户: " << session_manager->getCurrentUsername() << endl;
cout << "当前类型: " << session_manager->getCurrentUserType().getName() << endl;
cout << "已生成题目: " << question_checker->getUserQuestionCount(
session_manager->getCurrentUsername()) << "" << endl;
}
void handleGenerateQuestions(SessionManager* session_manager, IFileManager* file_manager) {
cout << "准备生成" << session_manager->getCurrentUserType().getName()
<< "数学题目请输入生成题目数量10-30: ";
string input;
cin >> input;
if (!isNumber(input)) {
cout << "请输入有效的数字" << endl;
return;
}
int count = stringToInt(input);
if (count < 10 || count > 30) {
cout << "题目数量应在10到30之间" << endl;
return;
}
vector<string> questions = session_manager->generateQuestions(count);
if (file_manager->saveQuestions(questions, session_manager->getCurrentUsername(),
session_manager->getCurrentUserType())) {
cout << "成功生成" << questions.size() << "道不重复的"
<< session_manager->getCurrentUserType().getName() << "数学题目!" << endl;
if (questions.size() < (size_t)count) {
cout << "注意:由于题目重复,实际生成" << questions.size()
<< "道题目(要求" << count << "道)" << endl;
}
}
}
void handleSwitchType(SessionManager* session_manager) {
cout << "请输入要切换的类型(小学/初中/高中): ";
string target_type;
cin >> target_type;
session_manager->switchUserType(target_type);
}
void handleUserChoice(SessionManager* session_manager, IFileManager* file_manager,
QuestionChecker* question_checker, const string& choice) {
if (choice == "1") {
handleGenerateQuestions(session_manager, file_manager);
}
else if (choice == "2") {
handleSwitchType(session_manager);
}
else if (choice == "-1") {
session_manager->logout();
cout << "已退出当前用户" << endl;
}
else if (choice == "3") {
cout << "感谢使用,再见!" << endl;
exit(0);
}
else {
cout << "无效选择,请重新输入" << endl;
}
}
void cleanupResources(SessionManager* session_manager, QuestionChecker* question_checker,
IFileManager* file_manager, IQuestionGenerator* question_generator,
IUserManager* user_manager) {
delete session_manager;
delete question_checker;
delete file_manager;
delete question_generator;
delete user_manager;
}
#include "Application.h"
int main() {
// 创建组件
IUserManager* user_manager = new UserManager();
IQuestionGenerator* question_generator = new QuestionGenerator();
IFileManager* file_manager = new FileManager();
QuestionChecker* question_checker = new QuestionChecker(file_manager);
SessionManager* session_manager = new SessionManager(user_manager, question_generator, question_checker);
displayWelcome();
// 主循环
while (true) {
if (!session_manager->isLoggedIn()) {
handleLogin(session_manager);
continue;
}
displayCurrentStatus(session_manager, question_checker);
displayMainMenu();
string choice;
cin >> choice;
handleUserChoice(session_manager, file_manager, question_checker, choice);
}
cleanupResources(session_manager, question_checker, file_manager, question_generator, user_manager);
return 0;
Application app;
return app.Run();
}

Binary file not shown.

@ -29,7 +29,7 @@ IncludeVersionInfo=0
SupportXPThemes=0
CompilerSet=0
CompilerSettings=0000000000000000000000000
UnitCount=18
UnitCount=20
[VersionInfo]
Major=1
@ -230,3 +230,23 @@ Priority=1000
OverrideBuildCmd=0
BuildCmd=
[Unit19]
FileName=Application.h
CompileCpp=1
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=
[Unit20]
FileName=Application.cpp
CompileCpp=1
Folder=
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=

Binary file not shown.

@ -1,23 +1,23 @@
[Editors]
Order=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
Focused=17
Order=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19
Focused=18
[Editor_0]
CursorCol=5
CursorRow=61
CursorCol=3
CursorRow=16
TopLine=1
LeftChar=1
[Editor_1]
CursorCol=7
CursorRow=26
CursorCol=8
CursorRow=29
TopLine=1
LeftChar=1
[Editor_2]
CursorCol=2
CursorRow=53
CursorRow=54
TopLine=1
LeftChar=1
[Editor_3]
CursorCol=7
CursorCol=34
CursorRow=27
TopLine=1
LeftChar=1
@ -27,67 +27,77 @@ CursorRow=8
TopLine=1
LeftChar=1
[Editor_5]
CursorCol=7
CursorCol=27
CursorRow=23
TopLine=1
LeftChar=1
[Editor_6]
CursorCol=44
CursorCol=45
CursorRow=5
TopLine=1
LeftChar=1
[Editor_7]
CursorCol=3
CursorRow=26
CursorCol=9
CursorRow=28
TopLine=1
LeftChar=1
[Editor_8]
CursorCol=7
CursorRow=20
CursorCol=29
CursorRow=11
TopLine=1
LeftChar=1
[Editor_9]
CursorCol=76
CursorRow=33
TopLine=3
CursorCol=2
CursorRow=46
TopLine=4
LeftChar=1
[Editor_10]
CursorCol=2
CursorRow=190
TopLine=158
CursorRow=6
TopLine=1
LeftChar=1
[Editor_11]
CursorCol=7
CursorRow=15
CursorCol=37
CursorRow=10
TopLine=1
LeftChar=1
[Editor_12]
CursorCol=7
CursorRow=15
CursorCol=30
CursorRow=10
TopLine=1
LeftChar=1
[Editor_13]
CursorCol=7
CursorRow=18
CursorCol=9
CursorRow=20
TopLine=1
LeftChar=1
[Editor_16]
CursorCol=7
CursorRow=30
CursorCol=9
CursorRow=33
TopLine=1
LeftChar=1
[Editor_17]
CursorCol=1
CursorRow=12
TopLine=1
CursorCol=2
CursorRow=76
TopLine=44
LeftChar=1
[Editor_15]
CursorCol=1
CursorRow=23
TopLine=1
CursorCol=2
CursorRow=37
TopLine=5
LeftChar=1
[Editor_14]
CursorCol=7
CursorCol=38
CursorRow=23
TopLine=1
LeftChar=1
[Editor_18]
CursorCol=34
CursorRow=39
TopLine=7
LeftChar=1
[Editor_19]
CursorCol=2
CursorRow=177
TopLine=145
LeftChar=1

Loading…
Cancel
Save