第1关:图书信息管理
本关任务:已知每种图书包括三部分信息:ISBN(书号)、书名和价格,现在要求编写程序,用顺序表存储图书信息,实现对图书信息的管理,具体包括以下7个功能: (1)初始化 cpp void InitBookList(BookList &L)
操作结果:构造一个空的存放图书记录的顺序表L。 (2)插入 cpp bool ListInput(BookList &L,string ISBN,string BookName,int Price)
初始条件:顺序表L已经存在。 操作结果:在L的末尾增加一条图书记录(ISBN,BookName,Price)。 (3)删除 cpp bool ListDelete(BookList &L,int i)
初始条件:顺序表L已经存在,1<=i<=L.length。 操作结果:删除L中第i条图书记录。 (4)修改 cpp bool ListUpdate(BookList &L,int i,string ISBN,string BookName,int Price)
初始条件:顺序表L已经存在,1<=i<=L.length。 操作结果:修改L中第i条图书记录为(ISBN,BookName,Price)。 (5)查找 cpp bool ListSearch(BookList L,string BookName)
初始条件:顺序表L已经存在。 操作结果:在L中查找并输出书名为BookName的图书记录。 (6)计数 cpp int ListLength(BookList L)
初始条件:顺序表L已经存在。 操作结果:返回L中图书记录的条数。 (7)输出 cpp void ListOutput(BookList L)
初始条件:顺序表L已经存在。 操作结果:遍历并输出L中所有的图书记录。
#include <iostream>
#include <string>
using namespace std;
// 图书记录结构体
struct BookRecord
{
string ISBN;
string BookName;
int Price;
};
// 顺序表结构体
struct BookList
{
BookRecord *data; // 动态分配的数组存放图书记录
int length; // 顺序表的当前长度
int capacity; // 顺序表的容量
};
// 初始化顺序表
void InitBookList(BookList &L)
{
L.data = new BookRecord[20]; // 初始容量为20
L.length = 0;
L.capacity = 20;
}
// 在顺序表末尾插入一条图书记录
bool ListInput(BookList &L, string ISBN, string BookName, int Price)
{
if (L.length >= L.capacity)
{
return false; // 顺序表已满,无法插入
}
L.data[L.length].ISBN = ISBN;
L.data[L.length].BookName = BookName;
L.data[L.length].Price = Price;
L.length++;
return true;
}
// 删除顺序表中第i条图书记录
bool ListDelete(BookList &L, int i)
{
if (i < 1 || i > L.length)
{
return false; // i的值不合法
}
for (int j = i - 1; j < L.length - 1; j++)
{
L.data[j] = L.data[j+1];
}
L.length--;
return true;
}
// 修改顺序表中第i条图书记录
bool ListUpdate(BookList &L, int i, string ISBN, string BookName, int Price)
{
if (i < 1 || i > L.length)
{
return false; // i的值不合法
}
L.data[i-1].ISBN = ISBN;
L.data[i-1].BookName = BookName;
L.data[i-1].Price = Price;
return true;
}
// 在顺序表中查找并输出书名为BookName的图书记录
bool ListSearch(BookList L, string BookName)
{
for (int i = 0; i < L.length; i++)
{
if (L.data[i].BookName == BookName)
{
cout << L.data[i].ISBN << " " << L.data[i].BookName << " " << L.data[i].Price << endl;
return true;
}
}
return false; // 没有找到对应的书名
}
// 返回顺序表的长度
int ListLength(BookList L)
{
return L.length;
}
// 遍历并输出顺序表中所有的图书记录
void ListOutput(BookList L)
{
for (int i = 0; i < L.length; i++)
{
cout << L.data[i].ISBN << " " << L.data[i].BookName << " " << L.data[i].Price << endl;
}
}
int main()
{
BookList myBookList;
InitBookList(myBookList);
// 输入图书记录
string ISBN, BookName;
int Price;
while (cin >> ISBN >> BookName >> Price)
{
if (ISBN == "-1" && BookName == "-1" && Price == -1)
{
break; // 输入结束
}
ListInput(myBookList, ISBN, BookName, Price);
}
// 输出当前图书列表
cout << "当前图书列表" << endl;
ListOutput(myBookList);
// 修改第3条记录
cout << endl << "修改第3条记录" << endl;
ListUpdate(myBookList, 3, "ISBN", "BookName", 100);
ListOutput(myBookList);
// 删除第3条记录
cout << endl << "删除第3条记录" << endl;
ListDelete(myBookList, 3);
ListOutput(myBookList);
// 计数
cout << endl << "当前共有记录:" << ListLength(myBookList) << endl;
// 查找输出图书名是《DataStructure》的记录
cout << "查询输出图书名是《DataStructure》的记录:" << endl;
ListSearch(myBookList, "DataStructure");
return 0;
}
》的记录: 9787302257649 DataStructure 80