改成UTF-8格式

main
陈映江 7 months ago
parent e2746adef9
commit c563fbc93d

@ -16,8 +16,8 @@ void ExamSystem::run() {
void ExamSystem::loginLoop() {
std::string username="",passworld="";
while(true){
std::cout<<"<<<<<<欢迎进入考试试卷生成系统!<<<<<<"<<std::endl;
std::cout<<"请输入用户名与密码使用空格间隔如要退出该系统请输入exit"<<std::endl;
std::cout<<"<<<<<<欢迎进入考试试卷生成系统!<<<<<<"<<std::endl;
std::cout<<"请输入用户名与密码使用空格间隔如要退出该系统请输入exit"<<std::endl;
std::string line="";
std::getline(std::cin,line);
std::istringstream iss(line);
@ -25,7 +25,7 @@ void ExamSystem::loginLoop() {
exit(0);
}
if(!(iss>>username>>passworld)){
std::cout<<"格式有误,请重新输入!"<<std::endl;
std::cout<<"格式有误,请重新输入!"<<std::endl;
continue;
}
if(userManager.loginCheck(username, passworld)){
@ -36,7 +36,7 @@ void ExamSystem::loginLoop() {
}
else {
//system("cls");
std::cout<<"账号或者密码错误,请重试!\n";
std::cout<<"账号或者密码错误,请重试!\n";
//system("pause");
}
}
@ -44,9 +44,9 @@ void ExamSystem::loginLoop() {
void ExamSystem::mainMenu() {
while(true){
std::cout<<"<<<<<<欢迎您!"<<currentUser<<"<<<<<<\n"<<"当前选择为"<<currentType<<"出题。"<<std::endl;
std::cout<<"准备生成"<<currentType<<"数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录:";
std::cout<<"请输入操作:\n"<<"1. 生成题目输入数字“10-30”\n"<<"2. 切换类型(输入“切换为小学/初中/高中”)\n"<<"3. 退出登录(输入“-1”\n";
std::cout<<"<<<<<<欢迎您!"<<currentUser<<"<<<<<<\n"<<"当前选择为"<<currentType<<"出题。"<<std::endl;
std::cout<<"准备生成"<<currentType<<"数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录:";
std::cout<<"请输入操作:\n"<<"1. 生成题目输入数字“10-30”\n"<<"2. 切换类型(输入“切换为小学/初中/高中”)\n"<<"3. 退出登录(输入“-1”\n";
std::string count="";
std::getline(std::cin,count);
int flag=countHandle(count);
@ -63,13 +63,13 @@ void ExamSystem::mainMenu() {
break;
}
if(flag==4){
std::cout<<"请按要求进行输入哦!"<<std::endl;
std::cout<<"请按要求进行输入哦!"<<std::endl;
}
}
}
int ExamSystem::countHandle(std::string &count) {
if(count.substr(0,6)=="切换为"){
if(count.substr(0,6)=="切换为"){
count = count.substr(6);
//std::cout<<count<<std::endl;
return 1;
@ -99,11 +99,11 @@ void ExamSystem::getExam(int count) {
}
fileHandler.saveQuestions(currentUser, questions);
std::cout << "已生成" << count << "道题目,保存成功!\n";
std::cout << "已生成" << count << "道题目,保存成功!\n";
}
QuestionGenerator *ExamSystem::getGenerator() {
if(currentType == "小学") return &primaryGenerator;
else if (currentType == "初中") return &juniorGenerator;
if(currentType == "小学") return &primaryGenerator;
else if (currentType == "初中") return &juniorGenerator;
return &seniorGenerator;
}

@ -10,20 +10,20 @@
#include <cctype>
#include <algorithm>
// 安全处理用户名,移除或替换非法字符
// 安全处理用户名,移除或替换非法字符
std::string FileHandler::getSafeUsername(const std::string& username) {
std::string safeUsername;
for (char c : username) {
// 只允许字母、数字、下划线、连字符和点号
// 只允许字母、数字、下划线、连字符和点号
if (std::isalnum(c) || c == '_' || c == '-' || c == '.') {
safeUsername += c;
} else {
// 将其他字符替换为下划线
// 将其他字符替换为下划线
safeUsername += '_';
}
}
// 确保用户名不为空
// 确保用户名不为空
if (safeUsername.empty()) {
safeUsername = "default_user";
}
@ -31,39 +31,39 @@ std::string FileHandler::getSafeUsername(const std::string& username) {
return safeUsername;
}
// 获取用户目录路径
// 获取用户目录路径
std::string FileHandler::getUserPath(const std::string& username) {
return "papers/" + getSafeUsername(username);
}
// 创建用户目录
// 创建用户目录
void FileHandler::createUserDir(const std::string& username) {
std::string userPath = getUserPath(username);
try {
if (!fs::exists(userPath)) {
// 递归创建目录
// 递归创建目录
bool created = fs::create_directories(userPath);
if (!created) {
std::cerr << "警告: 无法创建用户目录: " << userPath << std::endl;
std::cerr << "警告: 无法创建用户目录: " << userPath << std::endl;
} else {
std::cout << "已创建用户目录: " << userPath << std::endl;
std::cout << "已创建用户目录: " << userPath << std::endl;
}
}
} catch (const fs::filesystem_error& e) {
std::cerr << "文件系统错误 - 创建目录失败: " << e.what() << std::endl;
// 尝试创建默认目录作为备用
std::cerr << "文件系统错误 - 创建目录失败: " << e.what() << std::endl;
// 尝试创建默认目录作为备用
try {
if (!fs::exists("papers/default_user")) {
fs::create_directories("papers/default_user");
}
} catch (...) {
std::cerr << "严重错误: 无法创建默认目录" << std::endl;
std::cerr << "严重错误: 无法创建默认目录" << std::endl;
}
}
}
// 保存题目到文件
// 保存题目到文件
void FileHandler::saveQuestions(const std::string& username, const std::vector<std::string>& questions) {
try {
createUserDir(username);
@ -72,7 +72,7 @@ void FileHandler::saveQuestions(const std::string& username, const std::vector<s
std::ofstream file(fullPath);
if (!file.is_open()) {
std::cerr << "错误: 无法打开文件: " << fullPath << std::endl;
std::cerr << "错误: 无法打开文件: " << fullPath << std::endl;
return;
}
@ -81,18 +81,18 @@ void FileHandler::saveQuestions(const std::string& username, const std::vector<s
}
file.close();
std::cout << "题目已保存到: " << fullPath << std::endl;
std::cout << "题目已保存到: " << fullPath << std::endl;
} catch (const std::exception& e) {
std::cerr << "保存问题时出错: " << e.what() << std::endl;
std::cerr << "保存问题时出错: " << e.what() << std::endl;
}
}
// 规范化题目字符串,移除空格等以便比较
// 规范化题目字符串,移除空格等以便比较
std::string FileHandler::normalizeQuestion(const std::string& question) {
std::string normalized = question;
// 移除所有空格
// 移除所有空格
normalized.erase(std::remove_if(normalized.begin(), normalized.end(),
[](unsigned char c) { return std::isspace(c); }),
normalized.end());
@ -100,7 +100,7 @@ std::string FileHandler::normalizeQuestion(const std::string& question) {
return normalized;
}
// 检查题目是否重复
// 检查题目是否重复
bool FileHandler::isDuplicate(const std::string& username, const std::string& question) {
std::string userPath = getUserPath(username);
@ -119,13 +119,13 @@ bool FileHandler::isDuplicate(const std::string& username, const std::string& qu
}
}
} catch (const fs::filesystem_error& e) {
std::cerr << "文件系统错误 - 检查重复题目失败: " << e.what() << std::endl;
std::cerr << "文件系统错误 - 检查重复题目失败: " << e.what() << std::endl;
}
return false;
}
// 检查文件中是否包含特定题目
// 检查文件中是否包含特定题目
bool FileHandler::checkFile(const fs::path& path, const std::string& normalizedQuestion) {
try {
std::ifstream file(path);
@ -135,31 +135,31 @@ bool FileHandler::checkFile(const fs::path& path, const std::string& normalizedQ
std::string line;
while (std::getline(file, line)) {
// 跳过序号行(如 "1. "
// 跳过序号行(如 "1. "
if (line.empty() || (line.find('.') != std::string::npos &&
std::isdigit(line[0]) && line.size() > 2 && line[1] == '.')) {
continue;
}
// 规范化当前行并与题目比较
// 规范化当前行并与题目比较
std::string normalizedLine = normalizeQuestion(line);
if (normalizedLine == normalizedQuestion) {
return true;
}
}
} catch (const std::exception& e) {
std::cerr << "检查文件内容时出错: " << e.what() << std::endl;
std::cerr << "检查文件内容时出错: " << e.what() << std::endl;
}
return false;
}
// 获取时间戳字符串
// 获取时间戳字符串
std::string FileHandler::getTimestamp() {
auto now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
// 使用线程安全的时间函数
// 使用线程安全的时间函数
std::tm local_time;
#ifdef _WIN32
localtime_s(&local_time, &now_c);

@ -10,7 +10,7 @@
namespace fs = std::filesystem;
// 文件操作类
// 文件操作类
class FileHandler {
public:
void saveQuestions(const std::string& username, const std::vector<std::string>& questions);

@ -15,7 +15,7 @@ protected:
std::string addSqrt(const std::string exper);
std::string dealNum(std::string randomNum);
std::string dealParenthese(std::string ques);
std::vector<std::string> juniorOps = {"^2", "¡Ì"};
std::vector<std::string> juniorOps = {"^2", ""};
};

@ -15,6 +15,7 @@ std::string SeniorGenerator::generate() {
}
std::string SeniorGenerator::addSeniorOp(std::string exper) {
return seniorOps[gen() % 3] + exper + "°";
}
std::string SeniorGenerator::dealParenthese(std::string ques) {
@ -28,6 +29,7 @@ std::string SeniorGenerator::dealParenthese(std::string ques) {
for ( i; i < ques.size(); i++) {
if (ques[i] == ')') {
result = ques.substr(0, i+1);
result += "°";
result += ques.substr(i);
}
}

@ -11,14 +11,14 @@ UserManager::UserManager() {
void UserManager::loadAccounts() {
accounts = {
{"张三1", "123"}, {"张三2", "123"}, {"张三3", "123"},
{"李四1", "123"}, {"李四2", "123"}, {"李四3", "123"},
{"王五1", "123"}, {"王五2", "123"}, {"王五3", "123"}
{"张三1", "123"}, {"张三2", "123"}, {"张三3", "123"},
{"李四1", "123"}, {"李四2", "123"}, {"李四3", "123"},
{"王五1", "123"}, {"王五2", "123"}, {"王五3", "123"}
};
TypeLink = {
{"张三1", "小学"}, {"张三2", "小学"}, {"张三3", "小学"},
{"李四1", "初中"}, {"李四2", "初中"}, {"李四3", "初中"},
{"王五1", "高中"}, {"王五2", "高中"}, {"王五3", "高中"}
{"张三1", "小学"}, {"张三2", "小学"}, {"张三3", "小学"},
{"李四1", "初中"}, {"李四2", "初中"}, {"李四3", "初中"},
{"王五1", "高中"}, {"王五2", "高中"}, {"王五3", "高中"}
};
}
@ -37,7 +37,7 @@ std::string UserManager::getAccountType(const std::string &username) {
return it->second;
}
else {
return "<<<<出错了,暂时无法获取该身份!<<<<\n";
return "<<<<出错了,暂时无法获取该身份!<<<<\n";
}
}
@ -51,22 +51,22 @@ bool UserManager::setCurrentType(std::string username, std::string type) {
if(it != TypeLink.end()){
currentType = type;
//it->second = type;
std::cout<<"切换成功!\n";
std::cout<<"切换成功!\n";
return true;
}
else {
std::cout<<"切换失败T_T出现错误。。。\n";
std::cout<<"切换失败T_T出现错误。。。\n";
return false;
}
}
else {
std::cout<<"切换失败!请选择“小学”、“初中”、“高中”中的一个!";
std::cout<<"切换失败!请选择“小学”、“初中”、“高中”中的一个!";
//system("pause");
return false;
}
}
bool UserManager::checkType(std::string type) {
if(type!= "小学" && type!= "初中" && type!= "高中" ) return false;
if(type!= "小学" && type!= "初中" && type!= "高中" ) return false;
else return true;
}
Loading…
Cancel
Save