欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)圖書管理程序

 更新時(shí)間:2022年03月15日 09:34:08   作者:sipprince  
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)圖書管理程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C++實(shí)現(xiàn)圖書管理程序的具體代碼,供大家參考,具體內(nèi)容如下

主文件

#include <iostream>
#include "library.h"
#include "info.h"http://讀取幫助文件?
?
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 圖 書 管 理 系 統(tǒng)\n\n\n";?
? ? ? ? cout <<"\t\t\t1: 借 書\n\n\t\t\t2: 還 書 \n\n\t\t\t3: 圖 書 維 護(hù)\n\n\t\t\t4: 讀 者 維 護(hù)\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(); //圖書維護(hù)?
? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? case '4':?
? ? ? ? ? ? ? ? ? ? ReaderDB.readerdata(); //讀者維護(hù)?
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case '5':
? ? ? ? ? ? ? ? ? ? read_info();?
? ? ? ? ? ? case '0':
? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? cout<<"輸入錯(cuò)誤,請重新輸入:";?
? ? ? ??
? ? ? ? }?
? ? }?
? ??
? ? return 0;?
}?

acquire_date.h

//獲取系統(tǒng)時(shí)間?
#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<<"輸入錯(cuò)誤!請重新輸入:";
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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

//主功能實(shí)現(xiàn)?
#include <iostream>?
#include <iomanip>?
#include <string>?
#include <fstream>//輸入/輸出文件流類 ?
#include "delete_space.h"
#include "acquire_date.h"http://獲取系統(tǒng)日期?
?
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*/
?
//讀者類,實(shí)現(xiàn)對讀者的信息的描述?
class Reader?
{?
private:
? ? ? ? bool tag; //刪除標(biāo)記 1:已刪 0:未刪?
? ? ? ? int num; //讀者編號?
? ? ? ? char name[rdNlen]; //讀者姓名?
? ? ? ? string borbook[Maxbor];//所借圖書
? ? ? ? Time bortime[Maxbor];//借書時(shí)間?
public:?
? ? ? ? Reader() {}?
? ? ? ? char *getname() {return name;} //獲取姓名?
? ? ? ? bool gettag() {return tag;} //獲取刪除標(biāo)記?
? ? ? ? int getnum() {return num;} //獲取讀者編號?
? ? ? ??
? ? ? ? void setname(char na[]) //設(shè)置姓名?
? ? ? ? {?
? ? ? ? ? strcpy(name,na);?
? ? ? ? }?
? ? ? ? void delbook(){ tag=1; }//設(shè)置刪除標(biāo)記 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()//檢查是否欠費(fèi)?
? ? ? ? {
? ? ? ? ? ? ?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<<"欠費(fèi)"<<endl;
? ? ? ? ? ? ?}
? ? ? ? ? ? ?cout<<"不欠費(fèi)"<<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//讀者類庫,管理建立讀者的個(gè)人資料?
{?
? ? private:?
? ? ? ? ? ? int top; //讀者數(shù)組最高下標(biāo)?
? ? ? ? ? ? Reader read[Maxr];//讀者記錄?
? ? public:?
? ? ? ? ? ? RDatabase() //將reader.txt讀到read[]中?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? Reader s;?
? ? ? ? ? ? ? ? top=-1;?
? ? ? ? ? ? ? ? fstream file("reader.txt",ios::in);//打開一個(gè)輸入文件?
? ? ? ? ? ? ? ? while (true)?
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? file.read((char *)&s,sizeof(s));?
? ? ? ? ? ? ? ? ? ? if (!file)break;?
? ? ? ? ? ? ? ? ? ? top++;?
? ? ? ? ? ? ? ? ? ? read[top]=s;?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? file.close(); //關(guān)閉 reader.txt?
? ? ? ? ? ? }?
? ? ? ? ? ??
? ? ? ? ? ? void clear()//刪除所有讀者信息?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? top=-1;?
? ? ? ? ? ? }?
? ? ? ? ? ??
? ? ? ? ? ? int addreader(int n,char *na)//添加讀者時(shí)先查找是否存在?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? 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();//讀者庫維護(hù)?
? ? ? ? ? ??
? ? ? ? ? ? ~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讀 者 維 護(hù)\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<<"輸入錯(cuò)誤,請重新輸入:";?
? ? ? ? }?
? ? }?
}?

?
//圖書類,實(shí)現(xiàn)對圖書的描述,圖書的編號,書名,借出,還入等?
class Book?
{?
private:?
? ? ? ? bool tag;//刪除標(biāo)記 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; }//獲取刪除標(biāo)記?
? ? ? ??
? ? ? ? void setname(char na[])//設(shè)置書名?
? ? ? ? {?
? ? ? ? ? ? strcpy(name,na);?
? ? ? ? }
? ? ? ? ?
? ? ? ? void setauthor(char au[])//設(shè)置作者名?
? ? ? ? {?
? ? ? ? ? ? strcpy(author,au);?
? ? ? ? }
? ? ? ??
? ? ? ? void setkind(Genre g)//設(shè)置圖書類型?
? ? ? ? {
? ? ? ? ? ? ?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;
}
?
//圖書庫類,實(shí)現(xiàn)對圖書的維護(hù),查找,刪除等?
class BDatabase?
{?
? ? private:?
? ? ? ? ? ? int top; //圖書數(shù)組最高下標(biāo)?
? ? ? ? ? ? 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();//圖書庫維護(hù)?
? ? ? ? ? ??
? ? ? ? ? ? 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圖 書 維 護(hù) \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<<"輸入錯(cuò)誤,請重新輸入:";?
? ? ? ? ? ? }?
? ? }?
}?

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論