基于TXT文本的简单图书管理系统
1
////////////////////////////////////////////////////////////////////////////////////// 2 3 //SqList.h 顺序表数据结构C++类定义(基类) 4 5 ////////////////////////////////////////////////////////////////////////////////////// 6 //#ifndef MYHEAD_H 7 // #define MYHEAD_H 8 // #include"myhead.h" 9 //#endif 10 typedef int Status; 11 #define LIST_MAX_SIZE 10 12 #define LISTINCREMENT 3 //顺序表使用的一些常量说明 13 #define ERROR 0 14 #define OK 1 15 ////////////////////////////////////////////////////////////////////////////////////// 16 #include<assert.h> 17 //顺序表数据结构C++声明(基类) 18 template <typename ElemType> 19 class SqList 20 { 21 public: 22 23 int n; 24 25 int bin_Search(ElemType key); //有序顺序表折半查找 26 27 void clear(); //把顺序表置空 28 29 Status deleteElem(int i, ElemType& e); //删除第i个元素 30 31 Status getElem(int i, ElemType& e); //取第i个元素 32 33 int getLength(); //求顺序表中元素的个数 34 35 int getListSize(); //取顺序表空间的大小 36 37 Status insert(int i, ElemType e); //在第i个元素之前插入一个元素 38 39 bool isEmpty(); //判断顺序表是否置空 40 41 int locateElem(ElemType e, Status(*compare)(ElemType, ElemType)); //查找第1个与e满足compare()关系的元素的序号 42 43 Status nextElem(ElemType e, ElemType& next_e); //返回某元素的后继 44 45 SqList<ElemType> operator =(SqList <ElemType> rightL);//重载赋值运算符的定义 46 47 Status priorElem(ElemType e, ElemType& prior_e);//返回某元素的前驱 48 49 int sequentialSearch(ElemType e);//在顺序表中查找某元素 50 51 52 //******************************系统自动调用构造函数及析构函数声明************************************// 53 54 SqList(); 55 56 virtual ~SqList(); 57 58 SqList(const SqList<ElemType>& otherL); //顺序表拷贝初始化构造函数 59 60 void SqList<ElemType>::input(); 61 62 void SqList<ElemType>::display() const; 63 64 void SqList<ElemType>::randSqList(); 65 66 //**************************求并、交、差集****************************************// 67 68 //求两顺序表的并集 69 void unionSet(SqList <ElemType> firstL, SqList <ElemType> secondL); 70 71 72 protected: 73 ElemType *elem; 74 int listSize; 75 //int n; 76 }; 77 78 /////////////////////////////////////////////////////////////////////////////////// 79 80 //顺序表数据结构C++类的实现 81 82 ////////////////////////////////////////////////////////////////////////////////// 83 84 //折半查找 85 template <typename ElemType> 86 int SqList<ElemType>::bin_Search(ElemType key) 87 { 88 int low, mid, high; 89 low = 0, high = n - 1; 90 while (low <= high) 91 { 92 mid = (low + high) / 2; 93 if (elem[mid] == key) 94 return mid + 1; 95 else if (elem[mid]<key) 96 low = mid + 1; 97 else 98 high = mid - 1; 99 } 100 return 0; 101 } 102 103 //把顺序表置空 104 template <typename ElemType> 105 void SqList<ElemType>::clear() 106 { 107 n = 0; 108 } 109 110 //删除第i个元素 111 template <typename ElemType> 112 Status SqList<ElemType>::deleteElem(int i, ElemType& e) 113 { 114 115 if (i<1 || i>n) return ERROR; 116 e = elem[i - 1]; 117 for (int j = i + 1; j <= n; ++j) 118 elem[j - 2] = elem[j - 1]; 119 --n; 120 return OK; 121 } 122 123 //取第i个元素 124 template <typename ElemType> 125 Status SqList<ElemType>::getElem(int i, ElemType& e) 126 { 127 if (i<1 || i>n) 128 return ERROR; 129 e = elem[i - 1]; 130 return OK; 131 } 132 133 //求顺序表中元素个数 134 template <typename ElemType> 135 int SqList<ElemType>::getLength() 136 { 137 return n; 138 } 139 140 //取顺序表存储空间的大小 141 template <typename ElemType> 142 int SqList<ElemType>::getListSize() 143 { 144 return listSize; 145 } 146 147 //在第i个元素之后插入一个元素 148 template <typename ElemType> 149 Status SqList<ElemType>::insert(int i, ElemType e) 150 { 151 ElemType *newbase; 152 153 if (i<0 || i>n + 1) return ERROR; 154 155 if (n >= listSize) 156 { 157 newbase = new ElemType[listSize + LISTINCREMENT]; 158 assert(newbase != 0); 159 for (int j = 1; j <= n; ++j) 160 newbase[j - 1] = elem[j - 1]; 161 162 delete[] elem; 163 elem = newbase; 164 listSize += LISTINCREMENT; 165 } 166 for (int j = i+1; j+1<n-1; j++) 167 elem[j+1] = elem[j]; 168 elem[i] = e; 169 ++n; 170 return OK; 171 } 172 //判断顺序表是否为空 173 template <typename ElemType> 174 bool SqList<ElemType>::isEmpty() 175 { 176 return n ? false : true; 177 } 178 179 //查找第i个与额满足compare()关系的元素 180 template <typename ElemType> 181 int SqList<ElemType>::locateElem(ElemType e, Status(*compare)(ElemType, ElemType)) 182 { 183 int i = 1; 184 for (i = 1; i <= n && !(*compare)(elem[i - 1], e); ++i); 185 186 if (i <= n) 187 return i; 188 else 189 return 0; 190 } 191 192 //返回某元素的后继 193 template <typename ElemType> 194 Status SqList<ElemType>::nextElem(ElemType e, ElemType& next_e) 195 { 196 int i = locateElem(e, equal); 197 if (i<1 || i == n) 198 return ERROR; 199 else 200 getElem(i + 1, next_e); 201 return OK; 202 } 203 204 //重载赋值运算符的定义 205 template <typename ElemType> 206 SqList<ElemType> SqList<ElemType>::operator=(SqList<ElemType>rightL) 207 { 208 if (this != &rightL) 209 { 210 if (listSize<rightL.listSize) 211 { 212 delete[] elem; 213 elem = new ElemType[rightL.listSize]; 214 assert(elem != 0); 215 listSize = rightL.listSize; 216 } 217 n = rightL.n; 218 for (int i = 1; i <= n; ++i) 219 elem[i - 1] = rightL.elem[i - 1]; 220 } 221 return *this; 222 } 223 224 //返回某元素的前驱 225 template <typename ElemType> 226 Status SqList<ElemType>::priorElem(ElemType e, ElemType& prior_e) 227 { 228 int i = locateElem(e, equal); 229 230 if (i <= 1) 231 return ERROR; 232 else 233 getElem(i - 1, prior_e); 234 return OK; 235 } 236 237 //在顺序表中顺序查找某元素 238 template <typename ElemType> 239 int SqList<ElemType>::sequentialSearch(ElemType key) 240 { 241 for (int i = 1; i <= n; && key != elem[i - 1]; ++i); 242 243 if (i <= n) 244 return i; 245 else 246 return 0; 247 } 248 249 //**************************系统自动调用初始化构造函数****************************// 250 251 //构造函数 252 template <typename ElemType> 253 SqList<ElemType>::SqList() 254 { 255 elem = new ElemType[LIST_MAX_SIZE]; 256 assert(elem != 0); 257 listSize = LIST_MAX_SIZE; 258 n = 0; 259 } 260 261 //顺序表析构函数 262 template <typename ElemType> 263 SqList<ElemType>::~SqList() 264 { 265 delete[] elem; 266 } 267 268 //顺序表初始化构造函数 269 template <typename ElemType> 270 SqList<ElemType>::SqList(const SqList<ElemType>& otherL) 271 { 272 elem = new ElemType[otherL.listSize]; 273 assert(elem != 0); 274 listSize = otherL.listSize; 275 n = otherL.n; 276 for (int i = 1; i <= n; ++i) 277 elem[i - 1] = otherL.elem[i - 1]; 278 } 279 280 ////////////////////////////////////////////////////////////////////////////////////// 281 282 //输入顺序表 283 template <typename ElemType> 284 void SqList<ElemType>::input() 285 { 286 cout << "请输入顺序表元素的个数:"; 287 cin >> n; 288 cout << "请输入顺序表:"; 289 for (int i = 0; i<n; ++i) 290 cin >> elem[i]; 291 } 292 293 294 // 输出顺序表 295 296 template <typename ElemType> 297 void SqList<ElemType>::display() const 298 { 299 cout << "当前顺序表有" << n << "个元素,分别为:" << endl; 300 for (int i = 1; i <= n; ++i) 301 cout << "[" << i << "] "; 302 cout << endl; 303 for (int i = 1; i <= n; ++i) 304 cout << " " << elem[i - 1] << " "; 305 cout << endl; 306 cout << endl; 307 } 308 309 310 //随机生成顺序表 311 312 template <typename ElemType> 313 void SqList<ElemType>::randSqList() 314 { 315 srand((unsigned)time(NULL)); 316 317 cout << "用以下随机数作为当前顺序表的元素:" << endl; 318 319 for (int i = 0; i<listSize; i++) 320 { 321 elem[i] = rand() % 100 + 1; 322 cout << elem[i] << " "; 323 n++; 324 } 325 cout << endl; 326 // cout<<endl<<"输出随机生成的顺序表,"; 327 /*for(int i=1;i<=n;++i) 328 cout<<"["<<i<<"] "; 329 cout<<endl; 330 for(int i=1;i<=n;++i) 331 cout<<" "<<elem[i-1]<<" "; 332 cout<<endl;*/ 333 //display( ); 334 } 335 336 337 //////////////////////////////////////////////////////////////////////////////// 338 339 //功能:判断两整数是否相等 340 //输入:函数的值参a和b为需要比较的两个整数 341 //输出:若a等于b,函数的返回值为OK;否则为ERROR 342 Status equal(int a, int b) 343 { 344 if (a == b) 345 return OK; 346 else 347 return ERROR; 348 } 349 350 //功能:判断一个整数是否大于另一个整数 351 //输入:函数的值参a和b为需要比较的两个整数 352 //输出:若a大于b,函数的返回值为OK;否则为ERROR 353 Status great(int a, int b) 354 { 355 if (a>b) 356 return OK; 357 else 358 return ERROR; 359 } 360 361 //功能:判断一个整数是否小于另一个整数 362 //输入:函数的值参a和b为需要比较的两个整数 363 //输出:若a小于b,函数的返回值为OK;否则为ERROR 364 Status small(int a, int b) 365 { 366 if (a<b) 367 return OK; 368 else 369 return ERROR; 370 } 371 372 ////////////////////////////////////////////////////////////////////////////////////// 373 374 //求两顺序表的并集 375 template <typename ElemType> 376 void SqList <ElemType>::unionSet(SqList <ElemType> firstL, SqList <ElemType> secondL) 377 { 378 ElemType e; 379 SqList <ElemType> thirdL(firstL); 380 381 for (int i = 1; i <= secondL.getLength(); ++i) 382 { 383 cout << thirdL.n << endl; 384 secondL.getElem(i, e); 385 if (!firstL.locateElem(e, equal)) 386 { //++thirdL.n; 387 thirdL.insert(thirdL.n + 1, e); 388 } 389 } 390 thirdL.display(); 391 }
//派生图书信息类 #include"Sqlist.h" #include<string> #include <sstream> #include <strstream> using namespace std;//用string必带 enum book { children_book, learn_book, tool_book, story_book, Science_book }; struct Book_data { int ID; string name; double price; book type; bool borrow; }; class Dc_wr { private: static int n; public: static bool Write_dc(string a,string b)//追加 { ofstream f1(b, ios::app); if (!f1) return false;//打开文件失败则结束运行 f1 << a << endl;//使用插入运算符写文件内容 f1.close();//关闭文件 n++; return true; } static bool Write_fg(string a, string b)//覆盖 { ofstream f1(b); if (!f1) return false;//打开文件失败则结束运行 f1 << a << endl;//使用插入运算符写文件内容 f1.close();//关闭文件 n++; return true; } static bool Read_dc(string &a,string b) { ifstream f1(b); if (!f1) { f1.close(); return false; } f1 >> a; f1.close(); return true; } static bool Read_zline(string &a, int b,string c) { ifstream f1(c); if ((!f1) && b <= n) { f1.close(); return false; } for (int i = 1; i <= b; i++) f1 >> a; f1.close(); return true; } static bool Read_star(string &a, int b)//程序开始时取出文本指定行数据返回字符串 { string c;//接受不要的行字符串 ifstream f1("library.txt"); for (int i = 1; i <= b; i++) { if (i == b) { f1 >> c; if (c != "") { a = c; f1.close(); return true; } else { f1.close(); return false; } } f1 >> c; if (c == "") { f1.close(); return false; } } f1.close(); return false; } static bool Del(int b) { string a; ofstream f2("library(复本).txt", ios::app); if (!f2) return false; ifstream f1("library.txt"); if (!f1) return false; for (int i=0; ;i++) { f1 >> a; if (i == b) continue; if (a == "#") { f2 << a; break; } f2 << a; } f2.close(); f1.close(); ofstream f3("library.txt", ios::trunc); ifstream f4("library.txt(复本).txt"); for (;;) { f4 >> a; f3 << a; if (a == "#") break; } } static bool txt_to_txt(string b, string c, bool s)//用b覆盖c 清空b s为假添加#结束标记 { string a; ofstream f2(c, ios::trunc);//of写 if读 ios::trunc清空原文本后打开 ifstream f1(b); for (;;) { f1 >> a; if (a == "#")//遇到结束标记则关闭 break; if (a == "")//读出字符串为空则关闭 break; a = a + '\n';//换行 f2 << a; } f2.close(); if (!s)//根据参数判断是否给予结束标记 { Dc_wr::Write_dc("#", c); } return true; } }; class Book_in :public SqList<Book_data> { public: ~Book_in() { Class_totxt(); Dc_wr::Write_dc("#", "library(复本).txt");//加上结束标记方便下一次程序对文本读取 } int Find_book(int ID); int Find_book(string name); bool Add_book(Book_data e,bool a); string Record_tostring(Book_data e); void txt_toclass(); void Out_book(int i); void Borrow_book(); void Class_totxt(); void Return_book(); void Change_book(); }; bool Book_in::Add_book(Book_data e, bool a)//把e添加到类 如果a为真则添加到存本 { if (a) { string record = Record_tostring(e); Dc_wr::Write_dc(record, "library.txt"); cout << record << "添加到文本成功" << endl; } string record = Record_tostring(e); if (insert(n, e)) { cout << record << "填加到class成功" << endl; return true; } else return false; } string Book_in::Record_tostring(Book_data e) { string chge; strstream change; string record = ""; change << e.ID; change >> chge; record = record + chge + "|" + e.name; strstream change3; change3 <<e.price; change3 >> chge; record = record + "|" + chge; switch (e.type) { case children_book:record = record + "|" + "1"; break; case learn_book:record = record + "|" + "2"; break; case tool_book:record = record + "|" + "3"; break; case story_book:record = record + "|" + "4"; break; case Science_book:record = record + "|" + "5"; break; default: break; }; if (e.borrow) record = record + "|" + "1"; else record = record + "|" + "2"; return record; } void Book_in::txt_toclass() { int x, y; Book_data e;//把分割好的字符串转化为相应的类型赋值成为一个Bool_data类型 for (int p=1;;p++)//p控制接受输出字符串的数组 { int i = 0; string a; string data[5]; Book_data check; if (!Dc_wr::Read_star(a, p)) return; //if (a == "#") // return; for each (char b in a)//利用for each取出字符串存放在数组 { if (i == 5)//1个Book_data中有5值 break; if (b != '|') { data[i] = data[i] + b; } else { i++; } } //对应的数据类型转换 stringstream f1; f1 << data[0]; f1 >> e.ID; e.name = data[1]; stringstream f2; f2 << data[2]; f2 >> e.price; stringstream f3; f3 << data[3]; f3 >> x; switch (x) { case 1: e.type = children_book; break; case 2: e.type = learn_book; break; case 3: e.type = tool_book; break; case 4: e.type = story_book; break; case 5: e.type = Science_book; break; default: break; } stringstream f4; f4 << data[4]; f4 >> y; switch (y) { case 1: e.borrow = true; break; case 2: e.borrow = false; break; default: break; } //对应的数据类型转换 if (check.ID == e.ID) return; Add_book(e, false);//把Book_data 填加到类 check = e; } } void Book_in::Out_book(int i) { cout << elem[i].ID << " " << elem[i].name << " " << elem[i].price << " "; switch (elem[i].type) { case children_book:cout << "儿童类" << " "; break; case learn_book:cout << "学习类" << " "; break; case tool_book:cout << "工具类" << " "; break; case story_book:cout << "小说类" << " "; break; case Science_book:cout << "科学类" << " "; break; default: break; }; if (elem[i].borrow) cout << "被借阅" << endl; else cout << "未被借阅" << endl; } int Book_in::Find_book(int ID) { for (int i = 0; i <=n - 1;i++) { if (elem[i].ID == ID) { Out_book(i); return i; } } cout << "没有找到相关书籍" << endl; return -1; } int Book_in::Find_book(string name) { for (int i = 0; i <= n - 1; i++) { if (elem[i].name == name) { Out_book(i); return i; } } cout << "没有找到相关书籍" << endl; return -1; } void Book_in::Borrow_book() { int ID; int i; char t; p: cout << "请输入书籍ID" << endl; cin >> ID; i = Find_book(ID); if (i+1) { if (!elem[i].borrow) { cout << "该书现在可以借出" << endl; cout << "是否借书?(y or anykey)" << endl; cin >> t; if (t == 'y') elem[i].borrow = true; cout << "OK" << endl; } else cout << "该书现在已经被借走" << endl; } else { cout << "没有找到请重新输入" << endl; goto p; } } void Book_in::Class_totxt() { Book_data e; string a; for (int i = 1; i <= n; i++) { getElem(i, e); a=Record_tostring(e); if (i == 1) Dc_wr ::Write_fg(a, "library(复本).txt");//第一行覆盖原文本 else Dc_wr ::Write_dc(a, "library(复本).txt");//追加 } } void Book_in::Return_book() { int ID; int i; cout << "请输入书籍ID" << endl; cin >> ID; i = Find_book(ID); if (i + 1) { elem[i].borrow = false; cout << "已完成还书!" << endl; } } void Book_in::Change_book() { int ID; int change; int i; cout << "请你输入你要修改书的ID" << endl; cin >> ID; i = Find_book(ID); if (i + 1) { cout << "找到图书!" << endl; } else { cout << "未找到图书" << endl; return; } cout << "请输入要修改的项:"<<"1.ID" << " 2.书名"<< " 3.价格"<< " 4.书类"<< " 5.是否被借阅"<<"6.删除整个书的信息"<< endl; cin >> change; switch (change) { case 1: { int a; cout << "请输入你要改的ID" << endl; cin >> a; elem[i].ID = a; cout << "修改成功!" << endl; }; break; case 2: { string a; cout << "请输入你要改的书名" << endl; cin >> a; elem[i].name = a; cout << "修改成功!" << endl; }; break; case 3: { int a; cout << "请输入你要改的价格" << endl; cin >> a; elem[i].price = a; cout << "修改成功!" << endl; }; break; case 4: { int a; string c[] = { "儿童类", "学习类", "工具类", "小说类", "科学类" }; cout << "请输入你要修改的书的类别(1." << c[0] << " 2." << c[1] << " 3." << c[2] << " 4." << c[3] << " 5." << c[4] << endl; cin >> a; switch (a) { case 1: elem[i].type = children_book; break; case 2: elem[i].type = learn_book; break; case 3: elem[i].type = tool_book; break; case 4: elem[i].type = story_book; break; case 5: elem[i].type = Science_book; break; default: break; } cout << "修改成功!" << endl; }; break; case 5: { if (elem[i].borrow) elem[i].borrow = false; else elem[i].borrow = true; cout << "修改成功!" << endl; } case 6: { Book_data a; deleteElem(i+1, a); cout << "修改成功!" << endl; } default: break; } }
//mian #include<iostream> #include<fstream> //#include"Query_sequence.h" #include"Book_information.h" using namespace std; int Dc_wr::n = 0; int Menu() { int choice; system("cls"); cout << "****************************************************图书管理系统****************************************************" << endl; //cout << "①⑧⑨⑩" << endl; cout << "▓ ①增加图书 ▓" << endl; cout << "▓ ②查看现有图书 ▓" << endl; cout << "▓ ③借书 ▓" << endl; cout << "▓ ④还书 ▓" << endl; cout << "▓ ⑤修改图书 ▓" << endl; cout << "▓ ⑥查找书籍 ▓" << endl; cout << "****************************************************图书管理系统****************************************************" << endl; cout << "请选择:"; cin >> choice; return choice; } void Add(Book_in& library) { Book_data a; //Book_data b; //Book_data c; book type; int b; string o; string c[] = { "儿童类", "学习类", "工具类", "小说类", "科学类" }; cout << "请输入ID" << endl; cin >> a.ID; cout << "请输入书名" << endl; cin >> a.name; cout << "请输入书的价格" << endl; cin >> a.price; book_type_xuanz: cout << "请输入数的类别(1." << c[0] << " 2." << c[1] << " 3." << c[2] << " 4." << c[3] << " 5." << c[4] << endl; cin >> b; switch (b) { case 1: a.type = children_book; break; case 2: a.type = learn_book; break; case 3: a.type = tool_book; break; case 4: a.type = story_book; break; case 5: a.type = Science_book; break; default:cout << "选择错误!请重新选择" << endl; goto book_type_xuanz; }; getchar(); cout << "书是否被借出(n OR y)" << endl; cin >> o; if (o == "n") a.borrow = false; else a.borrow = true; library.Add_book(a, true); //a.ID = 1; //a.borrow = false; //a.name = "first"; //a.price = 12; //a.type = tool_book; //b.ID = 2; //b.borrow = false; //b.name = "scend"; //b.price = 13; //b.type = learn_book; //c.ID = 3; //c.borrow = true; //c.name = "third"; //c.price = 14; //c.type = story_book; //library.Add_book(a,true); //library.Add_book(b,true); //library.Add_book(c,true); } int main() { Book_in library;//一个图书类 int choice;//用户菜单选择 Book_data h; Dc_wr::txt_to_txt("library(复本).txt", "library.txt",true);//以复本覆盖存本 library.txt_toclass();//根据存本提出字符串转化为类 start: choice=Menu(); switch (choice) { case 1: { for (char m = 'y'; m != 'n';) { Add(library); getchar(); cout << "是否继续添加?(n OR y)" << endl; m=getchar(); } } break; case 2: { if (library.n == 0) { cout << "无" << endl; } cout << "ID" << " " << "书名" << " " << "价格" << " " << "类型" << " " << "是否被借阅" << endl; for (int i = 0; i <=library.n - 1; i++) library.Out_book(i); }; break; case 3: { library.Borrow_book(); } ; break; case 4: { library.Return_book(); } ; break; case 5: { library.Change_book(); } ; break; case 6: { int p; int ID; string name; cout << "通过ID查找输入1 通过书名查找输入2" << endl; cin >> p; if (p == 1) { cout << "请输入ID" << endl; cin >> ID; cout << "相关信息" << endl; library.Find_book(ID); } else { cout << "请输入书名" << endl; cin >> name; cout << "相关信息" << endl; library.Find_book(name); } } default: break; }; char k; cout << "是否继续选择?(anykey or y)" << endl; cin >> k; if (k == 'y') goto start; system("pause"); return 0; }