最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實現(xiàn)圖書管理系統(tǒng)(文件操作與類)

 更新時間:2022年03月12日 12:14:12   作者:糖醋web排骨  
這篇文章主要為大家詳細介紹了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 << "--------------------------------------歡迎進入圖書管理系統(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 << "請輸入要進行的操作:";
?? ??? ?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é)習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • VC++實現(xiàn)的OpenGL線性漸變色繪制操作示例

    VC++實現(xiàn)的OpenGL線性漸變色繪制操作示例

    這篇文章主要介紹了VC++實現(xiàn)的OpenGL線性漸變色繪制操作,結(jié)合實例形式分析了VC++基于OpenGL進行圖形繪制的相關(guān)操作技巧,需要的朋友可以參考下
    2017-07-07
  • 解讀C++編程的相關(guān)文件操作

    解讀C++編程的相關(guān)文件操作

    這篇文章主要介紹了解讀C++編程的相關(guān)文件操作,是C++入門學(xué)習中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-09-09
  • Qt中PaintEvent繪制實時波形圖的實現(xiàn)示例

    Qt中PaintEvent繪制實時波形圖的實現(xiàn)示例

    本文主要介紹了Qt中PaintEvent繪制實時波形圖的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2022-06-06
  • C++的多態(tài)與虛函數(shù)你了解嗎

    C++的多態(tài)與虛函數(shù)你了解嗎

    這篇文章主要為大家詳細介紹了C++多態(tài)與虛函數(shù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • 關(guān)于STL中l(wèi)ist容器的一些總結(jié)

    關(guān)于STL中l(wèi)ist容器的一些總結(jié)

    list就是數(shù)據(jù)結(jié)構(gòu)中的雙向鏈表(根據(jù)sgi stl源代碼),因此它的內(nèi)存空間是不連續(xù)的,通過指針來進行數(shù)據(jù)的訪問,這個特點使得它的隨即存取變的非常沒有效率,因此它沒有提供[]操作符的重載
    2013-09-09
  • 簡單總結(jié)C++中的修飾符類型

    簡單總結(jié)C++中的修飾符類型

    這篇文章主要介紹了C++中的修飾符類型總結(jié),是C++入門學(xué)習中的基礎(chǔ)知識,需要的朋友可以參考下
    2016-05-05
  • C++實現(xiàn)LeetCode(109.將有序鏈表轉(zhuǎn)為二叉搜索樹)

    C++實現(xiàn)LeetCode(109.將有序鏈表轉(zhuǎn)為二叉搜索樹)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(109.將有序鏈表轉(zhuǎn)為二叉搜索樹),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C中的volatile使用方法

    C中的volatile使用方法

    volatile 影響編譯器編譯的結(jié)果,指出,volatile 變量是隨時可能發(fā)生變化的,與volatile變量有關(guān)的運算,不要進行編譯優(yōu)化,以免出錯
    2013-02-02
  • 提高C++程序運行效率的10個簡單方法

    提高C++程序運行效率的10個簡單方法

    這篇文章主要介紹了提高C++程序運行效率的10個簡單方法,包括了循環(huán)、變量、繼承等等應(yīng)用的技巧,非常具有實用價值,需要的朋友可以參考下
    2014-09-09
  • C++實現(xiàn)LeetCode(202.快樂數(shù))

    C++實現(xiàn)LeetCode(202.快樂數(shù))

    這篇文章主要介紹了C++實現(xiàn)LeetCode(202.快樂數(shù)),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08

最新評論

老河口市| 聂拉木县| 迁安市| 桐梓县| 台州市| 岑巩县| 南平市| 淳安县| 综艺| 屯门区| 和静县| 大同县| 杨浦区| 长子县| 永德县| 武鸣县| 荆州市| 朝阳区| 长宁县| 芜湖市| 长阳| 进贤县| 塔城市| 呈贡县| 十堰市| 丰宁| 襄樊市| 酒泉市| 南和县| 郯城县| 恭城| 延长县| 张家港市| 夹江县| 惠水县| SHOW| 唐海县| 赤城县| 黎城县| 都兰县| 浑源县|