各位好!帮我看一下这个图书管理系统吧!
我是一个C++新手: 以下是一个图书管理系统的部份源代码,请几个地方搞不明白: #include <iostream> #include <fstream> #include <stdio.h> using namespace std; struct BookRecord //图书记录 { char BookSerial[9]; //图书编号 char BookName[40]; //图书书名 char publishingHouseOfBook[50]; //图书出版社 bool enableUse; //是否可借 }; //输入图书记录 void Input(BookRecord& record) { cout << "请输入图书编号:" << endl; cin >> record.BookSerial; cout << "请输入图书名:" << endl; cin >> record.BookName; cout << "请输入图书出版社:" << endl; cin >> record.publishingHouseOfBook; cout << "请输入图书使用状态(1 或 0):" << endl; cin >> record.enableUse; } //增加图书记录 bool AddBookRecord() { fstream file("D:\\Book.dbf", ios::binary | ios::out | ios::app); if(!file) { cout << "打开文件Book.dbf失败" << endl; return false; } BookRecord record; //这两句是在干什么?? Input(record); file.write((const char*) &record, sizeof BookRecord); file.close(); return true; } //删除图书记录 bool DeleteBookRecord() //为什么是bool而不是void{ fstream file("D:\\Book.dbf", ios::in | ios::out | ios::binary); if(!file) { cout << "打开文件Book.dbf失败" << endl; return false; } BookRecord record; cout << "请输入要删除的图书编号:" << endl; cin >> record.BookSerial; fstream tempfile("D:\\temp.dbf", ios::out | ios::binary); if(!tempfile) { cout << "文件操作失败" << endl; return false; } BookRecord temprecord; while(file.read((char*)&temprecord, sizeof BookRecord)) //不明白这句是在做什么? { if(strcmp(temprecord.BookSerial, record.BookSerial)) { tempfile.write((const char*) &temprecord, sizeof BookRecord); //这句是在做什么??? } } file.close(); tempfile.close(); remove("D:\\Book.dbf"); rename("D:\\temp.dbf", "D:\\Book.dbf"); return true; } //编辑图书状态 bool EditBookRecord() { fstream file("D:\\Book.dbf", ios::in | ios::out | ios::binary); if(!file) { cout << "打开文件Book.dbf失败" << endl; return false; } BookRecord record; cout << "请输入要编辑、查询的图书编号:" << endl; cin >> record.BookSerial; BookRecord temprecord; while(file.read((char*)&temprecord, sizeof BookRecord)) { if(!strcmp(record.BookSerial, temprecord.BookSerial)) { cout << "图书编号:"; cout << temprecord.BookSerial << endl; cout << "图书名:"; cout << temprecord.BookName << endl; cout << "图书出版社:"; cout << temprecord.publishingHouseOfBook << endl; cout << "图书可借状态:"; cout << temprecord.enableUse << endl; temprecord.enableUse ? cout << "图书可借!" << endl : cout << "图书不可借!" <<endl; cout << "是否要对该记录进行编辑(y/n): " << endl; char ch; cin >> ch; if(ch == 'y' || ch == 'Y') { Input(temprecord); file.seekp((long)file.tellg() - (long) sizeof(temprecord), ios::beg); file.write((const char*) &temprecord, sizeof BookRecord); return true; } } } return false; } bool Exit() { exit(1); } struct MenuItem { int number; bool (*pf)(); }; int main() { MenuItem Menu[4] = {{1, AddBookRecord}, {2, DeleteBookRecord}, {3, EditBookRecord}, {4, Exit}}; cout << "图书管理系统1.00" << endl; cout << "1.增加图书记录" << endl; cout << "2.删除图书记录" << endl; cout << "3.编辑、查询图书状态" << endl; cout << "4.退出" << endl; cout << endl; cout << "请输入要进行的操作(1--4):" << endl; int flag; while(cin >> flag) { if(flag < 1 || flag > 4) { cout << "输入错误!" << endl; continue; } for(int i = 0; i < 4; i++) { if(Menu[i].number == flag) { if(Menu[i].pf()) { cout << "操作成功!" << endl; } else cout << "操作不成功" << endl; } } } return 0; } |