parent
df6c978e57
commit
3c7ba3c8bb
@ -1,42 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
Loading…
Reference in new issue