C++實(shí)現(xiàn)圖書(shū)管理程序
本文實(shí)例為大家分享了C++實(shí)現(xiàn)圖書(shū)管理程序的具體代碼,供大家參考,具體內(nèi)容如下
主文件
#include <iostream>
#include "library.h"
#include "info.h"http://讀取幫助文件?
?
using namespace std;
?
int main()?
{
? ? char choice='w';?
? ? string bookid;
? ? int readerid;//圖書(shū)、讀者編號(hào)?
? ? RDatabase ReaderDB; //讀者庫(kù)?
? ? Reader *r;?
? ? BDatabase BookDB; //圖書(shū)庫(kù)?
? ? Book *b;?
? ? int i=1;
? ??
? ? while(choice!='0')?
? ? {?
? ? ? ? cout <<"\n\n\t\t\t 圖 書(shū) 管 理 系 統(tǒng)\n\n\n";?
? ? ? ? cout <<"\t\t\t1: 借 書(shū)\n\n\t\t\t2: 還 書(shū) \n\n\t\t\t3: 圖 書(shū) 維 護(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 <<" |借書(shū)|\n讀者編號(hào)(1~"<<Maxr<<"):"; ?
? ? ? ? ? ? ? ? ? ? cin >>readerid;
? ? ? ? ? ? ? ? ? ? r=ReaderDB.query(readerid);//按編號(hào)查找?
? ? ? ? ? ? ? ? ? ? if (NULL==r) //查找是否有該讀者?
? ? ? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? ? ? cout <<"不存在該讀者,不能借書(shū)"<<endl;?
? ? ? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? cout <<" 圖書(shū)編號(hào)(n-n-n-x):";?
? ? ? ? ? ? ? ? ? ? bookid=input_ISBN(); ?
? ? ? ? ? ? ? ? ? ? b=BookDB.query(bookid); //查找是否有該書(shū)?
? ? ? ? ? ? ? ? ? ? if (b==NULL)?
? ? ? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? ? ? cout <<"不存在該圖書(shū),不能借書(shū)"<< endl;?
? ? ? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? if (b->borrowbook()==0)//存在該書(shū)的情況下查找該書(shū)是否已借出?
? ? ? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? ? ? cout << " 該圖書(shū)已借出,不能借書(shū)"<< endl;?
? ? ? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? ? ? r->borrowbook(b->getISBN());//成功借書(shū)
? ? ? ? ? ? ? ? ? ? cout<<"借書(shū)成功!"<<endl;?
? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? case '2':?
? ? ? ? ? ? ? ? ? ? cout<<"|還書(shū)|\n 讀者編號(hào)(1~"<<Maxr<<"):";?
? ? ? ? ? ? ? ? ? ? cin >>readerid;
? ? ? ? ? ? ? ? ? ? if (r==NULL)?
? ? ? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? ? ? cout <<"不存在該讀者,不能還書(shū)" << endl;?
? ? ? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? cout <<"圖書(shū)編號(hào)(n-n-n-x):";?
? ? ? ? ? ? ? ? ? ? bookid=input_ISBN();
? ? ? ? ? ? ? ? ? ? r=ReaderDB.query(readerid);?
? ? ? ? ? ? ? ? ? ? b=BookDB.query(bookid);?
? ? ? ? ? ? ? ? ? ? if (b==NULL)?
? ? ? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? ? ? cout <<"不存在該圖書(shū),不能還書(shū)" <<endl;?
? ? ? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? b->retbook(); ?
? ? ? ? ? ? ? ? ? ? r->retbook(b->getISBN()); //還書(shū)成功?
? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? case '3':?
? ? ? ? ? ? ? ? ? ? BookDB.bookdata(); //圖書(shū)維護(hù)?
? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? case '4':?
? ? ? ? ? ? ? ? ? ? ReaderDB.readerdata(); //讀者維護(hù)?
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case '5':
? ? ? ? ? ? ? ? ? ? read_info();?
? ? ? ? ? ? case '0':
? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? cout<<"輸入錯(cuò)誤,請(qǐng)重新輸入:";?
? ? ? ??
? ? ? ? }?
? ? }?
? ??
? ? 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ò)誤!請(qǐng)重新輸入:";
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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;//每一行的長(zhǎng)度
?
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>//輸入/輸出文件流類(lèi) ?
#include "delete_space.h"
#include "acquire_date.h"http://獲取系統(tǒng)日期?
?
using namespace std;
?
const int Maxr=100;//最多的讀者?
const int Maxb=100;//最多的圖書(shū)?
const int Maxbor=8;//每位讀者最多借八本書(shū)?
const int bkNlen=20;//圖書(shū)名字長(zhǎng)度
const int rdNlen=20;//讀者及作者名字長(zhǎng)度?
const int Maxday=60;//讀書(shū)借閱期(以日算)?
enum Genre{fic,nfic,per,bio,chi,unk};/*fic-fiction,nfic-nonfiction,per-periodical,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?bio-biography,chi-children,unk-unknown*/
?
//讀者類(lèi),實(shí)現(xiàn)對(duì)讀者的信息的描述?
class Reader?
{?
private:
? ? ? ? bool tag; //刪除標(biāo)記 1:已刪 0:未刪?
? ? ? ? int num; //讀者編號(hào)?
? ? ? ? char name[rdNlen]; //讀者姓名?
? ? ? ? string borbook[Maxbor];//所借圖書(shū)
? ? ? ? Time bortime[Maxbor];//借書(shū)時(shí)間?
public:?
? ? ? ? Reader() {}?
? ? ? ? char *getname() {return name;} //獲取姓名?
? ? ? ? bool gettag() {return tag;} //獲取刪除標(biāo)記?
? ? ? ? int getnum() {return num;} //獲取讀者編號(hào)?
? ? ? ??
? ? ? ? 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)//借書(shū)操作?
? ? ? ? {?
? ? ? ? ? ? for(int i=0;i<Maxbor;i++)?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? if (borbook[i]=="0")?
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? borbook[i]=bookid;
? ? ? ? ? ? ? ? ? ? bortime[i]=acquire_time();?
? ? ? ? ? ? ? ? ? ? return;?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? }?
? ? ? ? }?
?
? ? ? ? int retbook(string bookid)//還書(shū)操作?
? ? ? ? {?
? ? ? ? ? ? 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<<"借書(shū)編號(hào):[";?
? ? ? ? ? ? for(int i=0;i<Maxbor;i++)?
? ? ? ? ? ? ? ? if(borbook[i]!="0")?
? ? ? ? ? ? ? ? cout << borbook[i] << "|";?
? ? ? ? ? ? cout << "]"<<endl;?
? ? ? ? }?
? ? ? ? ? ? ? ? ? ??
};?
?
?
class RDatabase//讀者類(lèi)庫(kù),管理建立讀者的個(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);//打開(kāi)一個(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)//按編號(hào)查找?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? 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();//讀者庫(kù)維護(hù)?
? ? ? ? ? ??
? ? ? ? ? ? ~RDatabase() //將read[]寫(xiě)到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 << "輸入讀者編號(hào)(1~"<<Maxr<<"):";?
? ? ? ? ? ? ? ? ? ? cin >> readerid;?
? ? ? ? ? ? ? ? ? ? cout << "輸入讀者姓名:";?
? ? ? ? ? ? ? ? ? ? cin >> rname;?
? ? ? ? ? ? ? ? ? ? addreader (readerid,rname);?
? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? case '2':?
? ? ? ? ? ? ? ? ? ? cout << "輸入讀者編號(hào)(1~"<<Maxr<<"):"; ?
? ? ? ? ? ? ? ? ? ? cin >> readerid;?
? ? ? ? ? ? ? ? ? ? r=query(readerid);?
? ? ? ? ? ? ? ? ? ? if (r==NULL)?
? ? ? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? ? ? cout << " 該讀者不存在 "<<endl;?
? ? ? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? ? ? cout << "輸入新的姓名:";?
? ? ? ? ? ? ? ? ? ? cin >> rname;?
? ? ? ? ? ? ? ? ? ? r->setname(rname);?
? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? case '3':?
? ? ? ? ? ? ? ? ? ? cout << " 輸入讀者編號(hào)(1~"<<Maxr<<"):";?
? ? ? ? ? ? ? ? ? ? cin >> readerid;?
? ? ? ? ? ? ? ? ? ? r=query(readerid);?
? ? ? ? ? ? ? ? ? ? if (r==NULL)?
? ? ? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? ? ? cout <<" 該讀者不存在" << endl;?
? ? ? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? ? ? r->delbook();?
? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? case '4':?
? ? ? ? ? ? ? ? ? ? cout << "輸入讀者編號(hào)(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ò)誤,請(qǐng)重新輸入:";?
? ? ? ? }?
? ? }?
}?
?
//圖書(shū)類(lèi),實(shí)現(xiàn)對(duì)圖書(shū)的描述,圖書(shū)的編號(hào),書(shū)名,借出,還入等?
class Book?
{?
private:?
? ? ? ? bool tag;//刪除標(biāo)記 1:已刪 0:未刪?
? ? ? ? string ISBN;//圖書(shū)編號(hào)?
? ? ? ? char name[bkNlen];//書(shū)名?
? ? ? ? char author[rdNlen];//作者名?
? ? ? ? Genre kind;//圖書(shū)類(lèi)型?
? ? ? ? bool onshelf;//是否在架 1:在架 0:已借?
public:?
? ? ? ? Book(){}?
? ? ? ? char *getname() { return name; }//獲取書(shū)名
? ? ? ? char *getauthor(){return author;}//獲取作者
? ? ? ? string getkind(); //獲取圖書(shū)類(lèi)型?
? ? ? ? string getISBN(){ return ISBN; }//獲取圖書(shū)ISBN?
? ? ? ? bool gettag(){ return tag; }//獲取刪除標(biāo)記?
? ? ? ??
? ? ? ? void setname(char na[])//設(shè)置書(shū)名?
? ? ? ? {?
? ? ? ? ? ? strcpy(name,na);?
? ? ? ? }
? ? ? ? ?
? ? ? ? void setauthor(char au[])//設(shè)置作者名?
? ? ? ? {?
? ? ? ? ? ? strcpy(author,au);?
? ? ? ? }
? ? ? ??
? ? ? ? void setkind(Genre g)//設(shè)置圖書(shū)類(lèi)型?
? ? ? ? {
? ? ? ? ? ? ?kind=g;
? ? ? ? }
? ? ? ??
? ? ? ? void delbook(){ tag=1;}//刪除圖書(shū)?
? ? ? ??
? ? ? ? void addbook(string s1,char *na,char *au,Genre g)//增加圖書(shū)?
? ? ? ? {?
? ? ? ? ? ? tag=0;?
? ? ? ? ? ? ISBN=s1;?
? ? ? ? ? ? strcpy(name,na);?
? ? ? ? ? ? strcpy(author,au);?
? ? ? ? ? ? kind=g;
? ? ? ? ? ? onshelf=1;?
? ? ? ? }?
? ? ? ??
? ? ? ? bool borrowbook()//借書(shū)操作 (并查找是否在架)?
? ? ? ? {?
? ? ? ? ? ? if (onshelf==1)?
? ? ? ? ? ? {?
? ? ? ? ? ? onshelf=0;?
? ? ? ? ? ? return 1;?
? ? ? ? ? ? }?
? ? ? ? ? ? return 0;?
? ? ? ? }
? ? ? ? ?
? ? ? ? void retbook()//還書(shū)操作?
? ? ? ? {?
? ? ? ? ? ? onshelf=1;?
? ? ? ? }?
? ? ? ? void disp()//輸出圖書(shū)?
? ? ? ? {?
? ? ? ? ? ? cout <<setw(10)<<ISBN
? ? ? ? ? ? ? ? ?<<setw(10)<<"《"<<name<<"》"
? ? ? ? ? ? ? ? ?<<setw(10)<<"("<<author<<")"
? ? ? ? ? ? ? ? ?<<setw(15)<<getkind()?
? ? ? ? ? ? ? ? ?<<setw(10)<<(onshelf==1? "在架":"已借") <<endl;?
? ? ? ? }
};?
?
string Book::getkind()//返回圖書(shū)類(lèi)型?
{
? ? ? ?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)//判斷兩本書(shū)的ISBN號(hào)是否相等 ?1-相同 0-不同?
{
? ? ?if(b1.getISBN()==b2.getISBN())
? ? ? ? return 1;
? ? ?return 0;
}
bool operator!=(Book &b1,Book&b2)//判斷兩本書(shū)的ISBN號(hào)是否不等 ?1-不同 0-相同?
{
? ? ?if(b1.getISBN()!=b2.getISBN())
? ? ? ? return 1;
? ? ?return 0;
}
?
//圖書(shū)庫(kù)類(lèi),實(shí)現(xiàn)對(duì)圖書(shū)的維護(hù),查找,刪除等?
class BDatabase?
{?
? ? private:?
? ? ? ? ? ? int top; //圖書(shū)數(shù)組最高下標(biāo)?
? ? ? ? ? ? Book book[Maxb]; //圖書(shū)記錄?
? ? 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)//增加圖書(shū)?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? Book *p=query(s1);?
? ? ? ? ? ? ? ? if (p==NULL)?
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? top++;?
? ? ? ? ? ? ? ? ? ? book[top].addbook(s1,na,au,g);?
? ? ? ? ? ? ? ? ? ? return 1;?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ?else{
? ? ? ? ? ? ? ? ? ?cout<<"已存在該ISBN號(hào)!請(qǐng)檢查輸入!"<<endl;
? ? ? ? ? ? ? ? ? ?return 0;
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? }?
? ? ? ? ? ??
? ? ? ? ? ? Book *query(string bookid)//查找圖書(shū)?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? for (int i=0;i<=top;i++)?
? ? ? ? ? ? ? ? if (book[i].getISBN()==bookid &&book[i].gettag()==0)//找到并且未刪?
? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? return &book[i];?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? return NULL;?
? ? ? ? ? ? }?
? ? ? ? ? ??
? ? ? ? ? ? void bookdata();//圖書(shū)庫(kù)維護(hù)?
? ? ? ? ? ??
? ? ? ? ? ? void disp()?
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? for (int i=0;i<=top;i++)?
? ? ? ? ? ? ? ? if (book[i].gettag()==0)?
? ? ? ? ? ? ? ? book[i].disp();?
? ? ? ? ? ? }?
? ? ? ? ? ??
? ? ? ? ? ? ~BDatabase()//將book[]寫(xiě)到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圖 書(shū) 維 護(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 <<"輸入圖書(shū)編號(hào)(n-n-n-x):"<<endl;?
? ? ? ? ? ? ? ? ? ? bookid=input_ISBN();
? ? ? ? ? ? ? ? ? ? cout <<"輸入圖書(shū)書(shū)名:"<<endl;?
? ? ? ? ? ? ? ? ? ? cin >>bname;
? ? ? ? ? ? ? ? ? ? cout<<"輸入作者姓名:"<<endl;
? ? ? ? ? ? ? ? ? ? cin>>aname;
? ? ? ? ? ? ? ? ? ? cout<<"輸入圖書(shū)類(lèi)型:\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 << "輸入圖書(shū)編號(hào)(n-n-n-x):"<<endl;?
? ? ? ? ? ? ? ? ? ? cin >> bookid;?
? ? ? ? ? ? ? ? ? ? b=query(bookid);?
? ? ? ? ? ? ? ? ? ? if (b==NULL)?
? ? ? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? ? ? cout << " 該圖書(shū)不存在 "<<endl;?
? ? ? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? ? ? cout <<"輸入新的書(shū)名:"<<endl;?
? ? ? ? ? ? ? ? ? ? cin >>bname;?
? ? ? ? ? ? ? ? ? ? b->setname(bname);
? ? ? ? ? ? ? ? ? ? cout <<"輸入新的作者名:"<<endl;?
? ? ? ? ? ? ? ? ? ? cin >>aname;?
? ? ? ? ? ? ? ? ? ? b->setauthor(aname);
? ? ? ? ? ? ? ? ? ? cout <<"輸入新的類(lèi)型:\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 <<"輸入圖書(shū)編號(hào)(n-n-n-x):"<<endl;?
? ? ? ? ? ? ? ? ? ? cin >> bookid;?
? ? ? ? ? ? ? ? ? ? b=query(bookid);?
? ? ? ? ? ? ? ? ? ? if (b==NULL)?
? ? ? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? ? ? cout <<"該圖書(shū)不存在" << endl;?
? ? ? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? ? ? b->delbook();?
? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? case '4':?
? ? ? ? ? ? ? ? ? ? cout <<"輸入圖書(shū)編號(hào)(n-n-n-x):"<<endl;?
? ? ? ? ? ? ? ? ? ? cin >> bookid;?
? ? ? ? ? ? ? ? ? ? b=query(bookid);?
? ? ? ? ? ? ? ? ? ? if (b==NULL)?
? ? ? ? ? ? ? ? ? ? {?
? ? ? ? ? ? ? ? ? ? ? ? cout <<"該圖書(shū)不存在"<< endl;?
? ? ? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? ? ? b->disp();?
? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? case '5':?
? ? ? ? ? ? ? ? ? ? disp();?
? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? case '6':?
? ? ? ? ? ? ? ? ? ? clear();?
? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? case '0':
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? cout<<"輸入錯(cuò)誤,請(qǐng)重新輸入:";?
? ? ? ? ? ? }?
? ? }?
}?



以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)源碼
- C++編寫(xiě)實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)簡(jiǎn)易版
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)課程設(shè)計(jì)(面向?qū)ο?
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)課程設(shè)計(jì)
- C++使用鏈表實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)(文件操作與類(lèi))
- C++圖書(shū)管理系統(tǒng)程序源代碼
- C++詳細(xì)實(shí)現(xiàn)完整圖書(shū)管理功能
相關(guān)文章
C++ 實(shí)現(xiàn)自定義類(lèi)型的迭代器操作
這篇文章主要介紹了C++ 實(shí)現(xiàn)自定義類(lèi)型的迭代器操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
VS2019使用Windows桌面應(yīng)用程序模塊創(chuàng)建Win32窗口
這篇文章主要介紹了VS2019使用Windows桌面應(yīng)用程序模塊創(chuàng)建Win32窗口,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
C語(yǔ)言菜鳥(niǎo)基礎(chǔ)教程之自定義函數(shù)
自定義函數(shù): 必須直接或間接在main中調(diào)用,否則該自定義函數(shù)不會(huì)被執(zhí)行。 返回值類(lèi)型 函數(shù)名(參數(shù)類(lèi)型 參數(shù)名,參數(shù)類(lèi)型 參數(shù)名...)2017-10-10
Visual Studio Code 從簡(jiǎn)介、安裝到配置所需插件詳細(xì)介紹
這篇文章給大家介紹到vs與vs code的區(qū)別,并且會(huì)詳細(xì)介紹vscode的安裝步驟,和我所了解過(guò)的插件配置,感興趣的朋友跟隨小編一起看看吧2020-03-03
Visual Studio 2019安裝使用C語(yǔ)言程序(VS2019 C語(yǔ)言)
這篇文章主要介紹了Visual Studio 2019安裝使用C語(yǔ)言程序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
C/C++ break和continue區(qū)別及使用方法
這篇文章主要介紹了C/C++ break和continue區(qū)別及使用方法的相關(guān)資料,需要的朋友可以參考下2017-07-07
C++的sstream標(biāo)準(zhǔn)庫(kù)詳細(xì)介紹
以下是對(duì)C++中的的sstream標(biāo)準(zhǔn)庫(kù)進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下2013-09-09

