编程实现如下功能: (1)根据输入的查找表的表长n和n个关键字值,建立顺序查找表,并在此查找表中用顺序查找方法查找给定关键值的记录,最后输出查找结果。 (2)根据输入的查找表的表长n和n个按升排列的关键字值,建立有序顺序查找表,并在此查找表中用二分查找方法查找给定关键值的记录,最后输出查找结果。 (3)主程序中要求设计一个菜单,允许用户通过菜单来多次选择执行哪一种查找操作。
时间: 2024-03-28 11:41:37 浏览: 8
好的,我可以为您提供相应的代码实现。以下是使用 C++ 编写的程序,实现了您所需的三个功能。程序中包含一个菜单,允许用户通过输入数字选择功能。
```cpp
#include <iostream>
#include <algorithm>
using namespace std;
//建立顺序查找表
void createSequentialSearchTable(int* table, int n) {
cout << "请输入" << n << "个关键字值:" << endl;
for (int i = 0; i < n; i++) {
cin >> table[i];
}
}
//顺序查找
int sequentialSearch(int* table, int n, int key) {
for (int i = 0; i < n; i++) {
if (table[i] == key) {
return i;
}
}
return -1;
}
//建立有序顺序查找表
void createOrderedSequentialSearchTable(int* table, int n) {
cout << "请输入" << n << "个按升序排列的关键字值:" << endl;
for (int i = 0; i < n; i++) {
cin >> table[i];
}
}
//二分查找
int binarySearch(int* table, int n, int key) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (table[mid] == key) {
return mid;
} else if (table[mid] < key) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int main() {
int choice;
do {
cout << "请选择查找方式:" << endl;
cout << "1.顺序查找" << endl;
cout << "2.有序顺序查找" << endl;
cout << "0.退出程序" << endl;
cin >> choice;
switch (choice) {
case 1: {
int n, key;
cout << "请输入表长n:" << endl;
cin >> n;
int* table = new int[n];
createSequentialSearchTable(table, n);
cout << "请输入要查找的关键字值:" << endl;
cin >> key;
int index = sequentialSearch(table, n, key);
if (index != -1) {
cout << "查找成功,关键字值在表中的位置为:" << index << endl;
} else {
cout << "查找失败,关键字值不在表中!" << endl;
}
delete[] table;
break;
}
case 2: {
int n, key;
cout << "请输入表长n:" << endl;
cin >> n;
int* table = new int[n];
createOrderedSequentialSearchTable(table, n);
cout << "请输入要查找的关键字值:" << endl;
cin >> key;
int index = binarySearch(table, n, key);
if (index != -1) {
cout << "查找成功,关键字值在表中的位置为:" << index << endl;
} else {
cout << "查找失败,关键字值不在表中!" << endl;
}
delete[] table;
break;
}
case 0: {
cout << "程序已退出!" << endl;
break;
}
default: {
cout << "输入有误,请重新输入!" << endl;
break;
}
}
} while (choice != 0);
return 0;
}
```
希望这段程序能够满足您的需求,如有任何疑问,请随时向我提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)