C/C++基础例子(1)图书管理系统

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_31437863/article/details/85646278
  1. 一共是三部分:
    1. 类的设计
    2. 类的实现
    3. 主逻辑函数

类的设计

//////////////////////////////////////////////////////////////////////////
//该类实现图书的写入和删除,
//
//////////////////////////////////////////////////////////////////////////
#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];
private:

};

2.类的实现

#define _CRT_SECURE_NO_WARNINGS
#include "CBook.h"
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

//////////////////////////////////////////////////////////////////////////
//CBook类的具体实现都在这里
//////////////////////////////////////////////////////////////////////////

//私有成员要这样,共有成员直接写,不用加类
CBook::CBook(char *cName, char *clsbn, char *cPrice, char *cAuthor)
{
	strncpy(m_cName, cName, NUM1);
	strncpy(m_clsbn, clsbn, NUM1);
	strncpy(m_cPrice, cPrice, NUM2);
	strncpy(m_cAuthor, cAuthor, NUM2);
}

char * CBook::GetName()
{
	return m_cName;
}
void CBook::SetName(char *cName)
{
	strncpy(m_cName, cName, NUM1);
}
char *CBook::Getlsbn()
{
	return m_clsbn;
}
void CBook::Setlsbn(char *clsbn)
{
	strncpy(m_clsbn, clsbn, NUM1);
}
char *CBook::GetPrice()
{
	return m_cPrice;
}
void CBook::SetPrice(char *cPrice)
{
	strncpy(m_cPrice, cPrice, NUM2);
}
char *CBook::GetAuthor()
{
	return m_cAuthor;
}

void CBook::SetAuthor(char *cAuthor)
{
	strncpy(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 occurred");
		ofile.close();
	}
	ofile.close();
	
}
//实现对图书的删除
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(clsbn, NUM1);
		if (ifile.tellg() > 0)
			strncpy(m_clsbn, clsbn, NUM1);
		ifile.read(cPrice, NUM1);
		if (ifile.tellg() > 0)
			strncpy(m_cPrice, cPrice, NUM2);
		ifile.read(cAuthor, NUM1);
		if (ifile.tellg() > 0)
			strncpy(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("bool.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);
			file.read(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2);
			tmpfile.write(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2);
		}
	}
	tmpfile.close();
	ofile.close();
	remove("temp.dat");
}

3.主函数逻辑

#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include "CBook.h"
#define CMD_COLS 80
#define CMD_LINES 25
using namespace std;
void MainLoop();
void ViewData(int iSelPage);
//////////////////////////////////////////////////////////////////////////
//1. 更新窗口
//2. 
//////////////////////////////////////////////////////////////////////////

void SetScreenGrid()
{
	char sysSetBuf[80];
	sprintf(sysSetBuf,"mode con cols=%d lines=%d",CMD_COLS,CMD_LINES);//设置控制台大小
	system(sysSetBuf);

}
void SetSysCaption()
{
	system("title Sample");
}
void ClearScreen()
{
	system("cls");
}
void SetSysCaption(const char *pText)
{
	char sysSetBuf[80];
	sprintf(sysSetBuf, "title %s",pText);//设置控制台大小
	system(sysSetBuf);
}
void ShowWelcome()
{
	for (int i = 0; i < 7;i++)
	{
		cout << endl; //换行
	}
	cout << setw(40);
	cout << "**************" << endl;
	cout << setw(39);
	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;
}
//////////////////////////////////////////////////////////////////////////
//获得输入;
//////////////////////////////////////////////////////////////////////////
int GetSelect()
{
	char buf[256];
	gets(buf);
	return atoi(buf); //把字符串转换成一个数字,返回int型,不能超过int型
	
}
void WaitUser()
{
	int iInputPage = 0;
	cout << "enter 返回主菜单 q 退出" << endl;
	char buf[256];
	gets(buf);
	if (buf[0]=='q')
	{
		system("exit");
	}
}
void GuideInput()
{
	char inName[NUM1];
	char inIsbn[NUM1];
	char inPrince[NUM2];
	char inAuthor[NUM2];

	cout << "输入书名" << endl;
	cin >> inName;
	cout << "输入ISBN" << endl;
	cin >> inIsbn;
	cout << "输入价格" << endl;
	cin >> inIsbn;
	cout << "输入作者" << endl;
	cin >> inAuthor;
	CBook book(inName, inIsbn, inPrince, inAuthor);
	book.WriteData();
	cout << "写入结束";
	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 WaitView(int iCurPage)
{
	char buf[256];
	gets(buf);
	if (buf[0]=='q')
	{
		system("exit");
	}
	if (buf[0]=='m')
	{
		MainLoop();
	}
	if (buf[0]=='n')
	{
		ViewData(iCurPage);
	}
}

void DeleteBookFromFile()
{
	int iDelCount;
	cout << "输入要删除的index" << endl;
	cin >> iDelCount;
	CBook tmpbook;
	tmpbook.DeleteData(iDelCount);
	cout << "Delete Finish" << endl;
	WaitUser();
}
//////////////////////////////////////////////////////////////////////////
//每次显示20条记录,默认从第一条记录开始读取
//////////////////////////////////////////////////////////////////////////
void ViewData(int iSelPage=1)
{
	int iPage = 1;
	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);
	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 << "显示下一页 m 返回"<<endl;
	cout << setw(5)<<"Index";
	cout << setw(22)<<"Name"<<setw(22)<<"Isbn";
	cout << setw(15) << "Price" << setw(15)<<"Author";
	cout << endl;
	try
	{
		//一次读取20个,如果还有在读
		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)//tellg返回当前指针位置
				{
					bIndex = false;
				}
				else
				{
					bIndex = true;
				}
			}
		}
	}
	catch (...)
	{
		cout<< "file error occurred"<<endl;
		
		throw "file error occurred";
		 
		ifile.close();
	}
	if (iCurPage<iPage)
	{
		iCurPage = iCurPage + 1;
		WaitView(iCurPage);
	}
	else
	{
		ViewData(iCurPage);
	}
	ifile.close();
}
void MainLoop()
{
	ShowWelcome();
	while (1)
	{
		ClearScreen();
		ShowWelcome();
		ShowRootMenu();
		switch (GetSelect())
		{

		case 1:
			ClearScreen();
			GuideInput();
			break;
		case 2:
			ClearScreen();
			ViewData();
			break;
		case 3:
			cout << "执行到 3 这里" << endl;
			break;
		default:
			break;
		}
	}
}
void main()
{
	SetScreenGrid();
	SetSysCaption("图书馆里系统");
	MainLoop();
}

结果:

猜你喜欢

转载自blog.csdn.net/baidu_31437863/article/details/85646278
今日推荐