d #4

Closed
hnu202326010324 wants to merge 6 commits from develop into main

@ -0,0 +1,2 @@
# smath

@ -0,0 +1,164 @@
#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;
}
}
}

@ -0,0 +1,31 @@
#ifndef HANSHU_H
#define HANSHU_H
#include "question.h"
#include <string>
#include <vector>
// 用户认证函数
User* AuthenticateUser(const std::string& username, const std::string& password);
bool IsValidUserType(const std::string& type);
// 文件操作函数
void SaveQuestionsToFile(const std::vector<std::string>& questions,
const std::string& username,
const std::string& type);
std::string GetCurrentTimestamp();
void CreateUserDirectory(const std::string& username);
// 输入验证函数
bool IsValidQuestionCount(int count);
bool IsSwitchCommand(const std::string& input, std::string& newtype);
// 系统流程函数
void DisplayWelcomeMessage();
void DisplayQuestionGenerator(std::unique_ptr<QuestionGenerator> generator,const User* users);
void DisplayCurrentMode(const std::string& type);
void DisplayQuestionExamples(const std::vector<std::string>& questions);
#endif // HANSHU_H

@ -1,9 +1,34 @@
#include "question.h"
#include "hanshu.h"
#include <iostream>
#include <memory>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
int main() {
while(1){
DisplayWelcomeMessage();
// 登录循环
User* currentuser = nullptr;
while (!currentuser) {
cout << "请输入用户名和密码(用空格隔开):";
string username, password;
cin >> username >> password;
currentuser = AuthenticateUser(username, password);
if (!currentuser) {
cout << "请输入正确的用户名、密码" << endl;
}
}
// 创建题目生成器
auto generator = CreateQuestionGenerator(currentuser->usertype);
DisplayCurrentMode(currentuser->usertype);
DisplayQuestionGenerator(std::move(generator),currentuser);
}
return 0;
}

@ -0,0 +1,263 @@
#include "question.h"
#include <random>
#include <stack>
#include <algorithm>
#include <sstream>
using namespace std;
// QuestionGenerator 基类实现
vector<string> QuestionGenerator::GenerateQuestion(int count) {
vector<string> questions;
int attempts = 0;
const int max_attempts = count * 10;
while (questions.size() < count && attempts < max_attempts) {
string question = GenerateLogic();
if (IsQuestionUnique(question)) {
questions.push_back(question);
existingqustions.insert(question);
}
attempts++;
}
return questions;
}
int QuestionGenerator::GetRand(int min, int max) {
static random_device rd;
static mt19937 gen(rd());
uniform_int_distribution<> dis(min, max);
return dis(gen);
}
char QuestionGenerator::GetOperator() {
char operators[] = {'+', '-', '*', '/'};
return operators[GetRand(0, 3)];
}
bool QuestionGenerator::IsQuestionUnique(const string& question) const {
return existingqustions.find(question) == existingqustions.end();
}
void QuestionGenerator::ClearQuestions() {
existingqustions.clear();
}
// PrimaryQuestionGenerator 实现
string PrimaryQuestionGenerator::GenerateLogic() {
int operands = GetRand(2, 5);
// 50%概率使用带括号的表达式
if (GetRand(0, 1) == 1 && operands >= 3) {
return GenerateWithParentheses(operands);
}
// 生成基础表达式
stringstream ss;
for (int i = 0; i < operands; i++) {
ss << GetRand(1, 100);
if (i < operands - 1) {
ss << " " << GetOperator() << " ";
}
}
ss << " = ";
return ss.str();
}
string PrimaryQuestionGenerator::GetType() const {
return "小学";
}
string PrimaryQuestionGenerator::ParenthesesCaceOne(int operands,
const vector<int>& numbers) {
stringstream ss;
ss << "(" << numbers[0] << " " << GetOperator() << " " << numbers[1] << ")";
for (int i = 2; i < operands; i++) {
ss << " " << GetOperator() << " " << numbers[i];
}
ss << " = ";
return ss.str();
}
string PrimaryQuestionGenerator::ParenthesesCaceTwo(int operands,
const vector<int>& numbers) {
stringstream ss;
ss << numbers[0] << " " << GetOperator() << " ";
ss << "(" << numbers[1] << " " << GetOperator() << " " << numbers[2] << ")";
for (int i = 3; i < operands; i++) {
ss << " " << GetOperator() << " " << numbers[i];
}
ss << " = ";
return ss.str();
}
string PrimaryQuestionGenerator::ParenthesesCaceThree(int operands,
const vector<int>& numbers) {
stringstream ss;
if (operands >= 4) {
int mid = operands / 2;
ss << "(";
for (int i = 0; i < mid; i++) {
if (i > 0) ss << " " << GetOperator() << " ";
ss << numbers[i];
}
ss << ") " << GetOperator() << " (";
for (int i = mid; i < operands; i++) {
if (i > mid) ss << " " << GetOperator() << " ";
ss << numbers[i];
}
ss << ")";
} else {
ss << "(" << numbers[0] << " " << GetOperator() << " " << numbers[1] << ")";
for (int i = 2; i < operands; i++) {
ss << " " << GetOperator() << " " << numbers[i];
}
}
ss << " = ";
return ss.str();
}
string PrimaryQuestionGenerator::GenerateWithParentheses(int operands) {
stringstream ss;
vector<int> numbers;
string result;
for (int i = 0; i < operands; i++) {
numbers.push_back(GetRand(1, 100));
}
int parenthesisType = GetRand(1, 3);
switch (parenthesisType) {
case 1:
result = ParenthesesCaceOne(operands,numbers);
break;
case 2:
result = ParenthesesCaceTwo(operands,numbers);
break;
case 3:
result = ParenthesesCaceThree(operands,numbers);
break;
}
if (!IsValidParentheses(result)) {
return GenerateLogic(); // 重新生成
}
return result;
}
bool PrimaryQuestionGenerator::IsValidParentheses(const string& expr) const {
stack<char> st;
for (char c : expr) {
if (c == '(') st.push(c);
else if (c == ')') {
if (st.empty()) return false;
st.pop();
}
}
return st.empty();
}
// JuniorQuestionGenerator 实现
string JuniorQuestionGenerator::GenerateLogic() {
int operands = GetRand(1, 5);
stringstream ss;
for (int i = 0; i < operands; i++) {
if(i==0){
if (GetRand(0, 1) == 1) {
ss << GetRand(1, 20) << "^2";
} else {
ss << "" << GetRand(1, 100);
}
if (i < operands - 1) {
ss << " " << GetOperator() << " ";
}
continue;
}
// 30%概率使用平方或开方
if (GetRand(0, 2) == 0) {
if (GetRand(0, 1) == 1) {
ss << GetRand(1, 20) << "^2";
} else {
ss << "" << GetRand(1, 100);
}
} else {
ss << GetRand(1, 100);
}
if (i < operands - 1) {
ss << " " << GetOperator() << " ";
}
}
ss << " = ";
return ss.str();
}
string JuniorQuestionGenerator::GetType() const {
return "初中";
}
// SeniorQuestionGenerator 实现
string SeniorQuestionGenerator::GenerateLogic() {
int operands = GetRand(1, 5);
stringstream ss;
for (int i = 0; i < operands; i++) {
if(i==0){
string trigFuncs[] = {"sin", "cos", "tan"};
string func = trigFuncs[GetRand(0, 2)];
ss << func << "(" << GetRand(1, 90) << ")";
if (i < operands - 1) {
ss << " " << GetOperator() << " ";
}
continue;
}
// 随机决定是否添加三角函数
if (GetRand(0, 2) == 0 ) {
string trigFuncs[] = {"sin", "cos", "tan"};
string func = trigFuncs[GetRand(0, 2)];
ss << func << "(" << GetRand(1, 90) << ")";
}
else{
ss << GetRand(1, 100);
}
if (i < operands - 1) {
ss << " " << GetOperator() << " ";
}
}
ss << " = ";
return ss.str();
}
string SeniorQuestionGenerator::GetType() const {
return "高中";
}
// 工厂函数实现
unique_ptr<QuestionGenerator> CreateQuestionGenerator(const std::string& type) {
if (type == "小学") {
return make_unique<PrimaryQuestionGenerator>();
} else if (type == "初中") {
return make_unique<JuniorQuestionGenerator>();
} else if (type == "高中") {
return make_unique<SeniorQuestionGenerator>();
}
return nullptr;
}

@ -0,0 +1,61 @@
#ifndef QUESTION_H
#define QUESTION_H
#include<set>
#include<string>
#include<vector>
#include<memory>
struct User {
std::string username;
std::string password;
std::string usertype;
};
//抽象基类
class QuestionGenerator{
protected:
std::set<std::string> existingqustions;//已生成的问题
public:
virtual std::string GenerateLogic()=0;
virtual ~QuestionGenerator() = default;
virtual std::vector<std::string> GenerateQuestion(int count);//生成问题
virtual std::string GetType() const = 0;
int GetRand(int min,int max);//产生随机数字
char GetOperator();
bool IsQuestionUnique(const std::string& question) const;
void ClearQuestions();
};
//小学题目生成器
class PrimaryQuestionGenerator : public QuestionGenerator {
public:
std::string GenerateLogic() override;
std::string GetType() const override;
private:
std::string GenerateWithParentheses(int operands);
bool IsValidParentheses(const std::string& expr) const;
std::string ParenthesesCaceOne(int operands, const std::vector<int>& numbers);
std::string ParenthesesCaceTwo(int operands, const std::vector<int>& numbers);
std::string ParenthesesCaceThree(int operands, const std::vector<int>& numbers);
};
// 初中题目生成器
class JuniorQuestionGenerator : public QuestionGenerator {
public:
std::string GenerateLogic() override;
std::string GetType() const override;
};
// 高中题目生成器
class SeniorQuestionGenerator : public QuestionGenerator {
public:
std::string GenerateLogic() override;
std::string GetType() const override;
};
// 工厂函数声明
std::unique_ptr<QuestionGenerator> CreateQuestionGenerator(const std::string& type);
#endif // QUESTION_H
Loading…
Cancel
Save