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.
72 lines
2.5 KiB
72 lines
2.5 KiB
#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;
|
|
} |