C++實現(xiàn)圖書管理系統(tǒng)(文件操作與類)
更新時間:2022年03月12日 12:14:12 作者:糖醋web排骨
這篇文章主要為大家詳細介紹了C++實現(xiàn)圖書管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C++實現(xiàn)圖書管理系統(tǒng)的具體代碼,供大家參考,具體內容如下
(1)定義圖書類;
(2)圖書信息包括:書名name,價格price,庫存num;
(3)可以查詢、增加、刪除、修改功能;
(4)使用文件保存及讀取圖書數(shù)據;
#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 << "--------------------------------------歡迎進入圖書管理系統(tǒng)--------------------------------------" << endl;
?? ?cout << endl << "0 - 退出系統(tǒng);" << "1 - 顯示庫存;" << "2 - 查詢圖書;" << "3 - 借閱圖書;" << "4 - 歸還圖書;" << "5 - 增加圖書;" << "6 - 刪除圖書;" << endl;
}
?
class Booklist?? ?//創(chuàng)建BookList類,數(shù)據成員有Book還有圖書數(shù)量
{
public:
?? ?void save()?? ?//新建圖書的話保存數(shù)據,用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ù)據,因為刪除了某個元素
?? ??? ?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ù)據
?? ?{
?? ??? ?Book B;
?? ??? ?B.Set();
?? ??? ?BList.push_back(B);
?? ??? ?num++;
?? ?}
?? ?void start()?? ?//程序一開始讀取文件里的數(shù)據
?? ?{
?? ??? ?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,所以符合關鍵字的都會被找到
?? ??? ?{
?? ??? ??? ?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 << "請輸入要進行的操作:";
?? ??? ?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;
?? ??? ?}
?
?? ?}
}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
VC++實現(xiàn)的OpenGL線性漸變色繪制操作示例
這篇文章主要介紹了VC++實現(xiàn)的OpenGL線性漸變色繪制操作,結合實例形式分析了VC++基于OpenGL進行圖形繪制的相關操作技巧,需要的朋友可以參考下2017-07-07
Qt中PaintEvent繪制實時波形圖的實現(xiàn)示例
本文主要介紹了Qt中PaintEvent繪制實時波形圖的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06
C++實現(xiàn)LeetCode(109.將有序鏈表轉為二叉搜索樹)
這篇文章主要介紹了C++實現(xiàn)LeetCode(109.將有序鏈表轉為二叉搜索樹),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-07-07
C++實現(xiàn)LeetCode(202.快樂數(shù))
這篇文章主要介紹了C++實現(xiàn)LeetCode(202.快樂數(shù)),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-08-08

