图  0.1
数据结构_第13章
ch8_1.cc
Go to the documentation of this file.
1 
14 #include "Set.h"
15 #include <algorithm>
16 #include <climits>
17 #include <cstdlib> /* srand, rand */
18 #include <ctime>
19 #include <fstream>
20 #include <iostream>
21 #include <vector>
22 
34 template <class Key_T, class Other_T>
35 size_t binarySearch(Set::Set<Key_T, Other_T> data[], size_t low, size_t high, const Key_T &x)
36 {
37  // 查找失败
38  if (low > high)
39  return high - low + 1;
40 
41  size_t mid{(low + high) / 2};
42  if (x > data[mid].key_)
43  return binarySearch(data, mid + 1, high, x);
44  if (x < data[mid].key_)
45  return binarySearch(data, low, mid - 1, x);
46  return mid;
47 }
48 
58 int main(int argc, char const *argv[])
59 {
60  // time
61  clock_t t = clock();
62  time_t rawtime;
63  struct tm *timeinfo;
64 
65  // 获取文件路径,参考:http://www.cplusplus.com/reference/string/string/find_last_of/
66  const std::string full_path_exec{argv[0]};
67  std::string::size_type found = full_path_exec.find_last_of("/\\", std::string::npos);
68  const std::string exec_path = full_path_exec.substr(0, found);
69  const std::string exec_filename = full_path_exec.substr(found + 1, std::string::npos);
70  const std::string data_file_name("\\ch8_1.result");
71 
72  // 打开测试数据文件
73  std::string data_file_full_path{exec_path + data_file_name}; // 数据文件的绝对地址
74  std::ofstream fout(data_file_full_path.c_str(), std::ios_base::app);
75  if (fout.fail())
76  {
77  std::cerr << "无写权限,测试数据文件生成失败!\n";
78  system("pause");
79  return false;
80  }
81 
82  // output time information
83  time(&rawtime); // Get the current calendar time
84  timeinfo = localtime(&rawtime); // Convert time_t to tm as local time
85  // printf("Current local time and date: %s\n", asctime(timeinfo)); // Convert tm structure to string
86  std::cout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
87  fout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
88 
89  // 开始测试
90  try
91  {
92  std::vector<Set::Set<size_t>> vector_size_t;
93 
94  /* initialize random seed: */
95  srand(time(NULL));
96 
97  /* generate secret number between low and high: */
98  size_t low{10}, high{99}, num_of_points{18};
99  size_t iSecret;
100  for (size_t i = 0; i < num_of_points; ++i)
101  {
102  // 第0个元素不参与查找,以符合binarySearch接口要求
103  iSecret = low + (high - low + 1) * rand() / (RAND_MAX + 1);
104  vector_size_t.push_back(Set::Set<size_t>{iSecret});
105  }
106 
107  // 先排序
108  std::sort(vector_size_t.begin(), vector_size_t.end(), std::less<Set::Set<size_t>>{});
109 
110  // 开始查找
111  for (size_t i = 0; i < vector_size_t.size(); ++i)
112  {
113  std::cout << "binarySearch(" << vector_size_t.at(i).key_ << ") = " << binarySearch(vector_size_t.data(), 0, vector_size_t.size() - 1, vector_size_t.at(i).key_) << '\n';
114  fout << "binarySearch(" << vector_size_t.at(i).key_ << ") = " << binarySearch(vector_size_t.data(), 0, vector_size_t.size() - 1, vector_size_t.at(i).key_) << '\n';
115  }
116  }
117  catch (const std::string &e)
118  {
119  std::cerr << e << '\n';
120  fout << e << '\n';
121  }
122 
123  // output time information
124  t = clock() - t;
125  // printf("\nIt took me %ld clicks (%f seconds).\n", t, ((float)t) / CLOCKS_PER_SEC);
126  std::cout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
127  fout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
128 
129  // 测试结束
130  fout.close();
131  system("pause");
132  return 0;
133 } // main
binarySearch
size_t binarySearch(Set::Set< Key_T, Other_T > data[], size_t low, size_t high, const Key_T &x)
二分查找(递归版本)
Definition: ch8_1.cc:35
Set::Set
Definition: Set.h:26
main
int main(int argc, char const *argv[])
测试程序
Definition: ch8_1.cc:58
Set.h
集合结构