test.cpp
来自「数据结构与程序设计教材源码 数据结构与程序设计教材源码」· C++ 代码 · 共 55 行
CPP
55 行
void print_out(char *comment, float t, int comp_ct, int searches)
{
float average;
cout << comment << " Search Statistics: " << endl;
cout << " Time for " << searches << " comparisons was " << t << endl;
average = (( float ) comp_ct) / (( float ) searches);
cout << " Average number of comparisons per search was " << average << endl;
}
void test_search(int searches, List<Record> &the_list)
/*
Pre: None.
Post: The number of key comparisons and CPU time for a
sequential searching funtion
have been calculated.
Uses: Methods of the classes List, Random, and Timer,
together with an output function print_out
*/
{
int list_size = the_list.size();
if (searches <= 0 || list_size < 0) {
cout << " Exiting test: " << endl
<< " The number of searches must be positive." << endl
<< " The number of list entries must exceed 0." << endl;
return;
}
int i, target, found_at;
Key::comparisons = 0;
Random number;
Timer clock;
for (i = 0; i < searches; i++) {
target = 2 * number.random_integer(0, list_size - 1) + 1;
if (sequential_search(the_list, target, found_at) == not_present)
cout << "Error: Failed to find expected target " << target << endl;
}
print_out("Successful", clock.elapsed_time(), Key::comparisons, searches);
Key::comparisons = 0;
clock.reset();
for (i = 0; i < searches; i++) {
target = 2 * number.random_integer(0, list_size);
if (sequential_search(the_list, target, found_at) == success)
cout << "Error: Found unexpected target " << target
<< " at " << found_at << endl;
}
print_out("Unsuccessful", clock.elapsed_time(), Key::comparisons, searches);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?