|
| 1 | +/* Exercise 26.1 */ |
| 2 | + |
| 3 | +#include<iostream> |
| 4 | +#include<sstream> |
| 5 | +#include<fstream> |
| 6 | +#include<iomanip> |
| 7 | +#include<vector> |
| 8 | +#include"C26_Exercise_26.1.h" |
| 9 | + |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +inline void error(string s) { throw runtime_error(s); } |
| 13 | +inline void error(const string& s, const string& s2) { error(s + s2); } |
| 14 | +inline void error(const string& s, int i) { ostringstream os; os << s << ": " << i; error(os.str()); } |
| 15 | +inline void keep_window_open() { char ch; cin >> ch; } |
| 16 | +inline int randint(int max) { return rand() % max; } |
| 17 | +inline int randint(int min, int max) { return randint(max - min) + min; } |
| 18 | + |
| 19 | +int main() |
| 20 | +{ |
| 21 | + enum Action { |
| 22 | + EXIT = -1, PRINTACTIONLIST, |
| 23 | + CASE1, CASE2, CASE3, CASE4, CASE5, CASE6, CASE7, CASE8, CASE9, CASE10 |
| 24 | + }; |
| 25 | + const string actionList = "\tList of actions:\n" |
| 26 | + " (1) BinarySearch()\n" |
| 27 | + " (-1) Exit, (0) Print the list of actions\n"; |
| 28 | + cout << actionList; |
| 29 | + int action; |
| 30 | + bool cond{ true }; |
| 31 | + while (cond) try { |
| 32 | + cout << "\nPlease enter the action: "; |
| 33 | + if (!(cin >> action)) { ClearInput(cin); error("Error. Incorrect input"); } |
| 34 | + char ch; |
| 35 | + cin.get(ch); |
| 36 | + switch (action) { |
| 37 | + case CASE1: { |
| 38 | + cout << endl; |
| 39 | + string failed = "failed"; |
| 40 | + string succeed = "succeed"; |
| 41 | + |
| 42 | + // Test 1 "The empty set" |
| 43 | + { |
| 44 | + vector<int> v; |
| 45 | + int x = 15; |
| 46 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 47 | + cout << sp_2 << "Test 1 \"The empty set\": " << ((result) ? failed : succeed) << endl; |
| 48 | + } |
| 49 | + |
| 50 | + // Test 2.1 "Small sets" |
| 51 | + { |
| 52 | + vector<int> v{ 2,15,16 }; |
| 53 | + int x = 15; |
| 54 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 55 | + cout << sp_2 << "Test 2.1 \"Small sets\": " << ((result) ? succeed : failed) << endl; |
| 56 | + } |
| 57 | + |
| 58 | + // Test 2.2 "Small sets" |
| 59 | + { |
| 60 | + vector<int> v{ -2,15 }; |
| 61 | + int x = 15; |
| 62 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 63 | + cout << sp_2 << "Test 2.2 \"Small sets\": " << ((result) ? succeed : failed) << endl; |
| 64 | + } |
| 65 | + |
| 66 | + // Test 2.3 "Small sets" |
| 67 | + { |
| 68 | + vector<int> v{ 15,17,30 }; |
| 69 | + int x = 15; |
| 70 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 71 | + cout << sp_2 << "Test 2.3 \"Small sets\": " << ((result) ? succeed : failed) << endl; |
| 72 | + } |
| 73 | + |
| 74 | + // Test 3.1 "Large sets" |
| 75 | + { |
| 76 | + vector<int> v{ -43,-42,-39,-35,-30,-28,-26,-20,-15,-14,-12,-10,-8,-6,-3,-1,0, |
| 77 | + 1,3,5,6,8,10,11,15,18,19,22,28,30,34,39,41,49,58,59,61,93 }; |
| 78 | + int x = 15; |
| 79 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 80 | + cout << sp_2 << "Test 3.1 \"Large sets\": " << ((result) ? succeed : failed) << endl; |
| 81 | + } |
| 82 | + |
| 83 | + // Test 3.2 "Large sets" |
| 84 | + { |
| 85 | + vector<int> v{ -434,-431,-420,-410,-400,-399,-390,-380,-371,-340,-320,-315,-309,-301, |
| 86 | + -300,-290,-289,-271,-270,-260,-222,-211,-199,-100,-43,-12,-8,-4,-1,0, |
| 87 | + 1,2,4,5,6,8,9,11,15,35,94,124,165,244,564,933,1145,1255,1599,2222,3445,9325, |
| 88 | + 35553,44554,99949,134412,255355,459999,699455,999999 }; |
| 89 | + int x = 15; |
| 90 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 91 | + cout << sp_2 << "Test 3.2 \"Large sets\": " << ((result) ? succeed : failed) << endl; |
| 92 | + } |
| 93 | + |
| 94 | + // Test 4.1 "Sets with extreme distributions" |
| 95 | + { |
| 96 | + vector<int> v{ -2345455346,-333444666,-1000,0,543534,324565345,1999888777 }; |
| 97 | + int x = -1000; |
| 98 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 99 | + cout << sp_2 << "Test 4.1 \"Sets with extreme distributions\": " |
| 100 | + << ((result) ? succeed : failed) << endl; |
| 101 | + } |
| 102 | + |
| 103 | + // Test 4.2 "Sets with extreme distributions" |
| 104 | + { |
| 105 | + vector<int> v{ -1000000000,-500000,-100,44444,222333444,200000000 }; |
| 106 | + int x = 222333444; |
| 107 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 108 | + cout << sp_2 << "Test 4.2 \"Sets with extreme distributions\": " |
| 109 | + << ((result) ? succeed : failed) << endl; |
| 110 | + } |
| 111 | + |
| 112 | + // Test 5.1 "Sets where 'what is of interest' happens near the end" |
| 113 | + { |
| 114 | + vector<int> v{ -43,-42,-39,-35,-30,-28,-26,-20,-15,-14,-12,-10,-8,-6,-3,-1,0, |
| 115 | + 1,3,5,6,8,10,11,15,18,19,22,28,30,34,39,41,49,58,59,61,93 }; |
| 116 | + int x = 61; |
| 117 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 118 | + cout << sp_2 << "Test 5.1 \"Sets where \"what is of interest\" happens near the end\": " |
| 119 | + << ((result) ? succeed : failed) << endl; |
| 120 | + } |
| 121 | + |
| 122 | + // Test 5.2 "Sets where 'what is of interest' happens near the end" |
| 123 | + { |
| 124 | + vector<int> v{ -43,-42,-39,-35,-30,-28,-26,-20,-15,-14,-12,-10,-8,-6,-3,-1,0, |
| 125 | + 1,3,5,6,8,10,11,15,18,19,22,28,30,34,39,41,49,58,59,61,93 }; |
| 126 | + int x = -39; |
| 127 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 128 | + cout << sp_2 << "Test 5.2 \"Sets where \"what is of interest\" happens near the end\": " |
| 129 | + << ((result) ? succeed : failed) << endl; |
| 130 | + } |
| 131 | + |
| 132 | + // Test 5.3 "Sets where 'what is of interest' happens near the end" |
| 133 | + { |
| 134 | + vector<int> v{ -43,-42,-39,-35,-30,-28,-26,-20,-15,-14,-12,-10,-8,-6,-3,-1,0, |
| 135 | + 1,3,5,6,8,10,11,15,18,19,22,28,30,34,39,41,49,58,59,61,93 }; |
| 136 | + int x = 93; |
| 137 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 138 | + cout << sp_2 << "Test 5.3 \"Sets where \"what is of interest\" happens near the end\": " |
| 139 | + << ((result) ? succeed : failed) << endl; |
| 140 | + } |
| 141 | + |
| 142 | + // Test 5.4 "Sets where "what is of interest" happens near the end" |
| 143 | + { |
| 144 | + vector<int> v{ -43,-42,-39,-35,-30,-28,-26,-20,-15,-14,-12,-10,-8,-6,-3,-1,0, |
| 145 | + 1,3,5,6,8,10,11,15,18,19,22,28,30,34,39,41,49,58,59,61,93 }; |
| 146 | + int x = -43; |
| 147 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 148 | + cout << sp_2 << "Test 5.4 \"Sets where \"what is of interest\" happens near the end\": " |
| 149 | + << ((result) ? succeed : failed) << endl; |
| 150 | + } |
| 151 | + |
| 152 | + // Test 6.1 "Sets with duplicate elements" |
| 153 | + { |
| 154 | + vector<int> v{ -43,-43,-39,-35,-30,-28,-28,-28,-15,-14,-12,-10,-10,-10,-10,-1,0, |
| 155 | + 1,1,5,6,8,8,8,8,8,19,22,28,30,34,34,34,34,58,59,59,93 }; |
| 156 | + int x = 30; |
| 157 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 158 | + cout << sp_2 << "Test 6.1 \"Sets with duplicate elements\": " |
| 159 | + << ((result) ? succeed : failed) << endl; |
| 160 | + } |
| 161 | + |
| 162 | + // Test 6.2 "Sets with duplicate elements" |
| 163 | + { |
| 164 | + vector<int> v{ -43,-43,-39,-35,-30,-28,-28,-28,-15,-14,-12,-10,-10,-10,-10,-1,0, |
| 165 | + 1,1,5,6,8,8,8,8,8,19,22,28,30,34,34,34,34,58,59,59,93 }; |
| 166 | + int x = -43; |
| 167 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 168 | + cout << sp_2 << "Test 6.2 \"Sets with duplicate elements\": " |
| 169 | + << ((result) ? succeed : failed) << endl; |
| 170 | + } |
| 171 | + |
| 172 | + // Test 6.3 "Sets with duplicate elements" |
| 173 | + { |
| 174 | + vector<int> v{ -43,-43,-39,-35,-30,-28,-28,-28,-15,-14,-12,-10,-10,-10,-10,-1,0, |
| 175 | + 1,1,5,6,8,8,8,8,8,19,22,28,30,34,34,34,34,58,59,59,93 }; |
| 176 | + int x = 8; |
| 177 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 178 | + cout << sp_2 << "Test 6.3 \"Sets with duplicate elements\": " |
| 179 | + << ((result) ? succeed : failed) << endl; |
| 180 | + } |
| 181 | + |
| 182 | + // Test 6.4 "Sets with duplicate elements" |
| 183 | + { |
| 184 | + vector<int> v{ -43,-43,-39,-35,-30,-28,-28,-28,-15,-14,-12,-10,-10,-10,-10,-1,0, |
| 185 | + 1,1,5,6,8,8,8,8,8,19,22,28,30,34,34,34,34,58,59,59,93 }; |
| 186 | + int x = 59; |
| 187 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 188 | + cout << sp_2 << "Test 6.4 \"Sets with duplicate elements\": " |
| 189 | + << ((result) ? succeed : failed) << endl; |
| 190 | + } |
| 191 | + |
| 192 | + // Test 7.1 "Sets with even and with odd numbers of elements" |
| 193 | + { |
| 194 | + vector<int> v{ 2,4,5,8,9,14,22,43 }; |
| 195 | + int x = 22; |
| 196 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 197 | + cout << sp_2 << "Test 7.1 \"Sets with even and with odd numbers of elements\": " |
| 198 | + << ((result) ? succeed : failed) << endl; |
| 199 | + } |
| 200 | + |
| 201 | + // Test 7.2 "Sets with even and with odd numbers of elements" |
| 202 | + { |
| 203 | + vector<int> v{ 2,4,5,8,9,14,22,43,66 }; |
| 204 | + int x = 4; |
| 205 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 206 | + cout << sp_2 << "Test 7.2 \"Sets with even and with odd numbers of elements\": " |
| 207 | + << ((result) ? succeed : failed) << endl; |
| 208 | + } |
| 209 | + |
| 210 | + // Test 8 "Sets generated using random numbers" |
| 211 | + constexpr int numberOfElements = 50; |
| 212 | + constexpr int spread = 20; |
| 213 | + for (int i = 1; i <= 20; ++i) { |
| 214 | + vector<int> v; |
| 215 | + int sum = 0; |
| 216 | + for (int j = 0; j < numberOfElements; ++j) { |
| 217 | + sum += randint(spread); |
| 218 | + v.push_back(sum); |
| 219 | + } |
| 220 | + int x = v[randint(v.size())]; |
| 221 | + bool result = BinarySearch(v.begin(), v.end(), x); |
| 222 | + cout << sp_2 << "Test 8." << i << " \"Sets generated using random numbers\": " |
| 223 | + << ((result) ? succeed : failed) << endl; |
| 224 | + } |
| 225 | + |
| 226 | + cout << vsp_2; |
| 227 | + break; |
| 228 | + } |
| 229 | + case PRINTACTIONLIST: |
| 230 | + cout << actionList; |
| 231 | + break; |
| 232 | + case EXIT: |
| 233 | + cond = false; |
| 234 | + break; |
| 235 | + default: |
| 236 | + error("Error. Incorrect action number"); |
| 237 | + break; |
| 238 | + } |
| 239 | + } |
| 240 | + catch (runtime_error& e) { |
| 241 | + cerr << e.what() << endl; |
| 242 | + } |
| 243 | + catch (...) { |
| 244 | + cerr << "Error. Exception\n"; |
| 245 | + return 1; |
| 246 | + } |
| 247 | + return 0; |
| 248 | +} |
| 249 | + |
| 250 | +void ClearInput(istream& is) |
| 251 | +{ |
| 252 | + is.clear(); |
| 253 | + is.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 254 | +} |
| 255 | + |
| 256 | +template<class RandomAccessIterator> |
| 257 | +bool BinarySearch(RandomAccessIterator first, RandomAccessIterator last, int x) |
| 258 | +{ |
| 259 | + using Iter = RandomAccessIterator; |
| 260 | + if (first == last) return false; |
| 261 | + --last; |
| 262 | + while (first <= last) { |
| 263 | + Iter middle = (last - first) / 2 + first; |
| 264 | + if (*middle == x) return true; |
| 265 | + else if (*middle > x) last = middle - 1; |
| 266 | + else first = middle + 1; // *middle < x |
| 267 | + } |
| 268 | + return false; |
| 269 | +} |
0 commit comments