|
| 1 | +/* Binary Search */ |
| 2 | + |
| 3 | +#include<iostream> |
| 4 | +#include<sstream> |
| 5 | +#include<fstream> |
| 6 | +#include<iomanip> |
| 7 | +#include<vector> |
| 8 | +#include<algorithm> |
| 9 | +#include"C26_BinarySearch_01.h" |
| 10 | + |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +inline void error(string s) { throw runtime_error(s); } |
| 14 | +inline void error(const string& s, const string& s2) { error(s + s2); } |
| 15 | +inline void error(const string& s, int i) { ostringstream os; os << s << ": " << i; error(os.str()); } |
| 16 | +inline void keep_window_open() { char ch; cin >> ch; } |
| 17 | + |
| 18 | +int main() |
| 19 | +{ |
| 20 | + enum Action { |
| 21 | + EXIT = -1, PRINTACTIONLIST, |
| 22 | + CASE1, CASE2, CASE3, CASE4, CASE5, CASE6, CASE7, CASE8, CASE9, CASE10 |
| 23 | + }; |
| 24 | + const string actionList = "\tList of actions:\n" |
| 25 | + " (1) BinarySearch()\n" |
| 26 | + " (-1) Exit, (0) Print the list of actions\n"; |
| 27 | + cout << actionList; |
| 28 | + int action; |
| 29 | + bool cond{ true }; |
| 30 | + while (cond) try { |
| 31 | + cout << "\nPlease enter the action: "; |
| 32 | + if (!(cin >> action)) { ClearInput(cin); error("Error. Incorrect input"); } |
| 33 | + char ch; |
| 34 | + cin.get(ch); |
| 35 | + switch (action) { |
| 36 | + case CASE1: { |
| 37 | + cout << endl; |
| 38 | + cout << sp_2 << "Enter list of ints:" << endl; |
| 39 | + string s; |
| 40 | + getline(cin, s); |
| 41 | + istringstream iss{ s }; |
| 42 | + vector<int> v; |
| 43 | + for (int num; iss >> num; ) { |
| 44 | + v.push_back(num); |
| 45 | + } |
| 46 | + sort(v.begin(), v.end()); |
| 47 | + for (int x : v) { |
| 48 | + cout << x << ' '; |
| 49 | + } |
| 50 | + cout << endl; |
| 51 | + cout << sp_2 << "Enter a number to search for: "; |
| 52 | + getline(cin, s); |
| 53 | + iss.clear(); |
| 54 | + iss.str(s); |
| 55 | + int x; |
| 56 | + iss >> x; |
| 57 | + pair<bool, size_t> result = BinarySearch(v, x); |
| 58 | + if (result.first == true) { |
| 59 | + cout << sp_4 << "The number found, position = " << result.second + 1 << endl; |
| 60 | + } |
| 61 | + else cout << "The number not found" << endl; |
| 62 | + |
| 63 | + cout << vsp_2; |
| 64 | + break; |
| 65 | + } |
| 66 | + case PRINTACTIONLIST: |
| 67 | + cout << actionList; |
| 68 | + break; |
| 69 | + case EXIT: |
| 70 | + cond = false; |
| 71 | + break; |
| 72 | + default: |
| 73 | + error("Error. Incorrect action number"); |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + catch (runtime_error& e) { |
| 78 | + cerr << e.what() << endl; |
| 79 | + } |
| 80 | + catch (...) { |
| 81 | + cerr << "Error. Exception\n"; |
| 82 | + return 1; |
| 83 | + } |
| 84 | + return 0; |
| 85 | +} |
| 86 | + |
| 87 | +void ClearInput(istream& is) |
| 88 | +{ |
| 89 | + is.clear(); |
| 90 | + is.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 91 | +} |
| 92 | + |
| 93 | +pair<bool, size_t> BinarySearch(vector<int>& v, int x) |
| 94 | +{ |
| 95 | + if (v.size() == 0) return { false, 0 }; |
| 96 | + size_t first = 0; |
| 97 | + size_t last = v.size() - 1; |
| 98 | + while (first <= last) { |
| 99 | + size_t middle = (last - first) / 2 + first; |
| 100 | + if (v[middle] == x) return { true, middle }; |
| 101 | + else if (v[middle] > x) last = middle - 1; |
| 102 | + else first = middle + 1; // v[middle] < x |
| 103 | + } |
| 104 | + return { false, 0 }; |
| 105 | +} |
0 commit comments