各位朋友。
這是我的第一個CSDN上的部落格。我将自己按照明日科技的書“C++從入門到精通”這本書上的第18章的圖書管理程式進行了敲寫;由于我的版本和書中版本有差異。導緻部分代碼不太能夠實作。同時自己也在網上搜尋學習了一些東西。當然主要還是按照書上的代碼。因為我還隻是個初學者。寫這個是希望能夠送給像我一樣的初學者。希望能夠對大家有幫助。
下面分别将代碼進行分享。
1 book.h程式
#define NUM1 128
#define NUM2 50
class CBook
{
public:
CBook(){}
CBook(char* cName,char* clsbn,char* cPrice,char* cAuthor);
~CBook(){}
public:
char* GetName();
void SetName(char* cName);
char* Getlsbn();
void Setlsbn(char* clsbn);
char* GetPrice();
void SetPrice(char* cPrice);
char* GetAuthor();
void SetAuthor(char* cAuthor);
void WriteData();
void DeleteData(int iCount);
void GetBookFromFile(int iCount);
protected:
char m_cName[NUM1];
char m_clsbn[NUM1];
char m_cPrice[NUM2];
char m_cAuthor[NUM2];
};
2. book.cpp代碼
#include "StdAfx.h"
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include"Book.h"
using namespace std;
CBook::CBook(char* cName,char* clsbn,char* cPrice,char* cAuthor)
{
strncpy_s(m_cName,cName,NUM1);
strncpy_s(m_clsbn,clsbn,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::Getlsbn()
{
return m_clsbn;
}
void CBook::Setlsbn(char* clsbn)
{
strncpy_s(m_clsbn,clsbn,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_clsbn,NUM1);
ofile.write(m_cPrice,NUM2);
ofile.write(m_cAuthor,NUM2);
}
catch(...)
{
throw"file error occured";
ofile.close();
}
ofile.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|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.data");
}
void CBook::GetBookFromFile(int iCount)
{
char cName[NUM1];
char clsbn[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(m_cName,cName,NUM1);
ifile.read(cPrice,NUM2);
if(ifile.tellg()>0)
strncpy(m_clsbn,clsbn,NUM1);
ifile.read(cAuthor,NUM2);
if(ifile.tellg()>0)
strncpy(m_cAuthor,cAuthor,NUM2);
}
catch(...)
{
throw"file error occured";
ifile.close();
}
ifile.close();
}
實作程式代碼:
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<conio.h>
#include<string>
#include<fstream>
#include"Book.h"
#include "StdAfx.h"
#include<stdio.h>
#define CMD_COLS 80
#define CMD_LINES 25
using namespace std;
void SetScreenGrid() //視窗顯示;
{
char sysSetBuf[80];
//HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
//sprintf_s(sysSetBuf,"mode con: cols=%d",CMD_COLS,CMD_LINES);
//system("mode con cols=30 lines=6");//注釋用代碼無法在vc2010中完成目标。
//SMALL_RECT rc={0,0,80,25};
//SetConsoleWindowInfo(hOut,true,&rc);
system("color 04");
//system(sysSetBuf);
}
void SetsysCaption()
{
system("title Sample");
}
void ClearScreen()
{
system("cls");
}
void ShowWelcome()
{
for (int i=0;i<7;i++)
{
cout<<endl;
}
cout<<setw(40);
cout<<"************"<<endl;
cout<<setw(40);
cout<<"圖書管理系統"<<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 WaitUser()
{
int iInputPage=0;
cout<<"enter 傳回菜單 q 退出"<<endl;
char buf[256];
gets_s(buf);
if(buf[0]=='q')
system("exit");
}
int GetSelect()
{
char buf[256];
gets_s(buf);
return atoi(buf);
}
void GuideInput()
{
char inName[NUM1];
char inlsdn[NUM1];
char inPrice[NUM2];
char inAuthor[NUM2];
cout<<"輸入書名"<<endl;
cin>>inName;
cout<<"輸入ISBN"<<endl;
cin>>inlsdn;
cout<<"輸入價格"<<endl;
cin>>inPrice;
cout<<"輸入作者"<<endl;
cin>>inAuthor;
CBook book(inName,inlsdn,inPrice,inAuthor);
book.WriteData();
cout<<"Write Finish"<<endl;
WaitUser();
}
long GetFileLength(ifstream&ifs)
{
long tmppos;
long respos;
tmppos=ifs.tellg();
ifs.seekg(0,ios::end);
respos=ifs.tellg();
ifs.seekg(tmppos,ios::beg);
return respos;
}
void ViewData(int iSelPage=1)
{
int iPage=0;
int iCurPage=0;
int iDataCount=0;
char inName[NUM1];
char inLsbn[NUM1];
char price[NUM2];
char inAuthor[NUM2];
bool bIndex=false;
int iFileLength;
iCurPage=iSelPage;
ifstream ifile;
ifile.open("book.dat",ios::binary);
//iFileLength=400; //該行用于初期調試時,忽略掉GetFileLength(ifstream&ifs)函數時用;
iFileLength=GetFileLength(ifile);
iDataCount=iFileLength/(NUM1+NUM1+NUM2+NUM2);
if(iDataCount>=1)
bIndex=true;
iPage=iDataCount/20+1;
ClearScreen();
cout<<"共有記錄:"<<iDataCount<<" ";
cout<<"共有頁數:"<<iPage<<" ";
cout<<"目前頁數:"<<iCurPage<<" ";
cout<<"n 顯示下一頁 m 傳回"<<endl;
cout<<setw(5)<<"Index";
cout<<setw(22)<<"Name"<<setw(22)<<"Lsbn";
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(inLsbn,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(inLsbn,NUM1);
cout<<setw(24)<<inLsbn;
ifile.read(price,NUM2);
cout<<setw(10)<<price;
ifile.read(inAuthor,NUM2);
cout<<setw(16)<<inAuthor;
cout<<endl;
if(ifile.tellg()<0)
bIndex=false;
else
bIndex=true;
}
}
}
catch(...)
{
cout<<"throw file exception"<<endl;
throw "file error occured";
ifile.close();
}
if(iCurPage<iPage)
{
iCurPage=iCurPage+1;
WaitUser();//WaitView(iCurPage);
}
else
{
WaitUser();
}
ifile.close();
}
void DeleteBook()
{
int iDelCount;
cout<<"input delete index"<<endl;
cin>>iDelCount;
CBook tmpbook;
tmpbook.DeleteData(iDelCount);
cout<<"Delete Finish"<<endl;
WaitUser();
}
void main()
{
ShowWelcome();
while(1)
{
ClearScreen();
ShowWelcome();
ShowRootMenu();
switch(GetSelect())
{
case 1:
ClearScreen();
GuideInput();
break;
case 2:
ClearScreen();
ViewData();
break;
case 3:
ClearScreen();
DeleteBook();
break;
}
}
}
有關 這個顯示視窗大小更改問題。可以在視窗生成後,滑鼠右鍵點選标題欄,選擇屬性進行修改。不清楚的可以看下面的圖。