From 582c065a9b6f062a60cbcb50ac6ebde05c536d2e Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 28 Sep 2025 14:46:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=AD=E5=B0=8F=E5=AD=A6=E7=94=9F=E5=8D=B7?= =?UTF-8?q?=E5=AD=90=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/README.md | 3 + src/6.cpp | 514 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 517 insertions(+) create mode 100644 doc/README.md create mode 100644 src/6.cpp diff --git a/doc/README.md b/doc/README.md new file mode 100644 index 0000000..8e7116d --- /dev/null +++ b/doc/README.md @@ -0,0 +1,3 @@ +# self-project +Windows shell可正常运行 + diff --git a/src/6.cpp b/src/6.cpp new file mode 100644 index 0000000..745addf --- /dev/null +++ b/src/6.cpp @@ -0,0 +1,514 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +//⣺?δ֪ +#include +#include + +#ifdef _WIN32 +#include +#include +#else +#include +#include +#endif + +using namespace std; + +// ûö +enum UserType { + PRIMARY, // Сѧ + JUNIOR, // + SENIOR, // + UNKNOWN //δ֪ +}; + +// ûṹ +struct User { + string username; + string password; + UserType type; +}; + +//ʷĿڲ +set loadExistingQuestions(const string& userDir) { + set existingQuestions;//ʹüϱظ + + #ifdef _WIN32 + // Windows汾Ŀ¼ + WIN32_FIND_DATA findFileData; + HANDLE hFind = FindFirstFile((userDir + "\\*.txt").c_str(), &findFileData); + + if (hFind != INVALID_HANDLE_VALUE) { + do { + if (!(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { + string filename = userDir + "\\" + findFileData.cFileName; + ifstream file(filename); + string line; + while (getline(file, line)) { + if (!line.empty() && line.find(".") != string::npos) { + // ȡĿݣȥţ + size_t dotPos = line.find("."); + if (dotPos != string::npos) { + string question = line.substr(dotPos + 2); // ". " + existingQuestions.insert(question); + } + } + } + file.close(); + } + } while (FindNextFile(hFind, &findFileData)); + FindClose(hFind); + } + #endif + + return existingQuestions; +} + +//ĿǷΨһ +bool isQuestionUnique(const string& question, const set& existingQuestions) { + return existingQuestions.find(question) == existingQuestions.end(); +} + +//ַָ +vector split(const string& str, char delimiter) { + vector tokens; + string token; + istringstream tokenStream(str); + + while (getline(tokenStream, token, delimiter)) { + if (!token.empty()) { + tokens.push_back(token); + } + } + return tokens; +} + +// ַ޼ - ȥַβĿհַ +string trim(const string& str) { + size_t start = str.find_first_not_of(" \t\n\r"); + if (start == string::npos) return ""; + + size_t end = str.find_last_not_of(" \t\n\r"); + return str.substr(start, end - start + 1); +} + +// ַתСд - ַתΪСдʽ +string toLower(const string& str) { + string result = str; + transform(result.begin(), result.end(), result.begin(), ::tolower); + return result; +} + +//֤ +bool isNumber(const string& str) { + if (str.empty()) return false; + + if (str == "-1") return true; + + for (size_t i = 0; i < str.length(); i++) { + if (!isdigit(str[i])) return false; + } + return true; +} + +// ȡǰʱַ - ɸʽΪ"---ʱ--"ʱַ +string getCurrentTimeString() { + time_t now = time(0); + struct tm* localTime = localtime(&now); + + char buffer[20]; + strftime(buffer, sizeof(buffer), "%Y-%m-%d-%H-%M-%S", localTime); + return string(buffer); +} + +//Ŀ¼ +bool createDirectory(const string& path) { + #ifdef _WIN32 + return _mkdir(path.c_str()) == 0; + #else + return mkdir(path.c_str(), 0755) == 0; + #endif +} + +//ûתַ +string getUserTypeString(UserType type) { + switch (type) { + case PRIMARY: return "Сѧ"; + case JUNIOR: return ""; + case SENIOR: return ""; + default: return "δ֪"; + } +} + +//ַתΪӦö +UserType parseUserType(const string& typeStr) { + string lowerType = toLower(typeStr); + if (lowerType == "Сѧ") return PRIMARY; + if (lowerType == "") return JUNIOR; + if (lowerType == "") return SENIOR; + return UNKNOWN; +} + +// û +class UserManager { +private: + vector users; + + void initializeUsers() { + // Сѧ˺ + users.push_back((User){"1", "123", PRIMARY}); + users.push_back((User){"2", "123", PRIMARY}); + users.push_back((User){"3", "123", PRIMARY}); + + // ˺ + users.push_back((User){"1", "123", JUNIOR}); + users.push_back((User){"2", "123", JUNIOR}); + users.push_back((User){"3", "123", JUNIOR}); + + // ˺ + users.push_back((User){"1", "123", SENIOR}); + users.push_back((User){"2", "123", SENIOR}); + users.push_back((User){"3", "123", SENIOR}); + } + +public: + //캯ʼ + UserManager() { + initializeUsers(); + } + + //֤ûǷȷ + User* authenticate(const string& username, const string& password) { + for (size_t i = 0; i < users.size(); i++) { + if (users[i].username == username && users[i].password == password) { + return &users[i]; + } + } + return NULL; + } +}; + +//Ŀӿ࣬ +class IQuestionGenerator { +public: + virtual ~IQuestionGenerator() {} + virtual void setUserType(UserType type) = 0; + virtual string generateQuestion() = 0; + virtual vector generatePaper(int questionCount, const string& username) = 0; +}; + +// Ŀʵ̳࣬нӿ +class QuestionGenerator : public IQuestionGenerator { +private: + UserType currentType; + set existingQuestions;//ʷĿڲ + + int generateOperand() { + return rand() % 100 + 1;//Χ1-100 + } + + //ɱʽ + string generateExpression(int operandCount) { + string operators[] = {"+", "-", "*", "/"}; + vector operands;//Ų + + for (int i = 0; i < operandCount; i++) { + operands.push_back(generateOperand()); + } + + stringstream ss; + ss << operands[0]; + + for (int i = 1; i < operandCount; i++) { + string op = operators[rand() % 4];// + ss << " " << op << " " << operands[i]; + } + + return ss.str(); + } + + //СѧĿ + string generatePrimaryQuestion() { + int operandCount = rand() % 4 + 2; // СѧҪ2-5 + return generateExpression(operandCount); + } + + //ɳĿ + string generateJuniorQuestion() { + int operandCount = rand() % 5 + 1; + string expression = generateExpression(operandCount); + + // ȷһƽ򿪸 + if (rand() % 2 == 0) { + return expression +"^2"; // ƽ + } else { + return "" + expression; // + } + } + + //ɸĿ + string generateSeniorQuestion() { + int operandCount = rand() % 5 + 1; + string expression = generateExpression(operandCount); + + // ȷһǺ + string trigFunctions[] = {"sin", "cos", "tan"}; + string trigFunc = trigFunctions[rand() % 3]; + + return trigFunc + "(" + expression + ")"; + } + +public: + //캯ʼ + QuestionGenerator() : currentType(PRIMARY) { + srand(time(0)); + } + + //û + void setUserType(UserType type) override {// + currentType = type; + } + + // ʷĿڲ + void setExistingQuestions(const set& questions) { + existingQuestions = questions; + } + + //ɵĿ + string generateQuestion() override { + string question; + int attempts = 0; + const int MAX_ATTEMPTS = 100; // ֹѭ + + // ѭĿֱɲظĿﵽԴ + do { + switch (currentType) { + case PRIMARY: + question = generatePrimaryQuestion(); + break; + case JUNIOR: + question = generateJuniorQuestion(); + break; + case SENIOR: + question = generateSeniorQuestion(); + break; + default: + question = generatePrimaryQuestion(); + } + attempts++; + } while (!isQuestionUnique(question, existingQuestions) && attempts < MAX_ATTEMPTS); + + // Ŀӵʷ¼ + existingQuestions.insert(question); + return question; + } + + //ԾָĿ + vector generatePaper(int questionCount, const string& username) override {// + vector paper; + + for (int i = 1; i <= questionCount; i++) { + string question = generateQuestion(); + stringstream ss; + ss << i << ". " << question;// + paper.push_back(ss.str()); + } + + return paper; + } +}; + +// Ծϵͳ +class PaperSystem { +private: + UserManager userManager; + QuestionGenerator questionGenerator; + User* currentUser; + string currentDirectory; + set existingQuestions;//¼ʱʷĿ + + //û¼ + bool login() { + cout << "û루ÿո"; + + string input; + getline(cin, input); + + vector tokens = split(input, ' '); + if (tokens.size() != 2) { + cout << "ȷû" << endl; + return false; + } + + string username = trim(tokens[0]); + string password = trim(tokens[1]); + + currentUser = userManager.authenticate(username, password); + if (currentUser == NULL) { + cout << "ȷû" << endl; + return false; + } + + questionGenerator.setUserType(currentUser->type); + + // ʷĿڲ + string userDir = currentDirectory + "/" + currentUser->username; + createDirectory(userDir); + existingQuestions = loadExistingQuestions(userDir); + questionGenerator.setExistingQuestions(existingQuestions); + + cout << "ǰѡΪ" << getUserTypeString(currentUser->type) << "" << endl; + + return true; + } + + //е"лΪxx" + bool processSwitchCommand(const string& input) { + if (input.length() < 7) return false; // "лΪXX" 7ַ + + string prefix = input.substr(0, 6); + if (toLower(prefix) != "лΪ") return false; + + string typeStr = trim(input.substr(6)); + UserType newType = parseUserType(typeStr); + + //лͲϷ + if (newType == UNKNOWN) { + cout << "Сѧк͸ѡеһ" << endl; + return true; // ʽȷݴ + } + + // лû + currentUser->type = newType; + questionGenerator.setUserType(newType); + cout << "׼" << getUserTypeString(newType) << "ѧĿĿ-1˳ǰûµ¼"; + + return true; + } + + // 벢֤Ŀ + int getValidQuestionCount() { + string input; + getline(cin, input); + input = trim(input); + + if (processSwitchCommand(input)) { + return -3; // ʾл + } + + if (input == "-1") { + return -1; // ˳ + } + + if (!isNumber(input)) { + cout << "Ч֣" << endl; + return -2; + } + + int questionCount = atoi(input.c_str()); + if (questionCount < 10 || questionCount > 30) { + cout << "ĿΧ10-30롣" << endl; + return -2; + } + + return questionCount; + } + + // ɲԾʷĿڲ + void generateAndSavePaper(int questionCount) { + vector paper = questionGenerator.generatePaper(questionCount, currentUser->username); + string filename = getCurrentTimeString() + ".txt"; + string userDir = currentDirectory + "/" + currentUser->username; + createDirectory(userDir); + string fullPath = userDir + "/" + filename; + savePaperToFile(paper, fullPath); + cout << "Ծɲ浽: " << fullPath << endl; + + // ʷĿ¼ļ¼أȷĿ + existingQuestions = loadExistingQuestions(userDir); + questionGenerator.setExistingQuestions(existingQuestions); + } + + // + void generatePaper() { + cout << "׼" << getUserTypeString(currentUser->type) + << "ѧĿĿ-1˳ǰûµ¼"; + + while (true) { + int questionCount = getValidQuestionCount(); + + if (questionCount == -3) { // л + continue; + } + + if (questionCount == -2) { // Ҫ + cout << "׼" << getUserTypeString(currentUser->type) + << "ѧĿĿ-1˳ǰûµ¼"; + continue; + } + + if (questionCount == -1) { // ˳ + currentUser = NULL; + return; + } + + // ЧĿ + generateAndSavePaper(questionCount); + break; + } + } + + //Ծļ - Ŀбдļ + void savePaperToFile(const vector& paper, const string& filename) { + ofstream file(filename.c_str()); + if (file.is_open()) { + for (size_t i = 0; i < paper.size(); i++) { + file << paper[i] << endl; + if (i < paper.size() - 1) { + file << endl; // Ŀ֮һ + } + } + file.close(); + } + } + + void userSession() { + while (currentUser != NULL) { + generatePaper(); + } + } + +public: + PaperSystem() : currentUser(NULL) { + currentDirectory = "papers"; + createDirectory(currentDirectory); + } + + //ϵͳѭ- + void run() { + cout << "=== СѧѧԶɳ ===" << endl; + + while (true) { + if (currentUser == NULL) { + if (!login()) { + continue; + } + } + + userSession(); + } + } +}; + +int main() { + PaperSystem system; + system.run(); + return 0; +} -- 2.34.1