|
|
#include <iostream>
|
|
|
#include <string>
|
|
|
#include <sstream>
|
|
|
#include <cstdlib>
|
|
|
|
|
|
#include "auth_manager.h"
|
|
|
#include "session_manager.h"
|
|
|
#include "file_manager.h"
|
|
|
|
|
|
// 显示欢迎界面
|
|
|
void ShowWelcome() {
|
|
|
std::cout << "=====================================" << std::endl;
|
|
|
std::cout << " 中小学数学卷子自动生成系统" << std::endl;
|
|
|
std::cout << "=====================================" << std::endl;
|
|
|
}
|
|
|
|
|
|
// 显示主菜单
|
|
|
void ShowMainMenu(const std::string& username, const std::string& difficulty) {
|
|
|
std::cout << "\n=====================================" << std::endl;
|
|
|
std::cout << "当前用户: " << username << " (" << difficulty << "老师)" << std::endl;
|
|
|
std::cout << "当前出题类型: " << difficulty << std::endl;
|
|
|
std::cout << "=====================================" << std::endl;
|
|
|
std::cout << "请选择操作:" << std::endl;
|
|
|
std::cout << "1. 生成题目(输入题目数量 10-30)" << std::endl;
|
|
|
std::cout << "2. 切换难度(输入:切换为小学/初中/高中)" << std::endl;
|
|
|
std::cout << "3. 退出登录(输入:-1)" << std::endl;
|
|
|
std::cout << "> ";
|
|
|
}
|
|
|
|
|
|
int main() {
|
|
|
AuthManager auth_manager;
|
|
|
SessionManager session_manager;
|
|
|
FileManager file_manager;
|
|
|
|
|
|
while (true) {
|
|
|
ShowWelcome();
|
|
|
std::cout << "请输入用户名和密码(格式:用户名 密码),或输入 quit 退出系统:" << std::endl; // 修改提示
|
|
|
std::cout << "> ";
|
|
|
|
|
|
std::string input;
|
|
|
std::getline(std::cin, input);
|
|
|
|
|
|
// 去除前后空格
|
|
|
input.erase(0, input.find_first_not_of(" "));
|
|
|
input.erase(input.find_last_not_of(" ") + 1);
|
|
|
|
|
|
// 检查是否要退出系统(只在首页)
|
|
|
if (input == "quit" || input == "exit" || input == "退出") {
|
|
|
std::cout << "感谢使用中小学数学卷子自动生成系统,再见!" << std::endl;
|
|
|
break; // 退出整个程序
|
|
|
}
|
|
|
|
|
|
std::istringstream iss(input);
|
|
|
std::string username, password;
|
|
|
iss >> username >> password;
|
|
|
|
|
|
// 检查输入格式
|
|
|
if (username.empty() || password.empty()) {
|
|
|
std::cout << "请输入正确的用户名、密码" << std::endl;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 检查是否正好有两个token
|
|
|
std::vector<std::string> tokens;
|
|
|
std::string token;
|
|
|
while (iss >> token) {
|
|
|
tokens.push_back(token);
|
|
|
}
|
|
|
|
|
|
if (!tokens.empty()) { // 如果还有更多token,说明输入格式错误
|
|
|
std::cout << "输入格式错误:请输入用户名和密码,用空格分隔" << std::endl;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
std::string user_type;
|
|
|
if (auth_manager.Authenticate(username, password, &user_type)) {
|
|
|
const char* difficulty_name =
|
|
|
(user_type == "primary") ? "小学" :
|
|
|
(user_type == "junior") ? "初中" : "高中";
|
|
|
|
|
|
std::cout << "登录成功!当前选择为" << difficulty_name << "出题" << std::endl;
|
|
|
|
|
|
session_manager.SetUser(username, user_type);
|
|
|
file_manager.CreateUserDirectory(username);
|
|
|
|
|
|
// 主循环
|
|
|
bool logged_in = true;
|
|
|
while (logged_in) {
|
|
|
ShowMainMenu(username, session_manager.GetCurrentDifficulty());
|
|
|
|
|
|
std::string command;
|
|
|
std::getline(std::cin, command);
|
|
|
|
|
|
// 去除前后空格
|
|
|
command.erase(0, command.find_first_not_of(" "));
|
|
|
command.erase(command.find_last_not_of(" ") + 1);
|
|
|
|
|
|
if (command.empty()) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 首先检查退出命令
|
|
|
if (command == "-1") {
|
|
|
std::cout << "退出当前用户,重新登录..." << std::endl;
|
|
|
logged_in = false;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 检查切换难度命令
|
|
|
if (command.find("切换为") == 0) {
|
|
|
std::string new_difficulty = command.substr(9);
|
|
|
|
|
|
if (new_difficulty == "小学" || new_difficulty == "初中" || new_difficulty == "高中") {
|
|
|
std::string difficulty_type =
|
|
|
(new_difficulty == "小学") ? "primary" :
|
|
|
(new_difficulty == "初中") ? "junior" : "senior";
|
|
|
|
|
|
if (session_manager.SwitchDifficulty(difficulty_type)) {
|
|
|
std::cout << "切换成功!当前出题类型: " << new_difficulty << std::endl;
|
|
|
}
|
|
|
} else {
|
|
|
std::cout << "请输入小学、初中和高中三个选项中的一个" << std::endl;
|
|
|
}
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 尝试解析为数字(题目数量)
|
|
|
try {
|
|
|
int count = std::stoi(command);
|
|
|
if (count >= 10 && count <= 30) {
|
|
|
std::cout << "正在生成" << session_manager.GetCurrentDifficulty()
|
|
|
<< "数学题目,数量: " << count << "..." << std::endl;
|
|
|
|
|
|
QuestionGenerator* generator = session_manager.GetCurrentGenerator();
|
|
|
auto questions = generator->GenerateQuestions(count, username);
|
|
|
|
|
|
if (file_manager.SaveQuestions(username, questions)) {
|
|
|
std::cout << "题目生成完成!已保存到文件。" << std::endl;
|
|
|
} else {
|
|
|
std::cout << "文件保存失败!" << std::endl;
|
|
|
}
|
|
|
} else {
|
|
|
std::cout << "题目数量范围应为10-30,请重新输入" << std::endl;
|
|
|
}
|
|
|
} catch (const std::exception& e) {
|
|
|
std::cout << "无效命令,请重新输入" << std::endl;
|
|
|
std::cout << "有效命令:10-30(生成题目)、-1(退出登录)、切换为小学/初中/高中" << std::endl;
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
std::cout << "请输入正确的用户名、密码" << std::endl;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
} |