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.
42 lines
1.3 KiB
42 lines
1.3 KiB
#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;
|
|
} |