帮我用c或c++写一个图书管理系统,要求包含删除图书,统计书的总数,对图书的序号、名称、ISBN进行遍历,修改图书的ISBN、名称、序号,用折半查找来查找图书的序号,用直接插入排序将图书的价格进行排序,用快速排序将图书按照序号排序
时间: 2024-03-25 13:37:06 浏览: 92
好的,以下是一个基本的图书管理系统的实现,它包含了删除图书、统计书的总数、对图书的序号、名称、ISBN进行遍历、修改图书的ISBN、名称、序号、折半查找、直接插入排序和快速排序等功能。需要注意的是,这只是一个简单的示例,实际的图书管理系统需要更多的功能和细节处理。
```c++
#include <iostream>
#include <algorithm>
using namespace std;
struct Book {
int id;
string name;
string author;
string isbn;
double price;
};
int n = 0; // 图书数量
Book books[1000]; // 图书数组
// 添加图书
void add_book() {
if (n >= 1000) {
cout << "图书数量已达上限,无法添加!" << endl;
return;
}
Book book;
cout << "请输入图书信息(编号、名称、作者、ISBN、价格):" << endl;
cin >> book.id >> book.name >> book.author >> book.isbn >> book.price;
books[n++] = book;
cout << "添加成功!" << endl;
}
// 删除图书
void delete_book() {
int id;
cout << "请输入要删除的图书编号:";
cin >> id;
int index = -1;
for (int i = 0; i < n; ++i) {
if (books[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
cout << "没有找到该图书,删除失败!" << endl;
return;
}
for (int i = index; i < n - 1; ++i) {
books[i] = books[i + 1];
}
n--;
cout << "删除成功!" << endl;
}
// 统计图书数量
void count_books() {
cout << "当前图书数量为:" << n << endl;
}
// 遍历图书(序号、名称、ISBN)
void traverse_books() {
cout << "图书列表:" << endl;
for (int i = 0; i < n; ++i) {
cout << books[i].id << " " << books[i].name << " " << books[i].isbn << endl;
}
}
// 修改图书信息
void modify_book() {
int id;
cout << "请输入要修改的图书编号:";
cin >> id;
int index = -1;
for (int i = 0; i < n; ++i) {
if (books[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
cout << "没有找到该图书,修改失败!" << endl;
return;
}
cout << "请输入新的图书信息(编号、名称、作者、ISBN、价格):" << endl;
cin >> books[index].id >> books[index].name >> books[index].author >> books[index].isbn >> books[index].price;
cout << "修改成功!" << endl;
}
// 折半查找
int binary_search(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() {
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(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(left, j);
quick_sort(i, right);
}
int main() {
int choice;
while (true) {
cout << "------------------------" << endl;
cout << "1. 添加图书" << endl;
cout << "2. 删除图书" << endl;
cout << "3. 统计图书数量" << endl;
cout << "4. 遍历图书" << endl;
cout << "5. 修改图书信息" << endl;
cout << "6. 按编号查找图书" << endl;
cout << "7. 按价格排序图书" << endl;
cout << "8. 按编号排序图书" << endl;
cout << "0. 退出" << endl;
cout << "------------------------" << endl;
cout << "请选择操作:";
cin >> choice;
switch (choice) {
case 1:
add_book();
break;
case 2:
delete_book();
break;
case 3:
count_books();
break;
case 4:
traverse_books();
break;
case 5:
modify_book();
break;
case 6:
int id;
cout << "请输入要查找的图书编号:";
cin >> id;
int index = binary_search(id);
if (index != -1) {
cout << "找到了该图书,编号为 " << books[index].id << ",名称为 " << books[index].name << ",作者为 " << books[index].author << ",ISBN为 " << books[index].isbn << ",价格为 " << books[index].price << endl;
} else {
cout << "没有找到该图书" << endl;
}
break;
case 7:
insert_sort();
cout << "按价格升序排序后的图书信息:" << endl;
for (int i = 0; i < n; ++i) {
cout << books[i].id << " " << books[i].name << " " << books[i].isbn << " " << books[i].price << endl;
}
break;
case 8:
quick_sort(0, n - 1);
cout << "按编号升序排序后的图书信息:" << endl;
for (int i = 0; i < n; ++i) {
cout << books[i].id << " " << books[i].name << " " << books[i].isbn << " " << books[i].price << endl;
}
break;
case 0:
cout << "谢谢使用!" << endl;
return 0;
default:
cout << "无效的选择!" << endl;
break;
}
}
}
```
以上是一个简单的图书管理系统实现,其中包含了删除图书、统计书的总数、对图书的序号、名称、ISBN进行遍历、修改图书的ISBN、名称、序号、折半查找、直接插入排序和快速排序等功能。如果需要更多的功能和细节处理,可以根据实际需求进行修改和完善。
阅读全文