C++實現(xiàn)圖書管理系統(tǒng)(文件操作與類)
本文實例為大家分享了C++實現(xiàn)圖書管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
(1)定義圖書類;
(2)圖書信息包括:書名name,價格price,庫存num;
(3)可以查詢、增加、刪除、修改功能;
(4)使用文件保存及讀取圖書數(shù)據(jù);
#include<iostream> using namespace std; #include<fstream> #define filename "booklist.txt" #include<list> #include<cstring> #include<iomanip> ? class Book?? ?//創(chuàng)建Book類,存放圖書信息 { public: ?? ?Book(string na=" ", int p=0, int n=0) ?? ?{ ?? ??? ?name = na; ?? ??? ?num = n; ?? ??? ?price = p; ?? ?} ?? ?void Show() ?? ?{ ?? ??? ?cout << ?"書名:" << std::left << setw(20) << name << std::right << setw(6) << "\t價格:" << price << "\t數(shù)量:" << num << endl; ?? ?} ?? ?void Set() ?? ?{ ?? ??? ?cout << "請輸入書名:"; ?? ??? ?cin >> name; ?? ??? ?cout << "請輸入價格:"; ?? ??? ?cin >> price; ?? ??? ?cout << "請輸入數(shù)量:"; ?? ??? ?cin >> num; ?? ?} ?? ?void Addnum() ?? ?{ ?? ??? ?int n; ?? ??? ?cout << "請輸入歸還的數(shù)量:"; ?? ??? ?cin >> n; ?? ??? ?num += n; ?? ?} ?? ?void Borrownum() ?? ?{ ?? ??? ?int n; ?? ??? ?cout << "請輸入借出的數(shù)量:"; ?? ??? ?cin >> n; ?? ??? ?num -= n; ?? ?} public: ?? ?string name; ?? ?int price; ?? ?int num; }; ? void menu() { ?? ?cout << "--------------------------------------歡迎進(jìn)入圖書管理系統(tǒng)--------------------------------------" << endl; ?? ?cout << endl << "0 - 退出系統(tǒng);" << "1 - 顯示庫存;" << "2 - 查詢圖書;" << "3 - 借閱圖書;" << "4 - 歸還圖書;" << "5 - 增加圖書;" << "6 - 刪除圖書;" << endl; } ? class Booklist?? ?//創(chuàng)建BookList類,數(shù)據(jù)成員有Book還有圖書數(shù)量 { public: ?? ?void save()?? ?//新建圖書的話保存數(shù)據(jù),用app方式打開文件 ?? ?{ ?? ??? ?ofstream fout(filename, ios::app); ?? ??? ?list<Book>::iterator it = BList.begin(); ?? ??? ?for (int i = 0; i < num-1; i++)?? ?//偏移迭代器,指向新加入的Book并寫入文件 ?? ??? ?{ ?? ??? ??? ?it++; ?? ??? ?} ?? ??? ?for (; it != BList.end(); it++) ?? ??? ?{ ?? ??? ??? ?fout << (*it).name << ' ' << (*it).price << ' ' << (*it).num << '\n'; ?? ??? ?} ?? ??? ?fout.close(); ?? ?} ?? ?void resave() ?? ?{ ?? ??? ?ofstream fout(filename, ios::out);?? ?//重新寫入數(shù)據(jù),因為刪除了某個元素 ?? ??? ?if (fout.is_open()) ?? ??? ?{ ?? ??? ??? ?for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++) ?? ??? ??? ?{ ?? ??? ??? ??? ?fout << (*it).name << ' ' << (*it).price << ' ' << (*it).num << '\n'; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?fout.close(); ?? ?} ?? ?void Show() ?? ?{ ?? ??? ?for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++) ?? ??? ?{ ?? ??? ??? ?(*it).Show(); ?? ??? ?} ?? ?} ?? ?void adddata()?? ?//添加數(shù)據(jù) ?? ?{ ?? ??? ?Book B; ?? ??? ?B.Set(); ?? ??? ?BList.push_back(B); ?? ??? ?num++; ?? ?} ?? ?void start()?? ?//程序一開始讀取文件里的數(shù)據(jù) ?? ?{ ?? ??? ?string na; ?? ??? ?int n; ?? ??? ?int p; ?? ??? ?ifstream fin(filename, ios::in); ?? ??? ?if (fin.is_open()) ?? ??? ?{ ?? ??? ??? ?while (fin >> na >> p >> n) ?? ??? ??? ?{ ?? ??? ??? ??? ?Book B(na, p, n); ?? ??? ??? ??? ?BList.push_back(B); ?? ??? ??? ??? ?num++; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?fin.close(); ?? ?} ?? ?void increase() ?? ?{ ?? ??? ?cout << "請輸入書名:" << endl; ?? ??? ?string n; ?? ??? ?cin >> n; ?? ??? ?for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++) ?? ??? ?{ ?? ??? ??? ?if ((*it).name == n) ?? ??? ??? ??? ?(*it).Addnum(); ?? ??? ?} ?? ??? ?resave(); ?? ?} ?? ?void decrease() ?? ?{ ?? ??? ?cout << "請輸入書名:" << endl; ?? ??? ?string n; ?? ??? ?cin >> n; ?? ??? ?for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++) ?? ??? ?{ ?? ??? ??? ?if ((*it).name == n) ?? ??? ??? ??? ?(*it).Borrownum(); ?? ??? ?} ?? ??? ?resave(); ?? ?} ?? ?void FindBook() ?? ?{ ?? ??? ?string name; ?? ??? ?cin >> name; ?? ??? ?for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++)?? ?//遍歷整個list,所以符合關(guān)鍵字的都會被找到 ?? ??? ?{ ?? ??? ??? ?int index = (*it).name.find(name);?? ?//如果沒找到返回值是一個很大的數(shù) ?? ??? ??? ?if (index < (*it).name.length()) ?? ??? ??? ??? ?(*it).Show(); ?? ??? ?} ?? ?} ?? ?void DeleteBook() ?? ?{ ?? ??? ?string name; ?? ??? ?cout << "請輸入書名:"; ?? ??? ?cin >> name; ?? ??? ?int i = 0; ?? ??? ?for (list<Book>::iterator it = BList.begin(); it != BList.end();it++) ?? ??? ?{ ?? ??? ??? ?if ((*it).name == name) ?? ??? ??? ??? ?break; ?? ??? ??? ?++i; ?? ??? ?} ?? ??? ?list<Book>::iterator it = BList.begin(); ?? ??? ?advance(it, i); ?? ??? ?BList.erase(it); ?? ??? ?--num; ?? ??? ?resave(); ?? ?} public: ?? ?list<Book>BList; ?? ?int num = 0; }; ? int main() { ?? ?Booklist B1; ?? ?B1.start(); ?? ?while (1) ?? ?{ ?? ??? ?menu(); ?? ??? ?int key; ?? ??? ?cout << "請輸入要進(jìn)行的操作:"; ?? ??? ?cin >> key; ?? ??? ?switch (key) ?? ??? ?{ ?? ??? ?case 0: ?? ??? ??? ?return 0; ?? ??? ??? ?break; ?? ??? ?case 1: ?? ??? ??? ?B1.Show(); ?? ??? ??? ?break; ?? ??? ?case 2: ?? ??? ??? ?B1.FindBook(); ?? ??? ??? ?break; ?? ??? ?case 3: ?? ??? ??? ?B1.decrease(); ?? ??? ??? ?break; ?? ??? ?case 4: ?? ??? ??? ?B1.increase(); ?? ??? ??? ?break; ?? ??? ?case 5: ?? ??? ?{ ?? ??? ??? ?B1.adddata(); ?? ??? ??? ?B1.save(); ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?case 6: ?? ??? ??? ?B1.DeleteBook(); ?? ??? ??? ?break; ?? ??? ?} ? ?? ?} }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++實現(xiàn)LeetCode(119.楊輝三角之二)
這篇文章主要介紹了C++實現(xiàn)LeetCode(119.楊輝三角之二),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C語言詳解如何應(yīng)用模擬字符串和內(nèi)存函數(shù)
這篇文章主要介紹了C語言詳解如何應(yīng)用模擬字符串和內(nèi)存函數(shù),文章有點長,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02C/C++?Linux?Socket網(wǎng)絡(luò)編程流程分析
這篇文章主要介紹了C/C++?Linux?Socket網(wǎng)絡(luò)編程,Linux環(huán)境中的C/C++?socket?與Window環(huán)境中的C/C++?socket類似,本文所記錄的是TCP協(xié)議的socket編程,圖文實例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02利用反射獲得類的public static/const成員的值實例
下面小編就為大家?guī)硪黄梅瓷浍@得類的public static/const成員的值實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12基于matlab實現(xiàn)DCT數(shù)字水印嵌入與提取
數(shù)字水印技術(shù)是將一些標(biāo)識信息直接嵌入數(shù)字載體當(dāng)中,?或間接表示在信號載體中,?且不影響原載體的使用價值。本文主要為大家介紹了基于matlab如何實現(xiàn)數(shù)字水印的嵌入與提取,感興趣的可以學(xué)習(xí)一下2022-01-01C++ 操作系統(tǒng)內(nèi)存分配算法的實現(xiàn)詳解
本文主要介紹了在動態(tài)分區(qū)管理方式下采用不同的分配算法實現(xiàn)主存分配和實現(xiàn)主存回收,旨在幫助學(xué)生理解在動態(tài)分區(qū)管理方式下應(yīng)怎樣實現(xiàn)主存空間的分配和回收。感興趣的可以了解一下2021-11-11