/
*binary_search.cpp
99 lines (83 loc) · 2.42 KB
/
*binary_search.cpp
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
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
void greetings() {
cout << setw(25) << "GUESS NUMBER" << endl;
for (size_t i = 0; i < 40; i++) //separator
cout << '-';
cout << "\nHi! This is the game 'guess the number'." << endl
<< "It uses binary search." << endl;
}
int binary_search(int &l_bound, int &r_bound, string user_answer) {
int middle;
if (r_bound < 0) {
middle = (l_bound + r_bound) / 2;
if (user_answer == "more")
{
l_bound = middle + 1;
middle = (l_bound + r_bound) / 2;
}
if (user_answer == "less")
{
r_bound = middle - 1;
middle = (l_bound + r_bound) / 2;
}
}
else { // if r_bound > 0
middle = (l_bound + r_bound) / 2;
if (user_answer == "more")
{
l_bound = middle + 1;
middle = (l_bound + r_bound) / 2;
}
if (user_answer == "less")
{
r_bound = middle - 1;
middle = (l_bound + r_bound) / 2;
}
}
return middle;
}
string lower_case(string word) {
for (auto &letter : word)
letter = tolower(letter);
return word;
}
int main() {
greetings();
int l_bound;
int r_bound;
try
{
cout << "Enter left bound: ";
cin >> l_bound;
cout << "Enter right bound: ";
cin >> r_bound;
if (l_bound > r_bound)
throw -1;
cout << "Ok! Let's play! Pick a number from " << l_bound << " to " << r_bound << endl;
}
catch(const int& e)
{
cerr << "\nError bounds of the range. Right bound must be less than left bound." << endl;
system("pause");
return -1;
}
string user_answer {""};
while (user_answer != "yes")
{
cout << "Your number is " << binary_search(l_bound, r_bound, user_answer) << "?" << endl;
cin >> user_answer;
user_answer = lower_case(user_answer);
if (user_answer != "yes")
{
cout << "More or less?" << endl;
cin >> user_answer;
user_answer = lower_case(user_answer);
}
}
cout << "\nGood game! Thank you!" << endl;
system("pause");
return 0;
}