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

  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();
}

结果:

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
好的,那么你可以参考以下的代码来编写你的图书管理系统。这是一个简单的例子,它可以帮助你了解如何使用 C 语言编写一个图书管理系统。 ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_BOOKS 100 struct Book { char title[100]; char author[50]; int pages; int year; }; struct Library { struct Book books[MAX_BOOKS]; int num_books; }; void print_book(struct Book* book) { printf("书名:%s\n", book->title); printf("作者:%s\n", book->author); printf("页数:%d\n", book->pages); printf("出版年份:%d\n", book->year); } int main() { struct Library library; library.num_books = 0; while (1) { printf("1. 添加图书\n"); printf("2. 查询图书\n"); printf("3. 退出\n"); int choice; scanf("%d", &choice); if (choice == 1) { if (library.num_books == MAX_BOOKS) { printf("图书馆已满!\n"); } else { printf("请输入图书信息:\n"); printf("书名:"); scanf("%s", library.books[library.num_books].title); printf("作者:"); scanf("%s", library.books[library.num_books].author); printf("页数:"); scanf("%d", &library.books[library.num_books].pages); printf("出版年份:"); scanf("%d", &library.books[library.num_books].year); library.num_books++; } } else if (choice == 2) { printf("请输入要查询的书名:"); char title[100]; scanf("%s", title); int found = 0; for (int i = 0; i < library.num_books; i++) { if (strcmp(library.books[i].title, title) == 0) { print_book(&library.books[i]); found = 1; break; }
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气滴点C

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值