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