Repository navigation
Expand file tree
/
Copy pathrandom_code_picker.cpp
More file actions
110 lines (87 loc) · 2.33 KB
/
Copy pathrandom_code_picker.cpp
File metadata and controls
110 lines (87 loc) · 2.33 KB
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
// random code picker (attempted but not fully coded by me/KAH)
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const int MAX_TASKS = 500;
unsigned int mix(unsigned int x) {
if (x == 0) x = 0x9E3779B9u;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return x;
}
string trimLine(const string &line) {
size_t start = 0;
while (start < line.length() &&
(line[start] == ' ' || line[start] == '\t')) {
start++;
}
size_t end = line.length();
while (end > start &&
(line[end - 1] == '\n' || line[end - 1] == '\r' ||
line[end - 1] == ' ' || line[end - 1] == '\t')) {
end--;
}
return line.substr(start, end - start);
}
bool isTaskLine(const string &line) {
if (line.empty()) return false;
size_t i = 0;
while (i < line.length() && line[i] >= '0' && line[i] <= '9') {
i++;
}
if (i == 0 || i >= line.length() || line[i] != '.') {
return false;
}
i++;
while (i < line.length() && (line[i] == ' ' || line[i] == '\t')) {
i++;
}
return i < line.length();
}
int main() {
const char *paths[] = {
"../../cpp/assignment/full_list.md",
"../cpp/assignment/full_list.md",
"cpp/assignment/full_list.md",
"../assignment/full_list.md",
"full_list.md"
};
ifstream file;
string line;
for(const char *path : paths) {
file.open(path);
if (file.is_open()) {
break;
}
}
if (!file.is_open()) {
cerr << "Could not find full_list.md." << endl;
return 1;
}
string tasks[MAX_TASKS];
int count = 0;
while (getline(file, line) && count < MAX_TASKS) {
string cleaned = trimLine(line);
if (isTaskLine(cleaned)) {
tasks[count++] = cleaned;
}
}
file.close();
if (count == 0) {
cerr << "No assignment tasks were found in the list." << endl;
return 1;
}
unsigned int seed;
cout << "Type a number: ";
if (!(cin >> seed)) {
cerr << "That wasn't a number." << endl;
return 1;
}
unsigned int rolled = mix(seed);
int chosen = static_cast<int>(rolled % static_cast<unsigned int>(count));
cout << "\nRandom code to do:" << endl;
cout << tasks[chosen] << endl;
return 0;
}