实例介绍
#include "Book.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <stdlib.h>
#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;
}
}
}
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <stdlib.h>
#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;
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论