pull/2/head
HuQiQiang 4 months ago
parent bcbef934ae
commit a230d4b00c

@ -77,7 +77,8 @@
"ranges": "cpp",
"span": "cpp",
"stack": "cpp",
"text_encoding": "cpp"
"text_encoding": "cpp",
"codecvt": "cpp"
},
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",

@ -1,117 +1,122 @@
#ifndef DATA_STRUCTURES_H
#define DATA_STRUCTURES_H
#ifndef BACKEND_INTERFACE_H
#define BACKEND_INTERFACE_H
#include <string>
#include <vector>
#include <memory>
#include <map>
#include <ctime>
#include "../../src/exam_system/exam_system.h"
// 用户信息结构体
struct UserInfo {
std::string email;
std::string password;
bool isLoggedIn = false;
};
namespace exam_system {
// 题目信息结构体
struct QuestionInfo {
int id;
std::string content;
std::vector<std::string> options;
int correctAnswer; // 0-3 对应ABCD
int correct_answer; // 0-3 对应ABCD
};
// 测试结果结构体
struct TestResult {
int totalQuestions; ///< 总问题数
int correctAnswers; ///< 正确问题数
double score; ///< 得分
int total_questions; ///< 总问题数
int correct_answers; ///< 正确问题数
double score; ///< 得分
std::string difficulty;
};
// 验证码信息结构体
struct VerificationCode {
std::string code;
std::time_t generate_time;
std::string email;
};
// 邮箱配置结构体
struct EmailConfig {
std::string smtp_server = "smtp.163.com";
int smtp_port = 25;
std::string username = "your_email@163.com";
std::string password = "your_password";
bool use_curl = true; // 使用libcurl发送邮件
};
// 前后端通信接口
class BackendInterface {
public:
/**
* @brief
* @param email
* @return true
*/
virtual bool sendSecondCode(const std::string& email) = 0;
/**
* @brief
* @param user_name
* @param password
* @param code
* @return true
*/
virtual bool userRegitster(const std::string& user_name, const std::string& password, const std::string& code) = 0;
public:
virtual ~BackendInterface() = default;
/**
* @brief
* @param user_name
* @param password
* @return true
*/
virtual bool userLogin(const std::string& user_name, const std::string& password) = 0;
virtual void userLogout() = 0;
virtual bool changePassword(const std::string& oldPassword, const std::string& newPassword) = 0;
virtual void userExit() = 0;
/**
* @brief
* @param difficulty
* @param count
* @return QuestionInfor
*/
virtual std::vector<QuestionInfo> generateQuestions(const std::string& difficulty, int count) = 0;
/**
* @brief
* @param userAnswers 0-3A-D
* @return TestResult
*/
virtual TestResult submitAnswers(const std::vector<int>& userAnswers) = 0;
virtual ~BackendInterface() = default;
virtual bool SendVerificationCode(const std::string& email) = 0;
virtual bool UserRegister(const std::string& username,
const std::string& password,
const std::string& email,
const std::string& code) = 0;
virtual bool UserLogin(const std::string& username,
const std::string& password) = 0;
virtual void UserLogout() = 0;
virtual bool ChangePassword(const std::string& old_password,
const std::string& new_password) = 0;
virtual void UserExit() = 0;
virtual std::vector<QuestionInfo> GenerateQuestions(
const std::string& difficulty, int count) = 0;
virtual TestResult SubmitAnswers(const std::vector<int>& user_answers) = 0;
virtual std::string GetCurrentUser() const = 0;
};
class BackendImpl : public BackendInterface {
public:
bool sendSecondCode(const std::string& email){ return true; };
bool userRegitster(const std::string& user_name, const std::string& password, const std::string& code){ return true; };
bool userLogin(const std::string& user_name, const std::string& password){ return true; };
bool changePassword(const std::string& old_password, const std::string& new_password){ return true; };
void userLogout(){return;};
void userExit(){return;};
std::vector<QuestionInfo> generateQuestions(const std::string& difficulty, int count) {
std::vector<QuestionInfo> questions = {
{1, "1 + 1 = ?", {"1", "2", "3", "4"}, 1},
{2, "3 * 4 = ?", {"10", "12", "14", "16"}, 1},
{3, "10 / 2 = ?", {"3", "4", "5", "6"}, 2},
{4, "7 - 3 = ?", {"2", "3", "4", "5"}, 2},
{5, "2 * 2 = ?", {"2", "3", "4", "5"}, 2},
{6, "9 / 3 = ?", {"2", "3", "4", "5"}, 1},
{7, "5 * 6 = ?", {"25", "30", "35", "40"}, 1},
{8, "8 + 7 = ?", {"14", "15", "16", "17"}, 1},
{9, "20 / 4 = ?", {"3", "4", "5", "6"}, 2},
{10, "6 * 7 = ?", {"40", "42", "44", "46"}, 1}
};
return questions;
}
public:
BackendImpl();
~BackendImpl() override = default;
TestResult submitAnswers(const std::vector<int>& userAnswers){
TestResult test_result = {
10,
8,
80.0,
"小学"
};
BackendImpl(const BackendImpl&) = delete;
BackendImpl& operator=(const BackendImpl&) = delete;
return test_result;
}
bool SendVerificationCode(const std::string& email) override;
bool UserRegister(const std::string& username,
const std::string& password,
const std::string& email,
const std::string& code) override;
bool UserLogin(const std::string& username,
const std::string& password) override;
void UserLogout() override;
bool ChangePassword(const std::string& old_password,
const std::string& new_password) override;
void UserExit() override;
std::vector<QuestionInfo> GenerateQuestions(
const std::string& difficulty, int count) override;
TestResult SubmitAnswers(const std::vector<int>& user_answers) override;
std::string GetCurrentUser() const override;
~BackendImpl() = default ;
private:
// 邮件发送方法
bool SendEmail(const std::string& recipient,
const std::string& subject,
const std::string& body);
bool SendEmailViaCurl(const std::string& recipient,
const std::string& subject,
const std::string& body);
// 验证码管理
std::string GenerateVerificationCode();
bool ValidateVerificationCode(const std::string& email,
const std::string& code);
void CleanExpiredVerificationCodes();
// 题目生成辅助方法
std::string CalculateSimpleAnswer(const std::string& problem);
std::vector<std::string> GenerateOptions(const std::string& correct_answer);
double CalculateScore(int correct_count, int total_count);
// 成员变量
std::map<std::string, VerificationCode> verification_codes_;
std::map<std::string, std::vector<QuestionInfo>> user_questions_;
std::string current_user_;
std::vector<std::string> current_correct_answers_;
std::unique_ptr<ExamSystem> exam_system_;
EmailConfig email_config_;
};
} // namespace exam_system
#endif
#endif // BACKEND_INTERFACE_H

@ -21,6 +21,7 @@ namespace constants {
constexpr int kMinQuestionCount = 10; ///< 最小题目数量
constexpr int kMaxQuestionCount = 30; ///< 最大题目数量
constexpr int kExitCode = -1; ///< 退出代码
constexpr char kDefaultUserDataFile[] = "users.csv"; ///< 默认用户数据文件名
} // namespace constants
/**
@ -38,6 +39,7 @@ struct UserInfo {
*/
struct SystemConfig {
std::vector<UserInfo> users; ///< 系统用户列表
std::string user_data_file; ///< 用户数据文件路径
};
/**
@ -76,18 +78,46 @@ struct LoginResponse {
};
/**
* @brief
* @brief
*/
struct ChangePasswordRequest {
std::string username; ///< 用户名
std::string old_password; ///< 旧密码
std::string new_password; ///< 新密码
};
/**
* @brief
*/
struct ChangePasswordResponse {
bool success; ///< 修改是否成功
std::string error_message; ///< 错误信息
};
/**
* @brief
*
*
* API
*/
class ExamSystem {
public:
public:
/**
* @brief - 使
*/
ExamSystem();
/**
* @brief
* @brief -
* @param config
*/
explicit ExamSystem(const SystemConfig& config);
/**
* @brief -
* @param config_file
*/
explicit ExamSystem(const std::string& config_file);
/// 析构函数
~ExamSystem() = default;
@ -108,6 +138,13 @@ class ExamSystem {
*/
void Logout(const std::string& username);
/**
* @brief
* @param request
* @return
*/
ChangePasswordResponse ChangePassword(const ChangePasswordRequest& request);
/**
* @brief
* @param request
@ -166,113 +203,115 @@ class ExamSystem {
* @return
*/
bool ValidateQuestionCount(int count) const;
/**
* @brief
*/
void Run();
private:
/**
* @brief
* @brief
* @param file_path 使
* @return
*/
void Initialize();
bool SaveUserData(const std::string& file_path = "");
/**
* @brief
* @param username
* @param password
* @return truefalse
* @brief
* @param file_path 使
* @return
*/
bool Login(const std::string& username, const std::string& password);
bool ReloadUserData(const std::string& file_path = "");
/**
* @brief
* @param type
*/
void SetGenerator(const std::string& type);
/**
* @brief
* @param username
* @param type
* @brief
* @return
*/
void LoadUserHistory(const std::string& username);
size_t GetUserCount() const { return config_.users.size(); }
/**
* @brief
* @param username
* @return nullptr
* @brief
* @return
*/
ProblemGenerator* GetUserGenerator(const std::string& username);
size_t GetLoggedInUserCount() const { return user_sessions_.size(); }
/**
* @brief
* @param username
* @return
* @brief
* @return
*/
bool IsUserLoggedIn(const std::string& username) const;
std::string GetUserDataFilePath() const { return config_.user_data_file; }
SystemConfig config_; ///< 系统配置信息
// 用户会话数据
struct UserSession {
std::unique_ptr<ProblemGenerator> generator;
std::string current_type;
bool history_loaded = false;
};
private:
/**
* @brief
*/
void Initialize();
/**
* @brief
* @param type
* @brief
*/
void SwitchType(const std::string& type);
void InitializeDefaultUsers();
/**
* @brief
* @param count
* @return
* @brief
* @param file_path
* @return
*/
std::vector<std::string> GenerateExam(int count);
bool LoadUserDataFromFile(const std::string& file_path);
/**
* @brief
* @param problems
* @brief
* @param file_path
* @return true
*/
void SaveExam(const std::vector<std::string>& problems);
bool EnsureUserDataFileExists(const std::string& file_path);
/**
* @brief
* @brief
* @param line
* @param user_info
* @return
*/
void HandleUserInput();
bool ParseUserDataLine(const std::string& line, UserInfo& user_info);
/**
* @brief
* @brief
* @param username
* @param type
*/
void DisplayLoginPrompt();
void SetGenerator(const std::string& username, const std::string& type);
/**
* @brief
* @brief
* @param username
*/
void DisplayQuestionCountPrompt();
void LoadUserHistory(const std::string& username);
/**
* @brief
* @brief
* @param username
* @return nullptr
*/
void DisplayWelcomeMessage();
ProblemGenerator* GetUserGenerator(const std::string& username);
/**
* @brief
* @brief
* @param username
* @return
*/
void Logout();
bool IsUserLoggedIn(const std::string& username) const;
/**
* @brief
* @brief
* @param username
* @param new_password
* @return
*/
void LoadUserHistory();
bool UpdateUserPassword(const std::string& username, const std::string& new_password);
// 用户会话数据
struct UserSession {
std::unique_ptr<ProblemGenerator> generator;
std::string current_type;
bool history_loaded = false;
};
SystemConfig config_; ///< 系统配置信息
std::unique_ptr<ProblemGenerator> generator_; ///< 题目生成器实例
const UserInfo* current_user_; ///< 当前登录用户指针
SystemConfig config_; ///< 系统配置信息
std::map<std::string, UserSession> user_sessions_; ///< 用户会话映射
DuplicateChecker duplicate_checker_; ///< 查重服务实例
};

Loading…
Cancel
Save