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

C++項目開發(fā)實現(xiàn)圖書管理系統(tǒng)

 更新時間:2022年03月12日 10:36:03   作者:小凡大帝  
這篇文章主要為大家詳細介紹了C++項目開發(fā)實現(xiàn)圖書管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C++實現(xiàn)圖書管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

一、需求分析

1.可以實現(xiàn)添加一條新的圖書信息(圖書名,圖書編號,圖書價格,圖書作者)
2.可以查看全部圖書條目
3.可以刪除指定的某條圖書記錄

二、系統(tǒng)設(shè)計

2.1系統(tǒng)功能介紹

1.添加新圖書模塊:該模塊可以實現(xiàn)將新圖書信息錄入到系統(tǒng)并將圖書信息保存到文件中。
2.瀏覽全部圖書模塊:可以通過該模塊獲取文件中全部圖書信息,確定圖書是否存在,及方便刪除。
3.刪除圖書模塊:可以根據(jù)圖書在文件中的記錄號刪除某條圖書記錄。

2.2系統(tǒng)預(yù)覽

主界面

添加新圖書界面

瀏覽全部圖書條目

三、代碼設(shè)計

3.1 圖書頭文件

#define NUM1 128
#define NUM2 50
class CBook{
?
public:
?? ?CBook(){}
?? ?CBook(char* cName,char*cIsbn,char* cPrice,char* cAuthor);
?? ?~CBook(){}
public:
?? ?char* GetName();//獲取圖書名稱
?? ?void SetName(char* cName);//設(shè)置圖書名稱
?? ?char* GetIsbn();//獲取圖書ISBN編號
?? ?void SetIsbn(char* clsbn);//設(shè)置圖書ISBN編號
?? ?char* GetPrice();//獲得圖書價格
?? ?void SetPrice(char* cPrice);//設(shè)置圖書價格
?? ?char* GetAuthor();//獲得圖書作者信息
?? ?void SetAuthor(char* cAuthor);//設(shè)置圖書作者信息
?? ?void WriteData();
?? ?void DeleteData(int iCount);
?? ?void GetBookFromFile(int iCount);
protected:
?? ?char m_cName[NUM1];
?? ?char m_cIsbn[NUM1];
?? ?char m_cPrice[NUM2];
?? ?char m_cAuthor[NUM2];
};

3.2 類中成員函數(shù)實現(xiàn)

#include "Book.h"
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
using namespace std;
CBook::CBook(char* cName, char*cIsbn, char* cPrice, char* cAuthor){
?? ?strncpy_s(m_cName,cName,NUM1);
?? ?strncpy_s(m_cIsbn, cIsbn, NUM1);
?? ?strncpy_s(m_cPrice, cPrice, NUM2);
?? ?strncpy_s(m_cAuthor, cAuthor, NUM2);
}
?
char* CBook::GetName(){
?? ?return m_cName;
}
?
void CBook::SetName(char* cName){
?? ?strncpy_s(m_cName, cName, NUM1);
}
?
char* CBook::GetIsbn(){
?? ?return m_cIsbn;
}
?
void CBook::SetIsbn(char* cIsbn){
?? ?strncpy_s(m_cIsbn, cIsbn, NUM1);
}
?
char* CBook::GetPrice(){
?? ?return m_cPrice;
}
?
void CBook::SetPrice(char*cPrice){
?? ?strncpy_s(m_cPrice, cPrice, NUM2);
}
?
char* CBook::GetAuthor(){
?? ?return m_cAuthor;
}
?
void CBook::SetAuthor(char* cAuthor){
?? ?strncpy_s(m_cAuthor, cAuthor, NUM2);
}
?
?
void CBook::WriteData()
{
?? ?ofstream ofile;
?? ?ofile.open("book.dat", ios::binary | ios::app);
?? ?try
?? ?{
?? ??? ?ofile.write(m_cName, NUM1);
?? ??? ?ofile.write(m_cIsbn, NUM1);
?? ??? ?ofile.write(m_cPrice, NUM2);
?? ??? ?ofile.write(m_cAuthor, NUM2);
?? ?}
?? ?catch (...)
?? ?{
?? ??? ?throw "file error occurred";
?? ??? ?ofile.close();
?? ?}
?? ?ofile.close();
}
void CBook::GetBookFromFile(int iCount)
{
?? ?char cName[NUM1];
?? ?char cIsbn[NUM1];
?? ?char cPrice[NUM2];
?? ?char cAuthor[NUM2];
?? ?ifstream ifile;
?? ?ifile.open("book.dat", ios::binary);
?? ?try
?? ?{
?? ??? ?ifile.seekg(iCount*(NUM1 + NUM1 + NUM2 + NUM2), ios::beg);
?? ??? ?ifile.read(cName, NUM1);
?? ??? ?if (ifile.tellg()>0)
?? ??? ??? ?strncpy_s(m_cName, cName, NUM1);
?? ??? ?ifile.read(cIsbn, NUM1);
?? ??? ?if (ifile.tellg()>0)
?? ??? ??? ?strncpy_s(m_cIsbn, cIsbn, NUM1);
?? ??? ?ifile.read(cPrice, NUM2);
?? ??? ?if (ifile.tellg()>0)
?? ??? ??? ?strncpy_s(m_cIsbn, cIsbn, NUM2);
?? ??? ?ifile.read(cAuthor, NUM2);
?? ??? ?if (ifile.tellg()>0)
?? ??? ??? ?strncpy_s(m_cAuthor, cAuthor, NUM2);
?? ?}
?? ?catch (...)
?? ?{
?? ??? ?throw "file error occurred";
?? ??? ?ifile.close();
?? ?}
?? ?ifile.close();
}
void CBook::DeleteData(int iCount)
{
?? ?long respos;
?? ?int iDataCount = 0;
?? ?fstream file;
?? ?fstream tmpfile;
?? ?ofstream ofile;
?? ?char cTempBuf[NUM1 + NUM1 + NUM2 + NUM2];
?? ?file.open("book.dat", ios::binary | ios::in | ios::out);
?? ?tmpfile.open("temp.dat", ios::binary | ios::in | ios::out | ios::trunc);
?? ?file.seekg(0, ios::end);
?? ?respos = file.tellg();
?? ?iDataCount = respos / (NUM1 + NUM1 + NUM2 + NUM2);
?? ?if (iCount < 0 && iCount > iDataCount)
?? ?{
?? ??? ?throw "Input number error";
?? ?}
?? ?else
?? ?{
?? ??? ?file.seekg((iCount)*(NUM1 + NUM1 + NUM2 + NUM2), ios::beg);
?? ??? ?for (int j = 0; j<(iDataCount - iCount); j++)
?? ??? ?{
?? ??? ??? ?memset(cTempBuf, 0, NUM1 + NUM1 + NUM2 + NUM2);
?? ??? ??? ?file.read(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2);
?? ??? ??? ?tmpfile.write(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2);
?? ??? ?}
?? ??? ?file.close();
?? ??? ?tmpfile.seekg(0, ios::beg);
?? ??? ?ofile.open("book.dat");
?? ??? ?ofile.seekp((iCount - 1)*(NUM1 + NUM1 + NUM2 + NUM2), ios::beg);
?? ??? ?for (int i = 0; i<(iDataCount - iCount); i++)
?? ??? ?{
?? ??? ??? ?memset(cTempBuf, 0, NUM1 + NUM1 + NUM2 + NUM2);
?? ??? ??? ?tmpfile.read(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2);
?? ??? ??? ?ofile.write(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2);
?? ??? ?}
?? ?}
?? ?tmpfile.close();
?? ?ofile.close();
?? ?remove("temp.dat");
}

3.3主函數(shù)代碼

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <fstream>
#include "Book.h"
?
#define CMD_COLS 80
#define CMD_LINES 25
using namespace std;
?
void SetScreenGrid();
void ClearScreen();
void SetSysCaption();
void SetSysCaption(const char *pText);
void ShowWelcome();
void ShowRootMenu();
void WaitView(int ?iCurPage);
void WaitUser();
void GuideInput();
int GetSelect();
long GetFileLength(ifstream & ifs);
void ViewData(int iSelPage);
void DeleteBookFromFile();
void mainloop();
?
void SetScreenGrid()
{
?? ?char sysSetBuf[80];
?? ?sprintf_s(sysSetBuf, "mode con cols=%d lines=%d", CMD_COLS, CMD_LINES);
?? ?system(sysSetBuf);
}
void ClearScreen()
{
?? ?system("cls");
}
void SetSysCaption()
{
?? ?system("title Sample");
}
void SetSysCaption(const char *pText)
{
?? ?char sysSetBuf[80];
?? ?sprintf_s(sysSetBuf, "title %s", pText);
?? ?system(sysSetBuf);
}
void ShowWelcome()
{
?? ?for (int i = 0; i<7; i++)
?? ?{
?? ??? ?cout << endl;
?? ?}
?? ?cout << setw(40);
?? ?cout << "**************" << endl;
?? ?cout << setw(40);
?? ?cout << "*圖書管理系統(tǒng)*" << endl;
?? ?cout << setw(40);
?? ?cout << "**************" << endl;
}
void ShowRootMenu()
{
?? ?cout << setw(40);
?? ?cout << "請選擇功能:" << endl;
?? ?cout << endl;
?? ?cout << setw(38);
?? ?cout << "1 添加新書" << endl;
?? ?cout << endl;
?? ?cout << setw(38);
?? ?cout << "2 瀏覽全部" << endl;
?? ?cout << endl;
?? ?cout << setw(38);
?? ?cout << "3 刪除圖書" << endl;
}
?
?
void WaitView(int ?iCurPage)
{
?? ?char buf[256];
?? ?gets_s(buf);
?? ?if (buf[0] == 'q')
?? ??? ?system("exit");
?? ?if (buf[0] == 'm')
?? ??? ?mainloop();
?? ?if (buf[0] == 'n')
?? ??? ?ViewData(iCurPage);
}
void WaitUser()
{
?? ?int iInputPage = 0;
?? ?cout << "enter返回主菜單,q退出" << endl;
?? ?char buf[256];
?? ?gets_s(buf);
?? ?if (buf[0] == 'q')
?? ??? ?system("exit");
}
void GuideInput()
{
?? ?char inName[NUM1];
?? ?char inIsdn[NUM1];
?? ?char inPrice[NUM2];
?? ?char inAuthor[NUM2];
?
?? ?cout << "輸入書名" << endl;
?? ?cin >> inName;
?? ?cout << "輸入ISDN" << endl;
?? ?cin >> inIsdn;
?? ?cout << "輸入價格" << endl;
?? ?cin >> inPrice;
?? ?cout << "輸入作者" << endl;
?? ?cin >> inAuthor;
?? ?CBook book(inName, inIsdn, inPrice, inAuthor);
?? ?book.WriteData();
?? ?cout << "Write Finish" << endl;
?? ?WaitUser();
}
?
int GetSelect()
{
?? ?char buf[256];
?? ?gets_s(buf);
?? ?return atoi(buf);
}
long GetFileLength(ifstream & ifs)
{
?? ?long tmppos;
?? ?long respos;
?? ?tmppos = ifs.tellg();//獲得當前位置
?? ?ifs.seekg(0, ios::end);
?? ?respos = ifs.tellg();
?? ?ifs.seekg(tmppos, ios::beg);//恢復(fù)當前位置
?? ?return respos;
}
?
?
void ViewData(int iSelPage = 1)
{
?? ?int iPage = 0;
?? ?int iCurPage = 0;
?? ?int iDataCount = 0;
?? ?char inName[NUM1];
?? ?char inIsbn[NUM1];
?? ?char price[NUM2];
?? ?char inAuthor[NUM2];
?? ?bool bIndex = false;
?? ?int iFileLength;
?? ?iCurPage = iSelPage;
?? ?ifstream ifile;
?? ?ifile.open("book.dat", ios::binary);//|ios::nocreate
?? ?iFileLength = GetFileLength(ifile);
?? ?iDataCount = iFileLength / (NUM1 + NUM1 + NUM2 + NUM2);
?? ?if (iDataCount >= 1)
?? ??? ?bIndex = true;
?? ?iPage = iDataCount / 20 + 1; //每頁20條記錄
?
?
?? ?ClearScreen();
?
?? ?cout << " 共有記錄" << iDataCount << " ";
?? ?cout << " 共有頁數(shù)" << iPage << " ";
?? ?cout << " 當前頁數(shù)" << iCurPage << " ";
?? ?cout << " n顯示下一頁 m返回" << endl;
?? ?cout << setw(5) << "Index";
?? ?cout << setw(22) << "Name" << setw(22) << "Isbn";
?? ?cout << setw(15) << "Price" << setw(15) << "Author";
?? ?cout << endl;
?? ?try
?? ?{
?? ??? ?ifile.seekg((iCurPage - 1) * 20 * (NUM1 + NUM1 + NUM2 + NUM2), ios::beg);
?? ??? ?if (!ifile.fail())
?? ??? ?{
?? ??? ??? ?for (int i = 1; i<21; i++)
?? ??? ??? ?{
?? ??? ??? ??? ?memset(inName, 0, 128);
?? ??? ??? ??? ?memset(inIsbn, 0, 128);
?? ??? ??? ??? ?memset(price, 0, 50);
?? ??? ??? ??? ?memset(inAuthor, 0, 50);
?? ??? ??? ??? ?if (bIndex)
?? ??? ??? ??? ??? ?cout << setw(3) << ((iCurPage - 1) * 20 + i);
?? ??? ??? ??? ?ifile.read(inName, NUM1);
?? ??? ??? ??? ?cout << setw(24) << inName;
?? ??? ??? ??? ?ifile.read(inIsbn, NUM1);
?? ??? ??? ??? ?cout << setw(24) << inIsbn;
?? ??? ??? ??? ?ifile.read(price, NUM2);
?? ??? ??? ??? ?cout << setw(12) << price;
?? ??? ??? ??? ?ifile.read(inAuthor, NUM2);
?? ??? ??? ??? ?cout << setw(12) << inAuthor;
?? ??? ??? ??? ?cout << endl;//一條紀錄
?? ??? ??? ??? ?if (ifile.tellg()<0)
?? ??? ??? ??? ??? ?bIndex = false;
?? ??? ??? ??? ?else
?? ??? ??? ??? ??? ?bIndex = true;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?catch (...)
?? ?{
?? ??? ?cout << "throw file exception" << endl;
?? ??? ?throw "file error occurred";
?? ??? ?ifile.close();
?? ?}
?? ?if (iCurPage<iPage)
?? ?{
?? ??? ?iCurPage = iCurPage + 1;
?? ??? ?WaitView(iCurPage);
?? ?}
?? ?else
?? ?{
?? ??? ?WaitView(iCurPage);
?? ?}
?? ?ifile.close();
}
?
?
void DeleteBookFromFile()
{
?? ?int iDelCount;
?? ?cout << "Input delete index" << endl;
?? ?cin >> iDelCount;
?? ?CBook tmpbook;
?? ?tmpbook.DeleteData(iDelCount);
?? ?cout << "Delete Finish" << endl;
?? ?WaitUser();
}
void mainloop()
{
?? ?ShowWelcome();
?? ?while (1)
?? ?{
?? ??? ?ClearScreen();
?? ??? ?ShowWelcome();
?? ??? ?ShowRootMenu();
?? ??? ?switch (GetSelect())
?? ??? ?{
?? ??? ?case 1:
?? ??? ??? ?ClearScreen();
?? ??? ??? ?GuideInput();
?? ??? ??? ?break;
?? ??? ?case 2:
?? ??? ??? ?ClearScreen();
?? ??? ??? ?ViewData();
?? ??? ??? ?break;
?? ??? ?case 3:
?? ??? ??? ?ClearScreen();
?? ??? ??? ?DeleteBookFromFile();
?? ??? ??? ?break;
?? ??? ?}
?? ?}
}
void main()
{
?
?? ?SetScreenGrid();
?? ?SetSysCaption("圖書管理系統(tǒng)");
?? ?mainloop();
}

【注】開發(fā)環(huán)境為VS2013控制臺程序
根據(jù)《C++項目開發(fā)全程實錄》修改

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • c語言輕松實現(xiàn)猜數(shù)字小游戲

    c語言輕松實現(xiàn)猜數(shù)字小游戲

    猜數(shù)字是興起于英國的益智類小游戲,起源于20世紀中期,一般由兩個人或多人玩,也可以由一個人和電腦玩。游戲規(guī)則為一方出數(shù)字,一方猜,今天我們來用C實現(xiàn)這個游戲案例
    2022-04-04
  • 基于Turbo C(V2.0)編譯錯誤信息的詳細介紹

    基于Turbo C(V2.0)編譯錯誤信息的詳細介紹

    本篇文章對Turbo C(V2.0)編譯的錯誤信息進行了詳細的介紹。需要的朋友參考下
    2013-05-05
  • C++ HLSL實現(xiàn)簡單的圖像處理功能

    C++ HLSL實現(xiàn)簡單的圖像處理功能

    本文主要介紹了HLSL實現(xiàn)簡單的圖像處理功能的方法,具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • C++11?lambda(匿名函數(shù))表達式詳細介紹

    C++11?lambda(匿名函數(shù))表達式詳細介紹

    lambda 表達式(lambda expression)是一個匿名函數(shù),C++11中的lambda表達式用于定義并創(chuàng)建匿名的函數(shù)對象,以簡化編程工作,下面這篇文章主要給大家介紹了關(guān)于C++11?lambda(匿名函數(shù))表達式的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • C/C++宏定義的可變參數(shù)詳細解析

    C/C++宏定義的可變參數(shù)詳細解析

    在1999年版本的ISO C 標準中,宏可以象函數(shù)一樣,定義時可以帶有可變參數(shù)。宏的語法和函數(shù)的語法類似
    2013-09-09
  • C語言泛型選擇編程示例詳解

    C語言泛型選擇編程示例詳解

    這篇文章主要介紹了C語言泛型選擇編程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • C語言中g(shù)etch()函數(shù)詳解及簡單實例

    C語言中g(shù)etch()函數(shù)詳解及簡單實例

    這篇文章主要介紹了C語言中g(shù)etch()函數(shù)詳解及簡單實例的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • c++實現(xiàn)掃雷小游戲代碼分享

    c++實現(xiàn)掃雷小游戲代碼分享

    這篇文章主要介紹了c++實現(xiàn)掃雷小游戲并附上代碼分享,代碼不能夠?qū)崿F(xiàn)當所查坐標周圍雷的數(shù)量為0時,直接展開周圍坐標,但有一點的知識性參考價值,需要的小伙伴可以參考一下
    2022-02-02
  • C語言基礎(chǔ)知識分享續(xù)篇

    C語言基礎(chǔ)知識分享續(xù)篇

    這篇文章主要介紹了C語言基礎(chǔ)知識分享續(xù)篇的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • C語言零基礎(chǔ)入門(2)

    C語言零基礎(chǔ)入門(2)

    這篇文章主要為大家詳細介紹了C語言零基礎(chǔ)入門的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03

最新評論

吉水县| 甘谷县| 蚌埠市| 湟源县| 隆回县| 南康市| 名山县| 日喀则市| 涞源县| 广元市| 和林格尔县| 自贡市| 庆阳市| 苍山县| 留坝县| 吉安市| 莱阳市| 大同市| 新宾| 亚东县| 乐清市| 五寨县| 习水县| 喀喇| 义马市| 嘉定区| 手游| 抚宁县| 咸宁市| 仙游县| 山丹县| 抚松县| 株洲县| 陇西县| 邮箱| 芦溪县| 星座| 诏安县| 黔西县| 龙泉市| 兖州市|