下你所需,载你所想!
汇集开发技术源码资料

简易图书管理

:9.402KB :1 :2022-10-09 14:19:40

部分简介

简易图书管理如果开发者对于本文件有需要的可以参考。
#include "Book.h"
#include
#include
#include
#include
#include
#define CMD_COLS 80
#define CMD_LINES 25
using namespace std;
void setScreenGrid(); //设置屏幕显示的行数和列数
void clearScreen(); //清屏
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(); //主循环
int main()
{
mainloop();
}
void setScreenGrid() //设置屏幕显示的行数和列数
{
char sysSetBuf[80];
snprintf(sysSetBuf, sizeof(sysSetBuf),"mode con cols=%d lines=%d", CMD_COLS, CMD_LINES);
system(sysSetBuf);
}
void clearScreen() //清屏
{
system("cls");
}
void setSysCaption(const char* pText) //设置窗体标题栏
{
system("title sample");
}
void showWelcome() //显示欢迎信息
{
for (int i = 0; i < 9; i ) {
cout << endl;
}
cout << setw(66) << "--------------" << endl;
cout << setw(66) << "图书馆管理系统" << endl;
cout << setw(66) << "--------------" << endl;
}
void showRootMenu() //显示开始菜单
{
cout << setw(64) << "请选择功能" << endl << endl;
cout << setw(64) << "1 添加新书" << endl << endl;
cout << setw(64) << "2 浏览全部" << endl << endl;
cout << setw(64) << "3 删除图书" << endl;
}
void waitView(int iCurPage) //浏览数据时等待数据操作
{
char a;
cout << "m 显示上一页 n 显示下一页 q 返回主菜單" << endl;
cin >> a;
if (a == 'm') {
if (iCurPage == 1) viewData(iCurPage);
else viewData(iCurPage - 1);
}
else if (a == 'n') {
viewData(iCurPage);
}
else if (a == 'q') {
system("exit");
}
else system("exit");
}
void waitUser() //等待用户操作
{
int iInputPage = 0;
cout << "q返回主菜單" << endl;
char buf[256];
cin >> buf;
if (buf[0] == 'q') {
system("exit");
}
else system("exit");
}
void guideInput() //使用向导添加图书信息
{
char inName[NUM1];
char inIsbn[NUM1];
char inPrice[NUM2];
char inAuthor[NUM2];
cout << "输入书名: ";
cin >> inName;
cout << "输入ISBN: ";
cin >> inIsbn;
cout << "输入价格: ";
cin >> inPrice;
cout << "输入作者: ";
cin >> inAuthor;
CBook book(inName, inIsbn, inPrice, inAuthor);
book.writeData();
cout << "Write Finish!" << endl;
waitUser();
}
int getSelect() //获得用户菜单选择
{
char buf[256];
cin >> 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);
return respos;
}
void viewData(int iSelPage) //浏览所有图书记录
{
int iPage = 0;
int iCurPage = 0;
int iDataCount = 0;
char inName[NUM1];
char inIsbn[NUM1];
char inPrice[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 > 0) {
bIndex = true;
}
iPage = iDataCount / 20 1;
clearScreen();
cout << "共有图书" << " " << iDataCount << " ";
cout << "共有页数" << " " << iPage << " ";
cout << "当前页数" << " " << iCurPage << " " << endl;

cout << setw(5) << "Index";
cout << setw(22) << "Name";
cout << setw(22) << "Isbn";
cout << setw(20) << "Price";
cout << setw(25) << "Author" << 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(inPrice, 0, 30);
memset(inAuthor, 0, 30);
if (bIndex)
cout << setw(3) << (iCurPage - 1) * 20 i;
ifile.read(inName, NUM1);
cout << setw(26) << inName;
ifile.read(inIsbn, NUM1);
cout << setw(26) << inIsbn;
ifile.read(inPrice, NUM2);
cout << setw(14) << inPrice;
ifile.read(inAuthor, NUM2);
cout << setw(25) << inAuthor << 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 ;
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(1);
break;
case 3:
clearScreen();
deleteBookFromFile();
break;
}
}
}

热门推荐

相关文章