23 using OpsC = std::array<Ops, 4>;
41 std::random_device rd;
44 std::default_random_engine g(rd());
45 std::shuffle(_data.begin(), _data.end(), g);
58 _eIdx = (_eIdx == _data.size()) ? 0 : _eIdx + 1;
67 std::random_device rd;
70 std::default_random_engine g(rd());
71 std::shuffle(_types.begin(), _types.end(), g);
72 _tIdx = _types.size();
81 _tIdx = (_tIdx == _types.size()) ? 0 : _tIdx + 1;
115 return std::make_tuple(next_item(), next_item(), next_op());
131 auto r1 = std::get<0>(q);
133 auto r2 = std::get<1>(q);
137 switch (std::get<2>(q)) {
168 static const std::array<std::string, 4>
ops;
182 , _maxScore{ maxq } {}
195 std::cout << to_string(q) <<
"=?" << std::endl;
198 getline(std::cin, answer);
200 auto cresult = Test::calc_result(q);
201 auto uresult =
read(answer);
202 if (cresult == uresult) {
203 std::cout <<
"OK" << std::endl;
206 std::cout <<
"correct answer is:" 207 << cresult << std::endl;
209 }
catch (
const std::out_of_range &err) {
210 std::cout <<
"wrong input format" << std::endl;
211 std::cout <<
"input must be like a/b" << std::endl;
212 std::cout << err.what() << std::endl;
213 }
catch (
const std::invalid_argument &err) {
214 std::cout <<
"wrong input format" << std::endl;
215 std::cout <<
"input must be like a/b" << std::endl;
216 std::cout << err.what() << std::endl;
217 }
catch (
const std::runtime_error &err) {
218 std::cout <<
"wrong input format" << std::endl;
219 std::cout <<
"input must be like a/b" << std::endl;
220 std::cout << err.what() << std::endl;
224 const auto score =
static_cast<double>(_score) / static_cast<double>(_maxScore) * 100;
225 std::cout <<
"score:" << score <<
"%" << std::endl;
233 std::stringstream sstream;
235 sstream << std::get<0>(q)
236 << ops[static_cast<size_t>(std::get<2>(q))]
238 return sstream.str();
249 long long num = std::stoll(answer, &l);
251 if (l == answer.size() || answer.at(l) !=
'/') {
252 throw std::runtime_error(
"expected /");
255 auto part2 = answer.substr(l + 1);
257 long long den = std::stoll(part2, &l, 10);
258 if (l != part2.size()) {
259 throw std::runtime_error(
"syntax error");
268 inline const std::array<std::string, 4>
TestUI::ops{
"+",
"-",
"*",
"/" };
276 auto fn = [](
long long lmaxden) {
277 std::vector<TestUI::Rational> data;
278 for (
long long idx = 1; idx < lmaxden; idx++) {
279 for (
long long jdx = idx + 1; jdx < lmaxden; jdx++) {
280 data.push_back({ idx, jdx });
void run()
run Interaktion mit dem Benutzer
TestGenerator(size_t maxq, const DataC &data)
TestGenerator Konstruktor.
Rational read(const std::string &answer)
read Lese Antwort des Benutzers
The TestUI class Test mit rationalen Zahlen Benutzerschnittstelle.
bool has_questions()
has_questions
auto next_question()
next_question generiere eine Frage
std::vector< Rational > DataC
std::tuple< Rational, Rational, Ops > Question
Rational Klasse für rationale Zahlen.
std::array< Ops, 4 > OpsC
static std::string to_string(const Test::Question &q)
to_string Umwandlung Frage in Zeichenkette
void ex1()
cppx3 Beispiel Test mit rationalen Zahlen
TestUI(size_t maxq, const Test::DataC &data)
TestUI Konstruktor.
static T calc_result(const Question &q)
get_result berechne korrekte Antwort
double read(const char *label)
static const std::array< std::string, 4 > ops
The TestGenerator class Erzeugt Fragen (Aufgaben) zum Rechnen mit Zahlen. T kann eine ganze eine rati...