VS2010下用C++编写图书管理小程序;(程序主要代码来自明日科技的书,但版本有差异,所以进行了部分调试和修改。)

来源:互联网 发布:js改变display样式 编辑:程序博客网 时间:2024/05/29 12:31

各位朋友。

       这是我的第一个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;
     }
   }
}

有关 这个显示窗口大小更改问题。可以在窗口生成后,鼠标右键点击标题栏,选择属性进行修改。不清楚的可以看下面的图。

1 0
原创粉丝点击