定义一个包含图书信息(书号、书名、价格)的顺序表,读入相应的图书数据来完成图书信息表的创建。然后,统计图书表中的图书个数,实现插入删除排序的功能,并输出主函数
时间: 2024-06-13 19:04:49 浏览: 15
定义一个包含图书信息(书号、书名、价格)的顺序表,读入相应的图书数据来完成图书信息表的创建。然后,统计图书表中的图书个数,实现插入删除排序的功能,并输出主函数。
```c++
#include <iostream>
#include <string>
using namespace std;
const int MAXSIZE = 100; //定义最大容量
struct Book {
string bookNo; //书号
string bookName; //书名
double price; //价格
};
struct SeqList {
Book bookList[MAXSIZE]; //存储图书信息的数组
int length; //当前存储的图书个数
};
//初始化顺序表
void InitList(SeqList &L) {
L.length = 0;
}
//插入图书信息
bool InsertList(SeqList &L, int pos, Book book) {
if (pos < 1 || pos > L.length + 1 || L.length == MAXSIZE) {
return false;
}
for (int i = L.length; i >= pos; i--) {
L.bookList[i] = L.bookList[i - 1];
}
L.bookList[pos - 1] = book;
L.length++;
return true;
}
//删除图书信息
bool DeleteList(SeqList &L, int pos) {
if (pos < 1 || pos > L.length) {
return false;
}
for (int i = pos - 1; i < L.length - 1; i++) {
L.bookList[i] = L.bookList[i + 1];
}
L.length--;
return true;
}
//按书号排序
void SortList(SeqList &L) {
for (int i = 0; i < L.length - 1; i++) {
for (int j = i + 1; j < L.length; j++) {
if (L.bookList[i].bookNo > L.bookList[j].bookNo) {
Book temp = L.bookList[i];
L.bookList[i] = L.bookList[j];
L.bookList[j] = temp;
}
}
}
}
//输出图书信息
void PrintList(SeqList L) {
for (int i = 0; i < L.length; i++) {
cout << L.bookList[i].bookNo << " " << L.bookList[i].bookName << " " << L.bookList[i].price << endl;
}
}
int main() {
SeqList L;
InitList(L);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
Book book;
cin >> book.bookNo >> book.bookName >> book.price;
InsertList(L, i + 1, book);
}
cout << "图书个数:" << L.length << endl;
PrintList(L);
DeleteList(L, 2);
cout << "删除后:" << endl;
PrintList(L);
SortList(L);
cout << "排序后:" << endl;
PrintList(L);
return 0;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)