Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 345c9dc

Browse files
committed
Binary Search
1 parent 11c2e01 commit 345c9dc

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

‎CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ add_subdirectory ("Chapter_21")
3131
add_subdirectory ("Chapter_23")
3232
add_subdirectory ("Chapter_24")
3333
add_subdirectory ("Chapter_25")
34+
add_subdirectory ("Chapter_26")

‎Chapter_26/C26_BinarySearch_01.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

‎Chapter_26/C26_BinarySearch_01.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Binary Search */
2+
3+
using namespace std;
4+
5+
const char* sp_2 = " ";
6+
const char* sp_4 = " ";
7+
const char* sp_6 = " ";
8+
const char* sp_8 = " ";
9+
const char* vsp_2 = "\n\n";
10+
const char* vsp_3 = "\n\n\n";
11+
const char* vsp_4 = "\n\n\n\n";
12+
13+
void ClearInput(istream& is);
14+
15+
pair<bool, size_t> BinarySearch(vector<int>& v, int x);

‎Chapter_26/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# CMakeList.txt : CMake project for Template, include source and define
2+
# project specific logic here.
3+
#
4+
cmake_minimum_required (VERSION 3.8)
5+
6+
project ("Chapter_26")
7+
8+
set (CMAKE_CXX_STANDARD 14)
9+
set (CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/..)
10+
11+
12+
set (FILE_NAME C26_BinarySearch_01)
13+
set (SOURCE_CXX_LIST ${FILE_NAME}.cpp)
14+
15+
16+
# Add source to this project's executable.
17+
add_executable (${FILE_NAME} ${SOURCE_CXX_LIST})
18+
19+
20+
# TODO: Add tests and install targets if needed.
21+
install (TARGETS ${FILE_NAME} CONFIGURATIONS Debug DESTINATION Build/Debug)
22+
install (TARGETS ${FILE_NAME} CONFIGURATIONS Release DESTINATION Build)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /