-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
150 lines (127 loc) · 4.32 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <ctime>
#include <string>
#include <vector>
#include <iomanip>
class Question{
private :
size_t m_question{};
std::string m_conv{};
std::string m_input{};
inline static size_t m_score{};
public :
Question(const size_t& radix, const size_t& range){
random_gen(range);
convert(radix);
std::cout << "What is dec " << m_question << " in a system with radix " << radix << "? ";
std::cin >> m_input;
score();
}
void score(){
if (m_conv == m_input){
++m_score;
std::cout << "Correct!" << std::endl;
} else {
std::cout << "Wrong answer! " << m_question << " = " << m_conv << std::endl;
}
}
static void print_score(const size_t& iterations){
std::cout << "You scored : " << m_score << " / " << iterations << std::endl;
}
static void print_info(const Question& q){
std::cout << std::right << std::setw(3) << q.m_question << " = " << std::left << std::setw(3) << q.m_conv << ". Your answer : " << q.m_input << std::endl;
}
static void reset_score(){
if (m_score != 0){
m_score = 0;
}
}
void random_gen(const size_t& range){
m_question = static_cast<size_t>((std::rand() % range));
}
void convert(const size_t& radix){
std::vector<int> a;
size_t n {m_question};
while (n > 0){
size_t remainder {n % radix};
n = n / radix;
a.emplace(a.begin(), remainder);
}
//Padding with 0 if needed
if (std::size(a) == 0){
a.emplace(a.begin(), 0);
}
for (auto i{a.begin()}; i != a.end(); ++i){
switch (*i){
case 10 :{
m_conv += 'A';
}
break;
case 11 :{
m_conv += 'B';
}
break;
case 12 :{
m_conv += 'C';
}
break;
case 13 :{
m_conv += 'D';
}
break;
case 14 :{
m_conv += 'E';
}
break;
case 15 :{
m_conv += 'F';
}
break;
default :{
m_conv += std::to_string(*i);
}
break;
}
}
}
};
int main(){
std::srand(std::time(0)); //Unique seed for the random number generator
size_t range;
size_t iterations;
size_t radix;
std::vector<Question> questions;
bool end {false};
while (end == false){
std::cout << "Enter radix, max is 16 (e.g. '16' for hex, '8' for oct, '2' for bin etc...) : ";
std::cin >> radix;
std::cout << "Enter max dec range for the random number generator (e.g. '256' for two positions in hex) : ";
std::cin >> range;
std::cout << "Enter the number of questions : ";
std::cin >> iterations;
//Delete leftovers before a new attempt
if (std::size(questions) != 0){
questions.clear();
}
//Reset score to '0' before a new attempt
Question::reset_score();
for (size_t i{}; i < iterations; ++i){
std::cout << i + 1 << " : ";
Question q(radix, range);
questions.push_back(q);
}
std::cout << "-------------------------------" << std::endl;
for (size_t i{}; i < iterations; ++i){
std::cout << i + 1 << " : ";
Question::print_info(questions[i]);
}
std::cout << "-------------------------------" << std::endl;
Question::print_score(iterations);
std::cout << "Try again? (y/n) : ";
char go_on;
std::cin >> go_on;
end = ((go_on == 'Y') || (go_on == 'y')) ? false : true;
}
std::cout << "Goodbye!";
return 0;
}