帮我用c或c++写一个图书管理系统,要求用折半查找来查找图书的序号,用直接插入排序将图书的价格进行排序,用快速排序将图书按照序号排序
时间: 2024-03-25 22:37:01 浏览: 13
好的,以下是一个简单的图书管理系统的实现,它使用了折半查找、直接插入排序和快速排序算法。需要注意的是,这只是一个简单的示例,实际的图书管理系统需要更多的功能和细节处理。
```c++
#include <iostream>
#include <algorithm>
using namespace std;
struct Book {
int id;
string name;
string author;
double price;
};
// 折半查找
int binary_search(Book books[], int n, int id) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (books[mid].id == id) {
return mid;
} else if (books[mid].id < id) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
// 直接插入排序(按价格升序)
void insert_sort(Book books[], int n) {
for (int i = 1; i < n; ++i) {
Book tmp = books[i];
int j = i - 1;
while (j >= 0 && books[j].price > tmp.price) {
books[j + 1] = books[j];
j--;
}
books[j + 1] = tmp;
}
}
// 快速排序(按编号升序)
void quick_sort(Book books[], int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right;
Book pivot = books[(left + right) / 2];
while (i <= j) {
while (books[i].id < pivot.id) {
i++;
}
while (books[j].id > pivot.id) {
j--;
}
if (i <= j) {
swap(books[i], books[j]);
i++;
j--;
}
}
quick_sort(books, left, j);
quick_sort(books, i, right);
}
int main() {
int n;
cout << "请输入图书数量:";
cin >> n;
Book books[n];
cout << "请依次输入图书信息(编号、名称、作者、价格):" << endl;
for (int i = 0; i < n; ++i) {
cin >> books[i].id >> books[i].name >> books[i].author >> books[i].price;
}
// 折半查找
int search_id;
cout << "请输入要查找的图书编号:";
cin >> search_id;
int index = binary_search(books, n, search_id);
if (index != -1) {
cout << "找到了该图书,编号为 " << books[index].id << ",名称为 " << books[index].name << ",作者为 " << books[index].author << ",价格为 " << books[index].price << endl;
} else {
cout << "没有找到该图书" << endl;
}
// 直接插入排序
insert_sort(books, n);
cout << "按价格升序排序后的图书信息:" << endl;
for (int i = 0; i < n; ++i) {
cout << books[i].id << " " << books[i].name << " " << books[i].author << " " << books[i].price << endl;
}
// 快速排序
quick_sort(books, 0, n - 1);
cout << "按编号升序排序后的图书信息:" << endl;
for (int i = 0; i < n; ++i) {
cout << books[i].id << " " << books[i].name << " " << books[i].author << " " << books[i].price << endl;
}
return 0;
}
```
以上是一个简单的图书管理系统实现,其中使用了折半查找、直接插入排序和快速排序算法。如果需要更多的功能和细节处理,可以根据实际需求进行修改和完善。
相关推荐
![](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)
![](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)