You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "hanshu.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <ctime>
#include <algorithm>
using namespace std;
// 预设用户数据
vector<User> presetusers = {
{"张三1", "123", "小学"},
{"张三2", "123", "小学"},
{"张三3", "123", "小学"},
{"李四1", "123", "初中"},
{"李四2", "123", "初中"},
{"李四3", "123", "初中"},
{"王五1", "123", "高中"},
{"王五2", "123", "高中"},
{"王五3", "123", "高中"}
};
// 用户认证函数
User* AuthenticateUser(const string& username, const string& password) {
for (auto& user : presetusers) {
if (user.username == username && user.password == password) {
return &user;
}
}
return nullptr;
}
bool IsValidUserType(const string& type) {
return type == "小学" || type == "初中" || type == "高中";
}
// 文件操作函数
void SaveQuestionsToFile(const vector<string>& questions,
const string& username,
const string& type) {
CreateUserDirectory(username);
string filename = GetCurrentTimestamp() + ".txt";
string fullPath = username + "/" + filename;
ofstream file(fullPath);
if (file.is_open()) {
file << type << "数学题目\n";
file << "生成时间: " << GetCurrentTimestamp() << "\n\n";
for (size_t i = 0; i < questions.size(); i++) {
file << i + 1 << ". " << questions[i] << "\n\n";
}
file.close();
cout << "题目已保存到: " << fullPath << endl;
} else {
cout << "保存文件失败: " << fullPath << endl;
}
}
string GetCurrentTimestamp() {
time_t now = time(nullptr);
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();
}
void CreateUserDirectory(const string& username) {
#ifdef _WIN32
string command = "md " + username; // Windows 命令
#else
string command = "mkdir -p " + username; // Linux/Mac 命令
#endif
system(command.c_str());
}
// 输入验证函数
bool IsValidQuestionCount(int count) {
return (count >= 10 && count <= 30) || count == -1;
}
bool IsSwitchCommand(const string& input, string& newtype) {
if (input.find("切换为") == 0) {
newtype = input.substr(6,input.length()-1);
if(!IsValidUserType(newtype))
{
cout << "请输入小学、初中和高中三个选项中的一个" << endl;
return false;
}
else return IsValidUserType(newtype);
}
return false;
}
// 系统流程函数
void DisplayWelcomeMessage() {
cout << "=== 数学题目生成系统 ===" << endl;
}
void DisplayCurrentMode(const string& type) {
cout << "当前选择为" << type << "出题" << endl;
}
void DisplayQuestionGenerator(std::unique_ptr<QuestionGenerator> generator,const User* users){
// 主交互循环
while (true) {
cout << "准备生成" << generator->GetType()
<< "数学题目,请输入生成题目数量(输入-1将退出当前用户重新登录";
string input;
cin >> input;
// 处理退出命令
if (input == "-1") {
cout << "退出当前用户,重新登录..." << endl;
break;
}
// 处理切换命令
string newtype;
if (IsSwitchCommand(input, newtype)) {
generator = CreateQuestionGenerator(newtype);
cout << "准备生成" << newtype << "数学题目,请输入生成题目数量:";
cin >> input;
}
// 处理题目生成
try {
int count = stoi(input);
if (count >= 10 && count <= 30) {
auto questions = generator->GenerateQuestion(count);
SaveQuestionsToFile(questions, users->username, generator->GetType());
DisplayQuestionExamples(questions);
} else {
cout << "题目数量应在10-30之间" << endl;
}
} catch (const exception& e) {
cout << "请输入有效的数字" << endl;
}
}
}
void DisplayQuestionExamples(const vector<string>& questions) {
if (!questions.empty()) {
cout << "题目示例:" << endl;
for (int i = 0; i < min(3, (int)questions.size()); i++) {
cout << " " << questions[i] << endl;
}
}
}