-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitwise.cpp
More file actions
129 lines (116 loc) · 4.3 KB
/
Copy pathbitwise.cpp
File metadata and controls
129 lines (116 loc) · 4.3 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Logan Jones
// Filename: bitwise.cpp
// ---------------------------------------------------------------------------
// Reads two sets from the user and then prints several of their operations.
// ---------------------------------------------------------------------------
#include <iostream>
using namespace std;
// getters
int getSet(string const& setName);
int getInt(string const& prompt);
// ------------------ Exploring C++ Bitwise Operators --------------------
// https://en.cppreference.com/w/cpp/language/operator_arithmetic
// ----------------------------------------------------------------------------
// everything that's in the universal set but not in the provided set
int complementSet(int set);
// Bitwise AND a & b
int intersectionSets(int setA, int setB);
// Bitwise OR a | b
int unionSets(int setA, int setB);
// a & ~b
int differenceSets(int setA, int setB);
// Bitwise XOR a ^ b
int symmetricDiffSets(int setA, int setB);
// printing helpers
void displayOperations(int setA, int setB);
string setStr(int set);
int main() {
cout << "This program will ask you for the elements to complete two sets "
<< "& will display the results of various set operations\n\n";
int setA = getSet("SetA"); // get sets from user and formats as an int
int setB = getSet("SetB");
displayOperations(setA, setB); // print set operation results
return 0;
}
// gets a set from the user and returns it as an integer
int getSet(string const& setName) { // first prompt for number of elems
int tmpSet = 0;
string prompt = "Please enter the number of elements in set "+setName+": ";
int numElems = getInt(prompt); // number of set elements
for (int i = 1; i < numElems+1; i++){ // for each element
prompt = "Please enter element " + to_string(i) + ": ";
tmpSet = tmpSet | ( 1 << getInt(prompt)); // get elem and add to set
}
cout << endl;
return tmpSet;
}
// gets a valid int from 0-9 and returns it
int getInt(string const &prompt = "enter a value between 0-9"){
int validInt;
while (true) {
cout << prompt;
cin >> validInt;
// if user input is good and int is 0-9 return int otherwise, reprompt
if (!cin.fail() && validInt >= 0 && validInt <= 9) {
return validInt;
}
cin.clear(); // clear cin errorstate
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cout << "Integers between 0-9 only please" << endl;
}
}
void displayOperations(int setA, int setB) {
cout << "Input Sets" << endl;
cout << "Set A = " << setStr(setA) << endl;
cout << "Set B = " << setStr(setB) << endl << endl;
cout << "Complement of A = " << setStr(complementSet(setA)) << endl;
cout << "Complement of B = " << setStr(complementSet(setB)) << endl;
cout << "Union of A and B = " << setStr(unionSets(setA, setB)) << endl;
cout << "Intersection of A and B = "
<< setStr(intersectionSets(setA, setB)) << endl;
cout << "Difference of A and B = "
<< setStr(differenceSets(setA, setB)) << endl;
cout << "Symmetric Difference of A and B = ";
cout << setStr(symmetricDiffSets(setA, setB)) << endl;
}
// prints set in the format {elem,elem,elem,elem}
string setStr(int set) {
string tmp = "{";
bool first = true;// handle commas
for (int i = 0; i <= 9; i++){ // for 0-9
if (set & (1 << i)){ // if in set, add to tmp
if (!first){ // add comma if not the first element
tmp += ",";
}
tmp += to_string(i);
first = false;
}
} // put closing bracket and return
return tmp + "}";
}
// everything in the universal set 0-9, not in the set provided.
int complementSet(int set){
int tmp = 0;
for (int i = 0; i <= 9; i++) {
if (!(set & (1 << i))) { // if not in set, but in universal set 0-9
tmp = tmp | ( 1 << i); // add to tmp
}
} // return the complement set
return tmp;
}
// Bitwise OR a | b
int unionSets(int setA, int setB){
return setA | setB;
}
// Bitwise AND a & b
int intersectionSets(int setA, int setB){
return setA & setB;
}
// a & ~b keep everything in a but not b in other words a - b
int differenceSets(int setA, int setB){
return setA & (~setB);
}
// Bitwise XOR a ^ b
int symmetricDiffSets(int setA, int setB){
return setA ^ setB;
}