C++图书管理程序
sipprince 人气:0主文件
#include <iostream> #include "library.h" #include "info.h"//读取帮助文件 using namespace std; int main() { char choice='w'; string bookid; int readerid;//图书、读者编号 RDatabase ReaderDB; //读者库 Reader *r; BDatabase BookDB; //图书库 Book *b; int i=1; while(choice!='0') { cout <<"\n\n\t\t\t 图 书 管 理 系 统\n\n\n"; cout <<"\t\t\t1: 借 书\n\n\t\t\t2: 还 书 \n\n\t\t\t3: 图 书 维 护\n\n\t\t\t4: 读 者 维 护\n\n\t\t\t5: 帮 助\n\n\t\t\t0: 退 出"<<endl; cin >> choice; switch (choice) { case '1': cout <<" |借书|\n读者编号(1~"<<Maxr<<"):"; cin >>readerid; r=ReaderDB.query(readerid);//按编号查找 if (NULL==r) //查找是否有该读者 { cout <<"不存在该读者,不能借书"<<endl; break; } cout <<" 图书编号(n-n-n-x):"; bookid=input_ISBN(); b=BookDB.query(bookid); //查找是否有该书 if (b==NULL) { cout <<"不存在该图书,不能借书"<< endl; break; } if (b->borrowbook()==0)//存在该书的情况下查找该书是否已借出 { cout << " 该图书已借出,不能借书"<< endl; break; } r->borrowbook(b->getISBN());//成功借书 cout<<"借书成功!"<<endl; break; case '2': cout<<"|还书|\n 读者编号(1~"<<Maxr<<"):"; cin >>readerid; if (r==NULL) { cout <<"不存在该读者,不能还书" << endl; break; } cout <<"图书编号(n-n-n-x):"; bookid=input_ISBN(); r=ReaderDB.query(readerid); b=BookDB.query(bookid); if (b==NULL) { cout <<"不存在该图书,不能还书" <<endl; break; } b->retbook(); r->retbook(b->getISBN()); //还书成功 break; case '3': BookDB.bookdata(); //图书维护 break; case '4': ReaderDB.readerdata(); //读者维护 break; case '5': read_info(); case '0': break; default: cout<<"输入错误,请重新输入:"; } } return 0; }
acquire_date.h
//获取系统时间 #include <iostream> #include <ctime> /* time.h in C */ using namespace std; struct Time { int y; int m; int d; }; Time acquire_time() { time_t now; struct tm *fmt; Time t; time(&now); fmt = localtime(&now); t.y=fmt->tm_year%100;//获取年月日 t.m=fmt->tm_mon+1; t.d=fmt->tm_mday; cout<<t.y<<"年"<<t.m<<"月"<<t.d<<"日"<<endl; return t; }
delete_space.h
//去空格 #include <iostream> #include <string> using namespace std; string input_ISBN() { bool ifGo=1; char n='0'; string names="0"; cout<<"(输入ISBN)"<<endl; while(ifGo==1){ int i=0; int f=0; while(cin>>n) { if(n>='0'&&n<='9'&&i<3){ names+=n; f++;} else if(n=='-'&&i<3&&f!=0){ names+=n; i++; } else if(i==3&&((n>='a'&&n<='z')||(n>='A'&&n<='Z')||(n>='0'&&n<='9'))) { names+=n; cin.clear(); cin.sync(); ifGo=0; break; } else{ cout<<"输入错误!请重新输入:"; names="0"; ifGo=1; cin.clear(); cin.sync(); i=0; f=0; } } } string names1; for(int j=1;j<names.length();j++) names1+=names[j]; return names1; }
info.h
//读取帮助文件 #include <fstream> #include <iostream> using namespace std; const int l=100;//每一行的长度 void read_info() { ifstream ios("help.txt"); char s[l];//用于读取文件中的一行 for(int j=0;j<l;j++) s[j]='\t'; while( ios.getline(s,l)) { for(int i=0;i<l;i++) { if(s[i]!='\t') cout<<s[i]; } cout<<endl; for(int j=0;j<l;j++) s[j]='\t'; } }
library.h
//主功能实现 #include <iostream> #include <iomanip> #include <string> #include <fstream>//输入/输出文件流类 #include "delete_space.h" #include "acquire_date.h"//获取系统日期 using namespace std; const int Maxr=100;//最多的读者 const int Maxb=100;//最多的图书 const int Maxbor=8;//每位读者最多借八本书 const int bkNlen=20;//图书名字长度 const int rdNlen=20;//读者及作者名字长度 const int Maxday=60;//读书借阅期(以日算) enum Genre{fic,nfic,per,bio,chi,unk};/*fic-fiction,nfic-nonfiction,per-periodical, bio-biography,chi-children,unk-unknown*/ //读者类,实现对读者的信息的描述 class Reader { private: bool tag; //删除标记 1:已删 0:未删 int num; //读者编号 char name[rdNlen]; //读者姓名 string borbook[Maxbor];//所借图书 Time bortime[Maxbor];//借书时间 public: Reader() {} char *getname() {return name;} //获取姓名 bool gettag() {return tag;} //获取删除标记 int getnum() {return num;} //获取读者编号 void setname(char na[]) //设置姓名 { strcpy(name,na); } void delbook(){ tag=1; }//设置删除标记 1:已删 0:未删 void addreader(int n,char *na)//增加读者 { tag=0; num=n; strcpy(name,na); for(int i=0;i<Maxbor;i++) borbook[i]="0"; } void borrowbook(string bookid)//借书操作 { for(int i=0;i<Maxbor;i++) { if (borbook[i]=="0") { borbook[i]=bookid; bortime[i]=acquire_time(); return; } } } int retbook(string bookid)//还书操作 { for(int i=0;i<Maxbor;i++) { if(borbook[i]==bookid) { borbook[i]="0"; bortime[i].y=0; bortime[i].m=0; bortime[i].d=0; return 1; } } return 0; } void check()//检查是否欠费 { Time t=acquire_time(); for(int i=0;i<Maxbor;i++) { if((bortime[i].y!=0&&bortime[i].m!=0&&bortime[i].d!=0) &&((t.y-bortime[i].y)*365+(t.m-bortime[i].m)*30+(t.d-bortime[i].d))>Maxday) cout<<"欠费"<<endl; } cout<<"不欠费"<<endl; } void disp()//读出读者信息 { check(); cout << setw(10) << num <<setw(20) << name<<"借书编号:["; for(int i=0;i<Maxbor;i++) if(borbook[i]!="0") cout << borbook[i] << "|"; cout << "]"<<endl; } }; class RDatabase//读者类库,管理建立读者的个人资料 { private: int top; //读者数组最高下标 Reader read[Maxr];//读者记录 public: RDatabase() //将reader.txt读到read[]中 { Reader s; top=-1; fstream file("reader.txt",ios::in);//打开一个输入文件 while (true) { file.read((char *)&s,sizeof(s)); if (!file)break; top++; read[top]=s; } file.close(); //关闭 reader.txt } void clear()//删除所有读者信息 { top=-1; } int addreader(int n,char *na)//添加读者时先查找是否存在 { Reader *p=query(n); if (p==NULL) { top++; read[top].addreader(n,na); return 1; } return 0; } Reader *query(int readerid)//按编号查找 { for (int i=0;i<=top;i++) if (read[i].getnum()==readerid && read[i].gettag()==0) { return &read[i]; } return NULL; } void disp() //输出所有读者信息 { for (int i=0;i<=top;i++) read[i].disp(); } void readerdata();//读者库维护 ~RDatabase() //将read[]写到reader.txt文件中 { fstream file("reader.txt",ios::out); for (int i=0;i<=top;i++) if (read[i].gettag()==0) file.write((char *)&read[i],sizeof(read[i])); file.close(); } }; void RDatabase::readerdata() { char choice='7'; char rname[rdNlen]; int readerid; Reader *r; while (choice!='0') { cout <<"\n\n\t\t\t读 者 维 护\n\n\n\t\t 1: 新 增\n\n\t\t 2: 更改\n\n\t\t 3: 删 除\n\n\t\t 4: 查 找\n\n\t\t 5: 显 示\n\n\t\t 6: 全 删\n\n\t\t 0: 退出"<<endl; cin >> choice; switch (choice) { case '1': cout << "输入读者编号(1~"<<Maxr<<"):"; cin >> readerid; cout << "输入读者姓名:"; cin >> rname; addreader (readerid,rname); break; case '2': cout << "输入读者编号(1~"<<Maxr<<"):"; cin >> readerid; r=query(readerid); if (r==NULL) { cout << " 该读者不存在 "<<endl; break; } cout << "输入新的姓名:"; cin >> rname; r->setname(rname); break; case '3': cout << " 输入读者编号(1~"<<Maxr<<"):"; cin >> readerid; r=query(readerid); if (r==NULL) { cout <<" 该读者不存在" << endl; break; } r->delbook(); break; case '4': cout << "输入读者编号(1~"<<Maxr<<"):"; cin >> readerid; r=query(readerid); if (r==NULL) { cout <<"该读者不存在"<< endl; break; } r->disp(); break; case '5': disp(); break; case '6': clear(); break; default: cout<<"输入错误,请重新输入:"; } } } //图书类,实现对图书的描述,图书的编号,书名,借出,还入等 class Book { private: bool tag;//删除标记 1:已删 0:未删 string ISBN;//图书编号 char name[bkNlen];//书名 char author[rdNlen];//作者名 Genre kind;//图书类型 bool onshelf;//是否在架 1:在架 0:已借 public: Book(){} char *getname() { return name; }//获取书名 char *getauthor(){return author;}//获取作者 string getkind(); //获取图书类型 string getISBN(){ return ISBN; }//获取图书ISBN bool gettag(){ return tag; }//获取删除标记 void setname(char na[])//设置书名 { strcpy(name,na); } void setauthor(char au[])//设置作者名 { strcpy(author,au); } void setkind(Genre g)//设置图书类型 { kind=g; } void delbook(){ tag=1;}//删除图书 void addbook(string s1,char *na,char *au,Genre g)//增加图书 { tag=0; ISBN=s1; strcpy(name,na); strcpy(author,au); kind=g; onshelf=1; } bool borrowbook()//借书操作 (并查找是否在架) { if (onshelf==1) { onshelf=0; return 1; } return 0; } void retbook()//还书操作 { onshelf=1; } void disp()//输出图书 { cout <<setw(10)<<ISBN <<setw(10)<<"《"<<name<<"》" <<setw(10)<<"("<<author<<")" <<setw(15)<<getkind() <<setw(10)<<(onshelf==1? "在架":"已借") <<endl; } }; string Book::getkind()//返回图书类型 { switch(kind) { case 0: return"fiction"; break; case 1: return "nonfiction"; break; case 2: return "periodical"; case 3: return "biography"; case 4: return "children"; default: return "unknow"; } } bool operator==(Book &b1,Book&b2)//判断两本书的ISBN号是否相等 1-相同 0-不同 { if(b1.getISBN()==b2.getISBN()) return 1; return 0; } bool operator!=(Book &b1,Book&b2)//判断两本书的ISBN号是否不等 1-不同 0-相同 { if(b1.getISBN()!=b2.getISBN()) return 1; return 0; } //图书库类,实现对图书的维护,查找,删除等 class BDatabase { private: int top; //图书数组最高下标 Book book[Maxb]; //图书记录 public: BDatabase()//将book.txt读到book[]中 { Book b; top=-1; fstream file("book.txt",ios::in); while (true) { file.read((char *)&b,sizeof(b)); if (!file) break; top++; book[top]=b; } file.close(); } void clear()//全删 { top=-1; } int addbook(string s1,char *na,char *au,Genre g)//增加图书 { Book *p=query(s1); if (p==NULL) { top++; book[top].addbook(s1,na,au,g); return 1; } else{ cout<<"已存在该ISBN号!请检查输入!"<<endl; return 0; } } Book *query(string bookid)//查找图书 { for (int i=0;i<=top;i++) if (book[i].getISBN()==bookid &&book[i].gettag()==0)//找到并且未删 { return &book[i]; } return NULL; } void bookdata();//图书库维护 void disp() { for (int i=0;i<=top;i++) if (book[i].gettag()==0) book[i].disp(); } ~BDatabase()//将book[]写到book.txt文件中 { fstream file("book.txt",ios::out); for (int i=0;i<top;i++) if (book[i].gettag()==0) file.write((char *)&book[i],sizeof(book[i])); file.close(); } }; void BDatabase::bookdata() { char choice='w'; char bname[bkNlen]; char aname[rdNlen]; int bkind; Genre bkind1; string bookid; Book *b; while (choice!='0') { cout <<"\n\n\n\t\t\t图 书 维 护 \n"<<endl; cout<<"\t\t1: 新 增\n \t\t2: 更 改\n\t\t3: 删 除\n\t\t4: 查 找\n\t\t5: 显示\n\t\t6: 全 删\n\t\t0: 退 出"<<endl; cin >> choice; switch (choice) { case '1': cout <<"输入图书编号(n-n-n-x):"<<endl; bookid=input_ISBN(); cout <<"输入图书书名:"<<endl; cin >>bname; cout<<"输入作者姓名:"<<endl; cin>>aname; cout<<"输入图书类型:\n1-fiction,2-nonfiction,3-periodical,4-biography,5-children"<<endl; cin>>bkind; if(bkind<6&&bkind>0) bkind1=Genre(bkind-1); else bkind1=Genre(5); addbook(bookid,bname,aname,bkind1); break; case '2': cout << "输入图书编号(n-n-n-x):"<<endl; cin >> bookid; b=query(bookid); if (b==NULL) { cout << " 该图书不存在 "<<endl; break; } cout <<"输入新的书名:"<<endl; cin >>bname; b->setname(bname); cout <<"输入新的作者名:"<<endl; cin >>aname; b->setauthor(aname); cout <<"输入新的类型:\n1-fiction,2-nonfiction,3-periodical,4-biography,5-children"<<endl; cin >>bkind; if(bkind<6&&bkind>0) bkind1=Genre(bkind-1); b->setkind(bkind1); break; case '3': cout <<"输入图书编号(n-n-n-x):"<<endl; cin >> bookid; b=query(bookid); if (b==NULL) { cout <<"该图书不存在" << endl; break; } b->delbook(); break; case '4': cout <<"输入图书编号(n-n-n-x):"<<endl; cin >> bookid; b=query(bookid); if (b==NULL) { cout <<"该图书不存在"<< endl; break; } b->disp(); break; case '5': disp(); break; case '6': clear(); break; case '0': break; default: cout<<"输入错误,请重新输入:"; } } }
加载全部内容