一、实验目的:、
实现书上图书馆管理系统
(1) 主函数
(2) 修改:根据指定的ISBN,修改图书的价格
(3) 排序:将图书按照价格由低到高进行排序。
(4) 计数:统计文件中的图书数量
要求:用c语言或者c++实现
二、实验仪器或设备:
操作系统:Windows11
编程环境:Dev-cpp 5.11
三、算法总体设计
1.顺序表的初始化
2. 添加书籍到顺序表
3. 打印顺序表中的书籍信息
4. 修改图书价格
5.冒泡排序
6. 顺序表的查找
7.用户交互
四、实验步骤(包括主要步骤、命令分析等)
1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 #include <iomanip> 5 #include <limits> 6 #include <vector> // 用于动态分配存储空间 7 using namespace std; 8 #define OK 1 9 #define ERROR 0 10 #define OVERFLOW -2 11 typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。 12 typedef int ElemType; //ElemType 为可定义的数据类型, 13 #define MAXSIZE 100 //顺序表可能达到的最大长度 14 struct Book { 15 string id;//ISBN 16 string name;//书名 17 double price;//定价 18 }; 19 typedef struct { 20 Book* elem; //存储空间的基地址 21 int length; 22 int maxSize;//当前长度 23 } SqList; 24 Status InitList_Sq(SqList& L) { 25 L.elem = new Book[MAXSIZE]; // 分配最大容量为MAXSIZE的数组空间 26 L.length = 0; // 初始化长度为0 27 L.maxSize = MAXSIZE; // 初始化最大容量 28 return OK; // 初始化成功 29 } 30 int LocateElem_Sq(SqList& L, const string& e) { //算法2.3 顺序表的查找 31 //顺序表的查找 32 for (int i = 0; i < L.length; i++) 33 if (L.elem[i].id== e) 34 35 return i + 1;//查找成功,返回序号i+1 36 return 0;//查找失败,返回0 37 } 38 Status ModifyBookPrice(SqList& L, const string& isbn, double newPrice) { 39 for (int i = 0; i < L.length; ++i) { 40 if (L.elem[i].id == isbn) { 41 L.elem[i].price = newPrice; 42 return OK; 43 } 44 } 45 return ERROR; // 未找到图书 46 } 47 // 修改图书价格 48 Status ModifyPrice(SqList &L, string id, double newPrice) { 49 int pos = LocateElem_Sq(L, id); 50 if (pos == 0) return ERROR; 51 L.elem[pos - 1].price = newPrice; 52 return OK; 53 } 54 //按价格排序 55 void BubbleSortByPrice(SqList& L) { 56 for (int i = 0; i < L.length - 1; ++i) { 57 for (int j = 0; j < L.length - 1 - i; ++j) { 58 if (L.elem[j].price > L.elem[j + 1].price) { 59 // 交换元素 60 Book temp = L.elem[j]; 61 L.elem[j] = L.elem[j + 1]; 62 L.elem[j + 1] = temp; 63 } 64 } 65 } 66 } 67 // 为了从文件中读取数据并填充到顺序表中 68 Status ReadBooksFromFile(SqList& L, const string& filename) { 69 ifstream infile(filename.c_str()); 70 if (!infile.is_open()) { 71 cerr << "文件打开失败 " << filename << endl; 72 return ERROR; 73 } 74 string id, name; 75 double price; 76 // 跳过文件的第一行(标题行) 77 infile.ignore(numeric_limits<streamsize>::max(), '\n'); 78 while (infile >> id >> name >> price) { 79 if (L.length >= L.maxSize) { 80 cerr << "List is full, cannot add more books." << endl; 81 return OVERFLOW; 82 } 83 // 添加书籍到顺序表 84 L.elem[L.length].id = id; 85 L.elem[L.length].name = name; 86 L.elem[L.length].price = price; 87 L.length++; 88 } 89 infile.close(); 90 return OK; 91 } 92 //计算总数 93 int CountBooksInFile(SqList& L) { 94 return L.length; 95 } 96 // 打印顺序表中的书籍信息(用于验证) 97 void PrintBooks(const SqList& L) { 98 cout<<setw(10) << "ID" <<setw(30) << "书名" <<setw(20) << "价格" <<endl; 99 for (int i = 0; i < L.length; i++) { 100 cout <<setw(10) <<L.elem[i].id<<setw(30)<< L.elem[i].name<<setw(15)<< L.elem[i].price << endl; 101 } 102 } 103 int main() { 104 cout << "欢迎来到图书管理系统\n"; 105 cout << "请输入项目前编号执行相关的操作\n"; 106 cout << "[0]退出\n"; 107 cout << "[1]修改(可根据指定的ISBN,修改图书的价格)\n"; 108 cout << "[2]排序(将图书按照价格由低到高进行排序)\n"; 109 cout << "[3]统计(文件中图书数量)\n"; 110 SqList bookList; 111 string filename = "book.txt"; 112 Status initStatus = InitList_Sq(bookList); 113 if (initStatus != OK) { 114 cerr << "顺序表初始化失败" << endl; 115 return ERROR; 116 } 117 Status readStatus = ReadBooksFromFile(bookList, filename); 118 if (readStatus != OK) { 119 cerr << "从文件中读取书本信息失败" << endl; 120 // 注意:在实际应用中,如果读取失败,应该释放已分配的资源 121 delete[] bookList.elem; 122 return ERROR; 123 } 124 string idToModify; 125 double newPrice; 126 while (1) { 127 int userkey = 0; 128 cin >> userkey; 129 switch (userkey) { 130 case 0: 131 cout << "【退出】\n"; 132 cout << "退出成功\n"; 133 system("pause"); 134 return 0; // 关闭整个程序,使用return更优雅 135 case 1: 136 cout << "【修改】\n"; 137 PrintBooks(bookList); 138 cout << "输出需要修改图书的ISBN\n"; 139 cin >> idToModify; 140 cout << "输出新的价格\n"; 141 cin >> newPrice; // 读取新的价格 142 143 if (ModifyPrice(bookList, idToModify, newPrice) == OK) { 144 cout << "ISBN为 " << idToModify << " 的图书价格修改为 " << newPrice << endl; 145 } else { 146 cout << "ISBN为 " << idToModify << " 的图书未找到。" << endl; 147 } 148 break; 149 150 case 2: 151 cout << "【排序】\n"; 152 BubbleSortByPrice(bookList); 153 PrintBooks(bookList); 154 break; 155 156 case 3: // 统计图书数量 157 cout << "【计数】\n"; 158 int count = CountBooksInFile(bookList); 159 cout << "文件中的图书总数: " << count << endl; 160 break; 161 } 162 system("pause"); 163 system("cls"); 164 } 165 return 0; 166 }
五、结果分析与总结
1.给用户提示信息
2.输入按键1进行修改功能根据指定的ISBN,修改图书的价格
3.输入按键2进行排序功能使书本价格由低到高排序。
4.输入按键3进行计数功能得出文件书本的数量。
结语:
通过本次实验,成功地实现了图书馆管理系统的基本功能,包括主函数的设置、根据ISBN修改图书价格、按价格排序图书以及统计文件中的图书数量。在实验过程中,我深入理解了C++语言的基本语法和面向对象编程的思想,并掌握了文件操作、数据结构以及算法设计等关键技能...