blob: 8bfac1c4f3d5f4f77c6c5df97602b6b5de83b15b (
plain)
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
|
#include <iostream>
#include <limits>
/*
Awfully long comment here, but i did this to visualize what's happening and make it easier to understand
-------------------------------------------------------------------------------------------------------------------------
00000000 00000000 00000000 00000000 (uint32_t Quiz1)
00000000 00000000 00000000 00000001 (1UL)
<< 27
00001000 00000000 00000000 00000000 (1UL << 27(Student))
passed: Quiz1 OR (1UL << 27)
00000000 00000000 00000000 00000000 (Quiz1)
00001000 00000000 00000000 00000000 (1UL << 27)
if ther is a 1 on either of the two sides then add the bit (1 -- 1 = 1) (1 -- 0 = 1) (0 -- 0 = 0)
result of Quiz1 OR (1UL << 27):
00001000 00000000 00000000 00000000
-------------------------------------------------------------------------------------------------------------------------
but what if the student failed? (from the start)
failed: Quiz1 AND NOT(1UL << 27)
00000000 00000000 00000000 00000000 (Quiz1)
11110111 11111111 11111111 11111111 NOT(1UL << 27)
if there is a 1 on both sides then keep it at 1. Otherwise set it at 0. (1 -- 1 = 1) (1 -- 0 = 0) (0 -- 0 = 0)
result of Quiz1 AND NOT(1UL << 27):
00000000 00000000 00000000 00000000
-------------------------------------------------------------------------------------------------------------------------
but what if we set the student to passed but then realise they failed?
failed: Quiz1 AND NOT(1UL << 27)
00001000 00000000 00000000 00000000 (Quiz1)
11110111 11111111 11111111 11111111 NOT(1UL << 27)
(1 -- 1 = 1) (1 -- 0 = 0) (0 -- 0 = 0)
result of Quiz1 AND NOT(1UL << 27):
00000000 00000000 00000000 00000000
-------------------------------------------------------------------------------------------------------------------------
okay but what if we have several students that pass and we need to change one to failed?
failed: Quiz1 AND NOT(1UL << 27)
00001000 01100000 00010000 11100000 (Quiz1)
11110111 11111111 11111111 11111111 NOT(1UL << 27)
(1 -- 1 = 1) (1 -- 0 = 0) (0 -- 0 = 0)
result of Quiz1 AND NOT(1UL << 27):
00000000 01100000 00010000 11100000
-------------------------------------------------------------------------------------------------------------------------
NOT(~) --- Inverts the operand
AND(&) --- If both operands contains 1 then it stays 1. Anything else becomes 0.
OR(|) --- If one or both of the operands have 1 then it becomes 1. Anything else becomes 0.
XOR(^) --- Turn to one if one operand has 1. If both have 1, or if both have 0 then turn to 0.
*/
void cin_clear() {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
int main () {
uint32_t Quiz1 = 0;
uint32_t Student = 0;
uint32_t Pass = 0;
const int maxval = 31;
std::string choice;
while(1) {
if(std::cin.eof()) {
std::cout << "Exiting..." << std::endl;
return -1;
}
std::cout << "Would you like to 'add' a new student's result or 'print' out the total results?" << std::endl;
if(std::cin >> choice) {} else {
cin_clear();
continue;
}
if(choice == "add") {
std::cout << "Which student out of 30?" << std::endl;
if(std::cin >> Student) {} else {
cin_clear();
continue;
}
if(Student >= maxval || Student < 1) {
continue;
}
std::cout << "Did this student pass? (0 == fail) (1 == pass)" << std::endl;
if(std::cin >> Pass) {} else {
cin_clear();
continue;
}
if(Pass) {
Quiz1 |= 1UL << Student;
}
if(!Pass) {
Quiz1 &= ~(1UL << Student);
}
std::cout << "Added result for student #" << Student
<< ((Quiz1 & (1UL << Student)) ? ": passed" : ": failed") << std::endl;
}
if(choice == "print") {
for(int i = 1 ; i != maxval ; ++i) {
std::cout << "Student #" << i << " " << ((Quiz1 & (1UL << i)) ? "passed" : "failed") << std::endl;
}
}
}
return 0;
}
|