图  0.1
数据结构_第13章
ch8_5.cc
Go to the documentation of this file.
1 
14 #include <algorithm>
15 #include <cstdlib> /* srand, rand */
16 #include <ctime>
17 #include <fstream>
18 #include <iostream>
19 #include <vector>
20 
30 int main(int argc, char const *argv[])
31 {
32  // time
33  clock_t t = clock();
34  time_t rawtime;
35  struct tm *timeinfo;
36 
37  // 获取文件路径,参考:http://www.cplusplus.com/reference/string/string/find_last_of/
38  const std::string full_path_exec{argv[0]};
39  std::string::size_type found = full_path_exec.find_last_of("/\\", std::string::npos);
40  const std::string exec_path = full_path_exec.substr(0, found);
41  const std::string exec_filename = full_path_exec.substr(found + 1, std::string::npos);
42  const std::string data_file_name("\\ch8_5.result");
43 
44  // 打开测试数据文件
45  std::string data_file_full_path{exec_path + data_file_name}; // 数据文件的绝对地址
46  std::ofstream fout(data_file_full_path.c_str(), std::ios_base::app);
47  if (fout.fail())
48  {
49  std::cerr << "无写权限,测试数据文件生成失败!\n";
50  system("pause");
51  return false;
52  }
53 
54  // output time information
55  time(&rawtime); // Get the current calendar time
56  timeinfo = localtime(&rawtime); // Convert time_t to tm as local time
57  // printf("Current local time and date: %s\n", asctime(timeinfo)); // Convert tm structure to string
58  std::cout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
59  fout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
60 
61  // 开始测试
62  try
63  {
64  std::vector<size_t> vec;
65 
66  /* initialize random seed: */
67  srand(time(NULL));
68 
69  /* generate secret number between low and high: */
70  size_t low{10}, high{25}, num_of_points{3};
71  size_t iSecret;
72  for (size_t i = 0; i < num_of_points; ++i)
73  {
74  iSecret = low + (high - low + 1) * rand() / (RAND_MAX + 1);
75  vec.push_back(iSecret);
76  }
77  }
78  catch (const std::string &e)
79  {
80  std::cerr << e << '\n';
81  fout << e << '\n';
82  }
83  catch (const std::exception &e)
84  {
85  std::cerr << e.what() << '\n';
86  fout << e.what() << '\n';
87  }
88 
89  // output time information
90  t = clock() - t;
91  // printf("\nIt took me %ld clicks (%f seconds).\n", t, ((float)t) / CLOCKS_PER_SEC);
92  std::cout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
93  fout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
94 
95  // 测试结束
96  fout.close();
97  system("pause");
98  return 0;
99 } // main
main
int main(int argc, char const *argv[])
测试程序
Definition: ch8_5.cc:30