commit
9626c903a3
@ -1,43 +1,43 @@
|
||||
#include "auth_manager.h"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <unordered_map>
|
||||
|
||||
AuthManager::AuthManager() {
|
||||
// 初始化预设的教师账户信息,包括小学、初中、高中各三个账号
|
||||
accounts_ = {
|
||||
{"张三1", {"123", "primary"}},
|
||||
{"张三2", {"123", "primary"}},
|
||||
{"张三3", {"123", "primary"}},
|
||||
{"李四1", {"123", "junior"}},
|
||||
{"李四2", {"123", "junior"}},
|
||||
{"李四3", {"123", "junior"}},
|
||||
{"王五1", {"123", "senior"}},
|
||||
{"王五2", {"123", "senior"}},
|
||||
{"王五3", {"123", "senior"}}
|
||||
};
|
||||
}
|
||||
|
||||
bool AuthManager::Authenticate(const std::string& username,
|
||||
const std::string& password,
|
||||
std::string* user_type) {
|
||||
// 验证输入不包含非法字符
|
||||
if (username.find(' ') != std::string::npos ||
|
||||
password.find(' ') != std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto it = accounts_.find(username);
|
||||
if (it != accounts_.end() && it->second.first == password) {
|
||||
*user_type = it->second.second;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AuthManager::IsValidUser(const std::string& username,
|
||||
const std::string& password) {
|
||||
std::string user_type;
|
||||
return Authenticate(username, password, &user_type);
|
||||
#include "auth_manager.h"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <unordered_map>
|
||||
|
||||
AuthManager::AuthManager() {
|
||||
// 初始化预设的教师账户信息,包括小学、初中、高中各三个账号
|
||||
accounts_ = {
|
||||
{"张三1", {"123", "primary"}},
|
||||
{"张三2", {"123", "primary"}},
|
||||
{"张三3", {"123", "primary"}},
|
||||
{"李四1", {"123", "junior"}},
|
||||
{"李四2", {"123", "junior"}},
|
||||
{"李四3", {"123", "junior"}},
|
||||
{"王五1", {"123", "senior"}},
|
||||
{"王五2", {"123", "senior"}},
|
||||
{"王五3", {"123", "senior"}}
|
||||
};
|
||||
}
|
||||
|
||||
bool AuthManager::Authenticate(const std::string& username,
|
||||
const std::string& password,
|
||||
std::string* user_type) {
|
||||
// 验证输入不包含非法字符
|
||||
if (username.find(' ') != std::string::npos ||
|
||||
password.find(' ') != std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto it = accounts_.find(username);
|
||||
if (it != accounts_.end() && it->second.first == password) {
|
||||
*user_type = it->second.second;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AuthManager::IsValidUser(const std::string& username,
|
||||
const std::string& password) {
|
||||
std::string user_type;
|
||||
return Authenticate(username, password, &user_type);
|
||||
}
|
||||
@ -1,76 +1,76 @@
|
||||
#include "junior_generator.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::string> JuniorGenerator::GenerateQuestions(int count,
|
||||
const std::string& username) {
|
||||
current_user_ = username;
|
||||
std::vector<std::string> questions;
|
||||
questions.reserve(count);
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
questions.push_back(GenerateSingleQuestion());
|
||||
}
|
||||
|
||||
return questions;
|
||||
}
|
||||
|
||||
std::string JuniorGenerator::GenerateSingleQuestion() {
|
||||
static const std::vector<char> kOperators = {'+', '-', '*', '/'};
|
||||
const int operand_count = GetRandomNumber(kMinOperands, kMaxOperands);
|
||||
|
||||
std::stringstream ss;
|
||||
bool has_special_operator = false;
|
||||
|
||||
// 第一个操作数可以是普通数字或特殊运算符
|
||||
if (operand_count == 1 || GetRandomNumber(0, 1)) {
|
||||
// 单操作数情况或第一个操作数是特殊运算符
|
||||
if (GetRandomNumber(0, 1)) {
|
||||
ss << "sqrt(" << GetRandomNumber(kMinNumber, kMaxNumber) << ")";
|
||||
} else {
|
||||
ss << GetRandomNumber(1, 10) << "^2";
|
||||
}
|
||||
has_special_operator = true;
|
||||
} else {
|
||||
// 第一个操作数是普通数字
|
||||
ss << GetRandomNumber(kMinNumber, kMaxNumber);
|
||||
}
|
||||
|
||||
// 添加后续操作数
|
||||
for (int i = 1; i < operand_count; ++i) {
|
||||
const char op = GetRandomOperator(kOperators);
|
||||
|
||||
// 随机决定是添加普通数字还是特殊运算符
|
||||
if (!has_special_operator || GetRandomNumber(0, 1)) {
|
||||
if (GetRandomNumber(0, 1)) {
|
||||
ss << " " << op << " sqrt(" << GetRandomNumber(kMinNumber, kMaxNumber) << ")";
|
||||
} else {
|
||||
ss << " " << op << " " << GetRandomNumber(1, 10) << "^2";
|
||||
}
|
||||
has_special_operator = true;
|
||||
} else {
|
||||
ss << " " << op << " " << GetRandomNumber(kMinNumber, kMaxNumber);
|
||||
}
|
||||
}
|
||||
|
||||
// 确保至少有一个特殊运算符
|
||||
if (!has_special_operator) {
|
||||
const char op = GetRandomOperator(kOperators);
|
||||
if (GetRandomNumber(0, 1)) {
|
||||
ss << " " << op << " sqrt(" << GetRandomNumber(kMinNumber, kMaxNumber) << ")";
|
||||
} else {
|
||||
ss << " " << op << " " << GetRandomNumber(1, 10) << "^2";
|
||||
}
|
||||
}
|
||||
|
||||
ss << " = ?";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
bool JuniorGenerator::MeetsDifficultyRequirements(const std::string& question) {
|
||||
// 使用ASCII字符而不是特殊Unicode字符
|
||||
return question.find("^2") != std::string::npos ||
|
||||
question.find("sqrt") != std::string::npos;
|
||||
#include "junior_generator.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::string> JuniorGenerator::GenerateQuestions(int count,
|
||||
const std::string& username) {
|
||||
current_user_ = username;
|
||||
std::vector<std::string> questions;
|
||||
questions.reserve(count);
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
questions.push_back(GenerateSingleQuestion());
|
||||
}
|
||||
|
||||
return questions;
|
||||
}
|
||||
|
||||
std::string JuniorGenerator::GenerateSingleQuestion() {
|
||||
static const std::vector<char> kOperators = {'+', '-', '*', '/'};
|
||||
const int operand_count = GetRandomNumber(kMinOperands, kMaxOperands);
|
||||
|
||||
std::stringstream ss;
|
||||
bool has_special_operator = false;
|
||||
|
||||
// 第一个操作数可以是普通数字或特殊运算符
|
||||
if (operand_count == 1 || GetRandomNumber(0, 1)) {
|
||||
// 单操作数情况或第一个操作数是特殊运算符
|
||||
if (GetRandomNumber(0, 1)) {
|
||||
ss << "sqrt(" << GetRandomNumber(kMinNumber, kMaxNumber) << ")";
|
||||
} else {
|
||||
ss << GetRandomNumber(1, 10) << "^2";
|
||||
}
|
||||
has_special_operator = true;
|
||||
} else {
|
||||
// 第一个操作数是普通数字
|
||||
ss << GetRandomNumber(kMinNumber, kMaxNumber);
|
||||
}
|
||||
|
||||
// 添加后续操作数
|
||||
for (int i = 1; i < operand_count; ++i) {
|
||||
const char op = GetRandomOperator(kOperators);
|
||||
|
||||
// 随机决定是添加普通数字还是特殊运算符
|
||||
if (!has_special_operator || GetRandomNumber(0, 1)) {
|
||||
if (GetRandomNumber(0, 1)) {
|
||||
ss << " " << op << " sqrt(" << GetRandomNumber(kMinNumber, kMaxNumber) << ")";
|
||||
} else {
|
||||
ss << " " << op << " " << GetRandomNumber(1, 10) << "^2";
|
||||
}
|
||||
has_special_operator = true;
|
||||
} else {
|
||||
ss << " " << op << " " << GetRandomNumber(kMinNumber, kMaxNumber);
|
||||
}
|
||||
}
|
||||
|
||||
// 确保至少有一个特殊运算符
|
||||
if (!has_special_operator) {
|
||||
const char op = GetRandomOperator(kOperators);
|
||||
if (GetRandomNumber(0, 1)) {
|
||||
ss << " " << op << " sqrt(" << GetRandomNumber(kMinNumber, kMaxNumber) << ")";
|
||||
} else {
|
||||
ss << " " << op << " " << GetRandomNumber(1, 10) << "^2";
|
||||
}
|
||||
}
|
||||
|
||||
ss << " = ?";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
bool JuniorGenerator::MeetsDifficultyRequirements(const std::string& question) {
|
||||
// 使用ASCII字符而不是特殊Unicode字符
|
||||
return question.find("^2") != std::string::npos ||
|
||||
question.find("sqrt") != std::string::npos;
|
||||
}
|
||||
@ -1,21 +1,21 @@
|
||||
#ifndef JUNIOR_GENERATOR_H_
|
||||
#define JUNIOR_GENERATOR_H_
|
||||
|
||||
#include "question_generator.h"
|
||||
|
||||
class JuniorGenerator : public QuestionGenerator {
|
||||
public:
|
||||
std::vector<std::string> GenerateQuestions(int count,
|
||||
const std::string& username) override;
|
||||
bool MeetsDifficultyRequirements(const std::string& question) override;
|
||||
|
||||
private:
|
||||
static constexpr int kMinOperands = 1;
|
||||
static constexpr int kMaxOperands = 5;
|
||||
static constexpr int kMinNumber = 1;
|
||||
static constexpr int kMaxNumber = 100;
|
||||
|
||||
std::string GenerateSingleQuestion();
|
||||
};
|
||||
|
||||
#ifndef JUNIOR_GENERATOR_H_
|
||||
#define JUNIOR_GENERATOR_H_
|
||||
|
||||
#include "question_generator.h"
|
||||
|
||||
class JuniorGenerator : public QuestionGenerator {
|
||||
public:
|
||||
std::vector<std::string> GenerateQuestions(int count,
|
||||
const std::string& username) override;
|
||||
bool MeetsDifficultyRequirements(const std::string& question) override;
|
||||
|
||||
private:
|
||||
static constexpr int kMinOperands = 1;
|
||||
static constexpr int kMaxOperands = 5;
|
||||
static constexpr int kMinNumber = 1;
|
||||
static constexpr int kMaxNumber = 100;
|
||||
|
||||
std::string GenerateSingleQuestion();
|
||||
};
|
||||
|
||||
#endif // JUNIOR_GENERATOR_H_
|
||||
@ -1,42 +1,42 @@
|
||||
#include "primary_generator.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::string> PrimaryGenerator::GenerateQuestions(int count,
|
||||
const std::string& username) {
|
||||
current_user_ = username;
|
||||
std::vector<std::string> questions;
|
||||
questions.reserve(count); // 预分配空间提高效率
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
questions.push_back(GenerateSingleQuestion());
|
||||
}
|
||||
|
||||
return questions;
|
||||
}
|
||||
|
||||
std::string PrimaryGenerator::GenerateSingleQuestion() {
|
||||
static const std::vector<char> kOperators = {'+', '-', '*', '/'};
|
||||
const int operand_count = GetRandomNumber(kMinOperands, kMaxOperands);
|
||||
|
||||
std::stringstream ss;
|
||||
ss << GetRandomNumber(kMinNumber, kMaxNumber); // 第一个操作数
|
||||
|
||||
for (int i = 1; i < operand_count; ++i) {
|
||||
const char op = GetRandomOperator(kOperators);
|
||||
ss << " " << op << " " << GetRandomNumber(kMinNumber, kMaxNumber);
|
||||
}
|
||||
|
||||
ss << " = ?";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
bool PrimaryGenerator::MeetsDifficultyRequirements(const std::string& question) {
|
||||
// 小学题目只需要包含基本运算符:+ - * /
|
||||
return question.find('+') != std::string::npos ||
|
||||
question.find('-') != std::string::npos ||
|
||||
question.find('*') != std::string::npos ||
|
||||
question.find('/') != std::string::npos;
|
||||
#include "primary_generator.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::string> PrimaryGenerator::GenerateQuestions(int count,
|
||||
const std::string& username) {
|
||||
current_user_ = username;
|
||||
std::vector<std::string> questions;
|
||||
questions.reserve(count); // 预分配空间提高效率
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
questions.push_back(GenerateSingleQuestion());
|
||||
}
|
||||
|
||||
return questions;
|
||||
}
|
||||
|
||||
std::string PrimaryGenerator::GenerateSingleQuestion() {
|
||||
static const std::vector<char> kOperators = {'+', '-', '*', '/'};
|
||||
const int operand_count = GetRandomNumber(kMinOperands, kMaxOperands);
|
||||
|
||||
std::stringstream ss;
|
||||
ss << GetRandomNumber(kMinNumber, kMaxNumber); // 第一个操作数
|
||||
|
||||
for (int i = 1; i < operand_count; ++i) {
|
||||
const char op = GetRandomOperator(kOperators);
|
||||
ss << " " << op << " " << GetRandomNumber(kMinNumber, kMaxNumber);
|
||||
}
|
||||
|
||||
ss << " = ?";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
bool PrimaryGenerator::MeetsDifficultyRequirements(const std::string& question) {
|
||||
// 小学题目只需要包含基本运算符:+ - * /
|
||||
return question.find('+') != std::string::npos ||
|
||||
question.find('-') != std::string::npos ||
|
||||
question.find('*') != std::string::npos ||
|
||||
question.find('/') != std::string::npos;
|
||||
}
|
||||
@ -1,21 +1,21 @@
|
||||
#ifndef PRIMARY_GENERATOR_H_
|
||||
#define PRIMARY_GENERATOR_H_
|
||||
|
||||
#include "question_generator.h"
|
||||
|
||||
class PrimaryGenerator : public QuestionGenerator {
|
||||
public:
|
||||
std::vector<std::string> GenerateQuestions(int count,
|
||||
const std::string& username) override;
|
||||
bool MeetsDifficultyRequirements(const std::string& question) override;
|
||||
|
||||
private:
|
||||
static constexpr int kMinOperands = 2;
|
||||
static constexpr int kMaxOperands = 5;
|
||||
static constexpr int kMinNumber = 1;
|
||||
static constexpr int kMaxNumber = 100;
|
||||
|
||||
std::string GenerateSingleQuestion();
|
||||
};
|
||||
|
||||
#ifndef PRIMARY_GENERATOR_H_
|
||||
#define PRIMARY_GENERATOR_H_
|
||||
|
||||
#include "question_generator.h"
|
||||
|
||||
class PrimaryGenerator : public QuestionGenerator {
|
||||
public:
|
||||
std::vector<std::string> GenerateQuestions(int count,
|
||||
const std::string& username) override;
|
||||
bool MeetsDifficultyRequirements(const std::string& question) override;
|
||||
|
||||
private:
|
||||
static constexpr int kMinOperands = 2;
|
||||
static constexpr int kMaxOperands = 5;
|
||||
static constexpr int kMinNumber = 1;
|
||||
static constexpr int kMaxNumber = 100;
|
||||
|
||||
std::string GenerateSingleQuestion();
|
||||
};
|
||||
|
||||
#endif // PRIMARY_GENERATOR_H_
|
||||
@ -1,19 +1,19 @@
|
||||
#include "question_generator.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <random>
|
||||
|
||||
QuestionGenerator::QuestionGenerator() {
|
||||
rng_.seed(std::chrono::steady_clock::now().time_since_epoch().count());
|
||||
}
|
||||
|
||||
int QuestionGenerator::GetRandomNumber(int min, int max) {
|
||||
std::uniform_int_distribution<int> dist(min, max);
|
||||
return dist(rng_);
|
||||
}
|
||||
|
||||
char QuestionGenerator::GetRandomOperator(const std::vector<char>& operators) {
|
||||
if (operators.empty()) return '+';
|
||||
std::uniform_int_distribution<int> dist(0, operators.size() - 1);
|
||||
return operators[dist(rng_)];
|
||||
#include "question_generator.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <random>
|
||||
|
||||
QuestionGenerator::QuestionGenerator() {
|
||||
rng_.seed(std::chrono::steady_clock::now().time_since_epoch().count());
|
||||
}
|
||||
|
||||
int QuestionGenerator::GetRandomNumber(int min, int max) {
|
||||
std::uniform_int_distribution<int> dist(min, max);
|
||||
return dist(rng_);
|
||||
}
|
||||
|
||||
char QuestionGenerator::GetRandomOperator(const std::vector<char>& operators) {
|
||||
if (operators.empty()) return '+';
|
||||
std::uniform_int_distribution<int> dist(0, operators.size() - 1);
|
||||
return operators[dist(rng_)];
|
||||
}
|
||||
@ -1,37 +1,37 @@
|
||||
#ifndef QUESTION_GENERATOR_H_
|
||||
#define QUESTION_GENERATOR_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <random>
|
||||
|
||||
class QuestionGenerator {
|
||||
public:
|
||||
virtual ~QuestionGenerator() = default;
|
||||
|
||||
// 生成指定数量的数学题目
|
||||
virtual std::vector<std::string> GenerateQuestions(int count,
|
||||
const std::string& username) = 0;
|
||||
|
||||
// 检查题目是否符合难度要求
|
||||
virtual bool MeetsDifficultyRequirements(const std::string& question) = 0;
|
||||
|
||||
protected:
|
||||
QuestionGenerator();
|
||||
|
||||
// 生成指定范围内的随机数
|
||||
int GetRandomNumber(int min, int max);
|
||||
|
||||
// 从运算符列表中随机选择一个运算符
|
||||
char GetRandomOperator(const std::vector<char>& operators);
|
||||
|
||||
std::string current_user_;
|
||||
std::mt19937 rng_;
|
||||
|
||||
private:
|
||||
// 禁用拷贝构造和赋值操作
|
||||
QuestionGenerator(const QuestionGenerator&) = delete;
|
||||
QuestionGenerator& operator=(const QuestionGenerator&) = delete;
|
||||
};
|
||||
|
||||
#ifndef QUESTION_GENERATOR_H_
|
||||
#define QUESTION_GENERATOR_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <random>
|
||||
|
||||
class QuestionGenerator {
|
||||
public:
|
||||
virtual ~QuestionGenerator() = default;
|
||||
|
||||
// 生成指定数量的数学题目
|
||||
virtual std::vector<std::string> GenerateQuestions(int count,
|
||||
const std::string& username) = 0;
|
||||
|
||||
// 检查题目是否符合难度要求
|
||||
virtual bool MeetsDifficultyRequirements(const std::string& question) = 0;
|
||||
|
||||
protected:
|
||||
QuestionGenerator();
|
||||
|
||||
// 生成指定范围内的随机数
|
||||
int GetRandomNumber(int min, int max);
|
||||
|
||||
// 从运算符列表中随机选择一个运算符
|
||||
char GetRandomOperator(const std::vector<char>& operators);
|
||||
|
||||
std::string current_user_;
|
||||
std::mt19937 rng_;
|
||||
|
||||
private:
|
||||
// 禁用拷贝构造和赋值操作
|
||||
QuestionGenerator(const QuestionGenerator&) = delete;
|
||||
QuestionGenerator& operator=(const QuestionGenerator&) = delete;
|
||||
};
|
||||
|
||||
#endif // QUESTION_GENERATOR_H_
|
||||
@ -1,72 +1,72 @@
|
||||
#include "senior_generator.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::string> SeniorGenerator::GenerateQuestions(int count,
|
||||
const std::string& username) {
|
||||
current_user_ = username;
|
||||
std::vector<std::string> questions;
|
||||
questions.reserve(count);
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
questions.push_back(GenerateSingleQuestion());
|
||||
}
|
||||
|
||||
return questions;
|
||||
}
|
||||
|
||||
std::string SeniorGenerator::GenerateSingleQuestion() {
|
||||
static const std::vector<char> kOperators = {'+', '-', '*', '/'};
|
||||
static const std::vector<std::string> kTrigFunctions = {"sin", "cos", "tan"};
|
||||
const int operand_count = GetRandomNumber(kMinOperands, kMaxOperands);
|
||||
|
||||
std::stringstream ss;
|
||||
bool has_trig_function = false;
|
||||
|
||||
// 第一个操作数可以是普通数字或三角函数
|
||||
if (operand_count == 1 || GetRandomNumber(0, 1)) {
|
||||
// 单操作数情况或第一个操作数是三角函数
|
||||
const int trig_index = GetRandomNumber(0, kTrigFunctions.size() - 1);
|
||||
ss << kTrigFunctions[trig_index] << "("
|
||||
<< GetRandomNumber(kMinNumber, kMaxAngle) << "°)";
|
||||
has_trig_function = true;
|
||||
} else {
|
||||
// 第一个操作数是普通数字
|
||||
ss << GetRandomNumber(kMinNumber, kMaxNumber);
|
||||
}
|
||||
|
||||
// 添加后续操作数
|
||||
for (int i = 1; i < operand_count; ++i) {
|
||||
const char op = GetRandomOperator(kOperators);
|
||||
|
||||
// 随机决定是添加普通数字还是三角函数
|
||||
if (!has_trig_function || GetRandomNumber(0, 1)) {
|
||||
const int trig_index = GetRandomNumber(0, kTrigFunctions.size() - 1);
|
||||
ss << " " << op << " " << kTrigFunctions[trig_index] << "("
|
||||
<< GetRandomNumber(kMinNumber, kMaxAngle) << "°)";
|
||||
has_trig_function = true;
|
||||
} else {
|
||||
ss << " " << op << " " << GetRandomNumber(kMinNumber, kMaxNumber);
|
||||
}
|
||||
}
|
||||
|
||||
// 确保至少有一个三角函数
|
||||
if (!has_trig_function) {
|
||||
const char op = GetRandomOperator(kOperators);
|
||||
const int trig_index = GetRandomNumber(0, kTrigFunctions.size() - 1);
|
||||
ss << " " << op << " " << kTrigFunctions[trig_index] << "("
|
||||
<< GetRandomNumber(kMinNumber, kMaxAngle) << "°)";
|
||||
}
|
||||
|
||||
ss << " = ?";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
bool SeniorGenerator::MeetsDifficultyRequirements(const std::string& question) {
|
||||
// 高中题目至少包含一个三角函数
|
||||
return question.find("sin") != std::string::npos ||
|
||||
question.find("cos") != std::string::npos ||
|
||||
question.find("tan") != std::string::npos;
|
||||
#include "senior_generator.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::string> SeniorGenerator::GenerateQuestions(int count,
|
||||
const std::string& username) {
|
||||
current_user_ = username;
|
||||
std::vector<std::string> questions;
|
||||
questions.reserve(count);
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
questions.push_back(GenerateSingleQuestion());
|
||||
}
|
||||
|
||||
return questions;
|
||||
}
|
||||
|
||||
std::string SeniorGenerator::GenerateSingleQuestion() {
|
||||
static const std::vector<char> kOperators = {'+', '-', '*', '/'};
|
||||
static const std::vector<std::string> kTrigFunctions = {"sin", "cos", "tan"};
|
||||
const int operand_count = GetRandomNumber(kMinOperands, kMaxOperands);
|
||||
|
||||
std::stringstream ss;
|
||||
bool has_trig_function = false;
|
||||
|
||||
// 第一个操作数可以是普通数字或三角函数
|
||||
if (operand_count == 1 || GetRandomNumber(0, 1)) {
|
||||
// 单操作数情况或第一个操作数是三角函数
|
||||
const int trig_index = GetRandomNumber(0, kTrigFunctions.size() - 1);
|
||||
ss << kTrigFunctions[trig_index] << "("
|
||||
<< GetRandomNumber(kMinNumber, kMaxAngle) << "°)";
|
||||
has_trig_function = true;
|
||||
} else {
|
||||
// 第一个操作数是普通数字
|
||||
ss << GetRandomNumber(kMinNumber, kMaxNumber);
|
||||
}
|
||||
|
||||
// 添加后续操作数
|
||||
for (int i = 1; i < operand_count; ++i) {
|
||||
const char op = GetRandomOperator(kOperators);
|
||||
|
||||
// 随机决定是添加普通数字还是三角函数
|
||||
if (!has_trig_function || GetRandomNumber(0, 1)) {
|
||||
const int trig_index = GetRandomNumber(0, kTrigFunctions.size() - 1);
|
||||
ss << " " << op << " " << kTrigFunctions[trig_index] << "("
|
||||
<< GetRandomNumber(kMinNumber, kMaxAngle) << "°)";
|
||||
has_trig_function = true;
|
||||
} else {
|
||||
ss << " " << op << " " << GetRandomNumber(kMinNumber, kMaxNumber);
|
||||
}
|
||||
}
|
||||
|
||||
// 确保至少有一个三角函数
|
||||
if (!has_trig_function) {
|
||||
const char op = GetRandomOperator(kOperators);
|
||||
const int trig_index = GetRandomNumber(0, kTrigFunctions.size() - 1);
|
||||
ss << " " << op << " " << kTrigFunctions[trig_index] << "("
|
||||
<< GetRandomNumber(kMinNumber, kMaxAngle) << "°)";
|
||||
}
|
||||
|
||||
ss << " = ?";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
bool SeniorGenerator::MeetsDifficultyRequirements(const std::string& question) {
|
||||
// 高中题目至少包含一个三角函数
|
||||
return question.find("sin") != std::string::npos ||
|
||||
question.find("cos") != std::string::npos ||
|
||||
question.find("tan") != std::string::npos;
|
||||
}
|
||||
@ -1,22 +1,22 @@
|
||||
#ifndef SENIOR_GENERATOR_H_
|
||||
#define SENIOR_GENERATOR_H_
|
||||
|
||||
#include "question_generator.h"
|
||||
|
||||
class SeniorGenerator : public QuestionGenerator {
|
||||
public:
|
||||
std::vector<std::string> GenerateQuestions(int count,
|
||||
const std::string& username) override;
|
||||
bool MeetsDifficultyRequirements(const std::string& question) override;
|
||||
|
||||
private:
|
||||
static constexpr int kMinOperands = 1;
|
||||
static constexpr int kMaxOperands = 5;
|
||||
static constexpr int kMinNumber = 1;
|
||||
static constexpr int kMaxNumber = 100;
|
||||
static constexpr int kMaxAngle = 90;
|
||||
|
||||
std::string GenerateSingleQuestion();
|
||||
};
|
||||
|
||||
#ifndef SENIOR_GENERATOR_H_
|
||||
#define SENIOR_GENERATOR_H_
|
||||
|
||||
#include "question_generator.h"
|
||||
|
||||
class SeniorGenerator : public QuestionGenerator {
|
||||
public:
|
||||
std::vector<std::string> GenerateQuestions(int count,
|
||||
const std::string& username) override;
|
||||
bool MeetsDifficultyRequirements(const std::string& question) override;
|
||||
|
||||
private:
|
||||
static constexpr int kMinOperands = 1;
|
||||
static constexpr int kMaxOperands = 5;
|
||||
static constexpr int kMinNumber = 1;
|
||||
static constexpr int kMaxNumber = 100;
|
||||
static constexpr int kMaxAngle = 90;
|
||||
|
||||
std::string GenerateSingleQuestion();
|
||||
};
|
||||
|
||||
#endif // SENIOR_GENERATOR_H_
|
||||
@ -1,40 +1,40 @@
|
||||
#include "session_manager.h"
|
||||
|
||||
SessionManager::SessionManager()
|
||||
: current_user_(""),
|
||||
current_difficulty_(""),
|
||||
current_generator_(nullptr) {}
|
||||
|
||||
void SessionManager::SetUser(const std::string& username,
|
||||
const std::string& difficulty) {
|
||||
current_user_ = username;
|
||||
SwitchDifficulty(difficulty);
|
||||
}
|
||||
|
||||
bool SessionManager::SwitchDifficulty(const std::string& difficulty) {
|
||||
if (difficulty == "primary") {
|
||||
current_generator_ = &primary_generator_;
|
||||
current_difficulty_ = "小学";
|
||||
} else if (difficulty == "junior") {
|
||||
current_generator_ = &junior_generator_;
|
||||
current_difficulty_ = "初中";
|
||||
} else if (difficulty == "senior") {
|
||||
current_generator_ = &senior_generator_;
|
||||
current_difficulty_ = "高中";
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
QuestionGenerator* SessionManager::GetCurrentGenerator() {
|
||||
return current_generator_;
|
||||
}
|
||||
|
||||
std::string SessionManager::GetCurrentDifficulty() const {
|
||||
return current_difficulty_;
|
||||
}
|
||||
|
||||
std::string SessionManager::GetCurrentUser() const {
|
||||
return current_user_;
|
||||
#include "session_manager.h"
|
||||
|
||||
SessionManager::SessionManager()
|
||||
: current_user_(""),
|
||||
current_difficulty_(""),
|
||||
current_generator_(nullptr) {}
|
||||
|
||||
void SessionManager::SetUser(const std::string& username,
|
||||
const std::string& difficulty) {
|
||||
current_user_ = username;
|
||||
SwitchDifficulty(difficulty);
|
||||
}
|
||||
|
||||
bool SessionManager::SwitchDifficulty(const std::string& difficulty) {
|
||||
if (difficulty == "primary") {
|
||||
current_generator_ = &primary_generator_;
|
||||
current_difficulty_ = "小学";
|
||||
} else if (difficulty == "junior") {
|
||||
current_generator_ = &junior_generator_;
|
||||
current_difficulty_ = "初中";
|
||||
} else if (difficulty == "senior") {
|
||||
current_generator_ = &senior_generator_;
|
||||
current_difficulty_ = "高中";
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
QuestionGenerator* SessionManager::GetCurrentGenerator() {
|
||||
return current_generator_;
|
||||
}
|
||||
|
||||
std::string SessionManager::GetCurrentDifficulty() const {
|
||||
return current_difficulty_;
|
||||
}
|
||||
|
||||
std::string SessionManager::GetCurrentUser() const {
|
||||
return current_user_;
|
||||
}
|
||||
@ -1,43 +1,43 @@
|
||||
#ifndef SESSION_MANAGER_H_
|
||||
#define SESSION_MANAGER_H_
|
||||
|
||||
#include <string>
|
||||
#include "question_generator.h"
|
||||
#include "primary_generator.h"
|
||||
#include "junior_generator.h"
|
||||
#include "senior_generator.h"
|
||||
|
||||
class SessionManager {
|
||||
public:
|
||||
SessionManager();
|
||||
|
||||
// 设置当前用户和难度
|
||||
void SetUser(const std::string& username, const std::string& difficulty);
|
||||
|
||||
// 切换难度级别
|
||||
bool SwitchDifficulty(const std::string& difficulty);
|
||||
|
||||
// 获取当前题目生成器
|
||||
QuestionGenerator* GetCurrentGenerator();
|
||||
|
||||
// 获取当前难度描述
|
||||
std::string GetCurrentDifficulty() const;
|
||||
|
||||
// 获取当前用户名
|
||||
std::string GetCurrentUser() const;
|
||||
|
||||
private:
|
||||
std::string current_user_;
|
||||
std::string current_difficulty_;
|
||||
QuestionGenerator* current_generator_;
|
||||
|
||||
PrimaryGenerator primary_generator_;
|
||||
JuniorGenerator junior_generator_;
|
||||
SeniorGenerator senior_generator_;
|
||||
|
||||
// 禁用拷贝构造和赋值操作
|
||||
SessionManager(const SessionManager&) = delete;
|
||||
SessionManager& operator=(const SessionManager&) = delete;
|
||||
};
|
||||
|
||||
#ifndef SESSION_MANAGER_H_
|
||||
#define SESSION_MANAGER_H_
|
||||
|
||||
#include <string>
|
||||
#include "question_generator.h"
|
||||
#include "primary_generator.h"
|
||||
#include "junior_generator.h"
|
||||
#include "senior_generator.h"
|
||||
|
||||
class SessionManager {
|
||||
public:
|
||||
SessionManager();
|
||||
|
||||
// 设置当前用户和难度
|
||||
void SetUser(const std::string& username, const std::string& difficulty);
|
||||
|
||||
// 切换难度级别
|
||||
bool SwitchDifficulty(const std::string& difficulty);
|
||||
|
||||
// 获取当前题目生成器
|
||||
QuestionGenerator* GetCurrentGenerator();
|
||||
|
||||
// 获取当前难度描述
|
||||
std::string GetCurrentDifficulty() const;
|
||||
|
||||
// 获取当前用户名
|
||||
std::string GetCurrentUser() const;
|
||||
|
||||
private:
|
||||
std::string current_user_;
|
||||
std::string current_difficulty_;
|
||||
QuestionGenerator* current_generator_;
|
||||
|
||||
PrimaryGenerator primary_generator_;
|
||||
JuniorGenerator junior_generator_;
|
||||
SeniorGenerator senior_generator_;
|
||||
|
||||
// 禁用拷贝构造和赋值操作
|
||||
SessionManager(const SessionManager&) = delete;
|
||||
SessionManager& operator=(const SessionManager&) = delete;
|
||||
};
|
||||
|
||||
#endif // SESSION_MANAGER_H_
|
||||
Loading…
Reference in new issue