圖書管理系統C++程式碼
阿新 • • 發佈:2018-12-18
#include<iostream> #include<iomanip> #include<windows.h> #include<conio.h> #include<string> using namespace std; const int Maxbor=10; //每位讀者最多借10本書 class Reader //讀者類 { int tag; int num; char mima[6]; char name[10]; int borbook[Maxbor]; public: Reader(); int getnum(); int gettag(); char* getname(); char* getmima(); void setname(char na[]); void delreader(); void addreader(int n,char *na,char* mi); void borrowbook(int bookid); int retbook(int bookid); void disp(); }; Reader::reader(){} char*Reader::getname() { return name; } int Reader::gettag() { return tag; } //獲取刪除標記 int Reader::getnum() { return num; } //獲取讀者編號 char * Reader::getmima(){ return mima;}//獲取讀者的密碼 void Reader::setname(char na[]) //設定姓名 { strcpy(name,na); } void Reader::delreader() { tag=1; } //刪除讀者 void Reader::addreader(int n,char *na,char* mi) //增加讀者 { tag=0; num=n; strcpy(name,na); strcpy(mima,mi); for (int i=0;i<Maxbor;i++) borbook[i]=0; } void Reader::borrowbook(int bookid) //借書操作 { for (int i=0;i<Maxbor;i++) { if (borbook[i]==0) { borbook[i]=bookid; return; } } } int Reader::retbook(int bookid)//還書操作 { for (int i=0;i<Maxbor;i++) { if (borbook[i]==bookid) { borbook[i]=0; return 1; } } return 0; } void Reader::disp()//輸出讀者資訊 { cout<<endl; cout << setw(12) << num<< " 借書編號:["; for (int i=0;i<Maxbor;i++) if (borbook[i]!=0) cout << borbook[i] << ";"; cout << "]" << endl; } class Book //圖書類 { int tag; int num; char name[10]; char writer[10]; char concem[30]; char Time[10]; float price; int onshelf; public: Book(); char *getname(); int getnum(); int getonshelf(); char *getwriter(); char *gettime(); char *getconcem(); float getprice(); int gettag(); void getTime(char time[]); void setname(char na[]) ; void delbook(); void addbook(int n,char *na, char *wri, char *con, char* tm,float pr); //增加圖書 int borrowbook(); //借書操作 void retbook(); //還書操作 void disp(); //輸出圖書 };Book::Book() {} char* Book::getname() { return name; } //獲取姓名 int Book::getonshelf(){return onshelf;} int Book::getnum() { return num; }//獲取圖書編號 char*Book::getwriter() { return writer; }//獲取書籍作者 char*Book::getconcem() { return concem; }//獲取書籍出版社 char* Book::gettime() {return Time;} float Book::getprice() { return price; }//獲取書籍價格 int Book::gettag() { return tag; } //獲取刪除標記 void Book::setname(char na[]) //設定書名 { strcpy(name,na); } void Book::getTime(char time[]) { strcpy(Time,time); } void Book::delbook() { tag=1; } //刪除圖書 void Book::addbook(int n,char *na, char *wri, char *con, char* tm,float pr) //增加圖書 { tag=0; num=n; strcpy(name,na); strcpy(writer,wri); strcpy(concem,con); strcpy(Time,tm); price=pr; onshelf=1; } int Book::borrowbook() //借書操作 { if (onshelf==1) { onshelf=0; return 1; } return 0; } void Book::retbook() //還書操作 { onshelf=1; } void Book::disp() //輸出圖書 { cout << setw(6) << num << setw(12) << name << setw(15)<<concem<<setw(10)<<writer<<setw(12)<<Time<<setw(6)<< (onshelf==1? "在架":"已借") << endl; } class BDatabase //圖書庫類 { int top; Book book[Maxb]; public: BDatabase(); void clear(); int addbook(int n,char *na, char *wri, char *con, char *tm,float pr); Book *query(int bookid); int BDatabase::qname(char* nm); Book* BDatabase::Qname(char* nm); int qzuozhe(char *wr); Book* Qzuozhe(char *wr); int qchuban(char *cb); Book* Qchuban(char *cb); void bookdata(); ~BDatabase(); void disp(); }; BDatabase::BDatabase() //建構函式,將book.txt讀到book[]中 { Book b; top=-1; fstream file("d:\\book.txt",ios::in); while (1) { file.read((char *)&b,sizeof(b)); if (!file) break; top++; book[top]=b; } file.close(); } void BDatabase::clear() //全刪 { top=-1; } int BDatabase::addbook(int n,char *na, char *wri, char *con,char *tm, float pr) //增加圖書 { Book *p=query(n); if (p==NULL) { top++; book[top].addbook(n,na,wri,con,tm,pr); return 1; } return 0; } Book* BDatabase::query(int bookid) //查詢圖書 { for (int i=0;i<=top;i++) if (book[i].getnum()==bookid && book[i].gettag()==0) return &book[i]; return NULL; } int BDatabase::qname(char* nm) { int j=0; for(int i=0;i<=top;i++) if((strcmp(book[i].getname(),nm)==0)&&book[i].gettag()==0) j++; if(j==0) return 0; else return j; } Book* BDatabase::Qname(char* nm) { int j=0; Book b[100]; for (int i=0;i<=top;i++) if ((strcmp(book[i].getname(),nm)==0) && book[i].gettag()==0) b[j++]=book[i]; if(j==0) return NULL; else return b; } int BDatabase::qzuozhe(char *wr) { int e=0; for (int i=0;i<=top;i++) if ((strcmp(book[i].getwriter(),wr)==0) && book[i].gettag()==0) e++; if(e==0) return 0; else return e; } Book* BDatabase::Qzuozhe(char *wr) { int f=0; Book b[100]; for (int i=0;i<=top;i++) if ((strcmp(book[i].getwriter(),wr)==0) && book[i].gettag()==0) b[f++]=book[i]; if(f==0) return NULL; else return b; } int BDatabase::qchuban(char *cb) { int g=0; for (int i=0;i<=top;i++) if ((strcmp(book[i].getconcem(),cb)==0) && book[i].gettag()==0) g++; if(g==0) return 0; else return g; } Book* BDatabase::Qchuban(char *cb) { int h=0; Book b[100]; for (int i=0;i<=top;i++) if ((strcmp(book[i].getconcem(),cb)==0) && book[i].gettag()==0) b[h++]=book[i]; if(h==0) return NULL; else return b; } void BDatabase::bookdata() //圖書庫維護 { int choice=1; char bname[40]; int bookid; char bwriter[15]; char bconcem[15]; char btime[15]; float bprice; Book *b; int r=0; while (choice!=0) { cout<<endl<<endl<<endl<<endl; cout << " ******************************************************"<<endl; cout << " ** 圖書維護 **"<<endl; cout << " ** 1:新增 **"<<endl; cout << " ** 2:刪除 **"<<endl; cout << " ** 3:查詢 **"<<endl; cout << " ** 4:顯示 **"<<endl; cout << " ** 5:全刪 **"<<endl; cout << " ** 0:退出 **"<<endl; cout << " ******************************************************"<<endl; cout<<"請選擇您將進行的操作:"; cin >> choice; switch (choice) { case 1: cout << " 輸入圖書編號:"; cin >> bookid; do { if(r!=0) { cout<<" 該編號已存在,請重新輸入圖書編號:"; cin>>bookid; } r++; if(r==3) break; } while(query(bookid)!=NULL); if(r==3) { system("cls"); break; } cout << " 輸入圖書書名:"; cin >> bname; cout << " 輸入圖書作者:"; cin >> bwriter; cout << " 輸入圖書出版社:"; cin >> bconcem; cout << " 輸入圖書出版時間:"; cin>>btime; cout << " 輸入圖書價格:"; cin >>bprice; cout<<"圖書新增成功"<<endl; Sleep(1000); system("cls"); addbook(bookid,bname,bwriter,bconcem,btime,bprice); break; case 2: cout << " 輸入圖書編號:"; cin >> bookid; b=query(bookid); if (b==NULL) { cout << " 該圖書不存在" << endl; Sleep(1000); system("cls"); break; } cout<<"該圖書已刪除"<<endl; Sleep(1000); system("cls"); b->delbook(); break; case 3: cout << " 輸入圖書編號:"; cin >> bookid; b=query(bookid); if (b==NULL) { cout << " 該圖書不存在" << endl; Sleep(1000); system("cls"); break; } system("cls"); cout<<"您所查詢的圖書資訊如下"<<endl; b->disp(); break; case 4: system("cls"); cout<<"所有圖書資訊如下"<<endl; disp(); break; case 5: clear(); cout<<"所有圖書已刪除"<<endl; Sleep(1000); system("cls"); break; } } system("cls"); } void BDatabase::disp() { Book temp; for (int i=0;i<=top&&(book[i].gettag()==0);i++) for(int j=i+1;j<=top&&(book[j].gettag()==0);j++) if(strcmp(book[i].gettime(),book[j].gettime())<0) { temp=book[i]; book[i]=book[j]; book[j]=temp; } for(int x=0;x<=top&&(book[x].gettag()==0);x++) book[x].disp(); } BDatabase::~BDatabase() //解構函式,將book[]寫入book.txt檔案中 { fstream file("d:\\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(); } class Rdatebase //讀者庫類 { int top; Reader read[Maxr]; //讀者記錄 public: Rdatebase(); void disp(); void clear(); void addreader(int n,char *na,char* mi) ; Reader *query(int readerid); ~Rdatebase(); void readerdata(); }; Rdatebase::Rdatebase()//建構函式,將reader.txt讀到read[]中 { Reader s; top=-1; fstream file("d:\\reader.txt",ios::in); while (1) { file.read((char *)&s,sizeof(s)); if (!file) break; top++; read[top]=s; } file.close(); } void Rdatebase::disp() { for (int i=0;i<=top;i++) if (read[i].gettag()==0) read[i].disp(); } void Rdatebase::clear() { top=-1; } void Rdatebase::addreader(int n,char *na,char* mi) { top++; read[top].addreader(n,na,mi); } Reader* Rdatebase::query(int readerid) { for (int i=0;i<=top;i++) if (read[i].getnum()==readerid &&read[i].gettag()==0) return &read[i]; return NULL; } Rdatebase::~Rdatebase() //解構函式,將read[]寫入reader.txt檔案中 { fstream file("d:\\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 Rdatebase::readerdata()//讀者庫維護 { int choice=1; char rname[20]; int readerid; char rmima[6]; Reader *r; while (choice!=0) { cout<<endl<<endl<<endl<<endl; cout << " ******************************************************"<<endl; cout << " ** 讀者維護 **"<<endl; cout << " ** 1:新增 **"<<endl; cout << " ** 2:更改 **"<<endl; cout << " ** 3:刪除 **"<<endl; cout << " ** 4:查詢 **"<<endl; cout << " ** 5:顯示 **"<<endl; cout << " ** 6:全刪 **"<<endl; cout << " ** 0:退出 **"<<endl; cout << " ******************************************************"<<endl; cout<<"請選擇您將進行的操作:"; cin >> choice; int a=0; int i=0; switch (choice) { case 0: system("cls"); break; case 1: cout << " 輸入讀者編號:"; cin >> readerid; do { if(a!=0) { cout<<"該讀者已存在,請重新輸入:"; cin >> readerid; } a++; } while(query(readerid)!=NULL); cout << " 輸入讀者密碼:"; for(i;i<6;i++) { rmima[i]=getch(); putch('*'); } cout <<endl<< " 輸入讀者姓名:"; cin >> rname; cout<<"讀者新增成功"<<endl; addreader(readerid,rname,rmima); Sleep(1000); system("cls"); break; case 2: cout << " 輸入讀者編號:"; cin >> readerid; r=query(readerid); if (r==NULL) { cout << " 該讀者不存在" << endl; Sleep(1000); system("cls"); break; } cout << " 輸入新的讀者姓名:"; cin >> rname; cout<<"讀者更改成功"<<endl; Sleep(1000); system("cls"); r->setname(rname); break; case 3: cout << " 輸入讀者編號:"; cin >> readerid; r=query(readerid); if (r==NULL) { cout <<" 該讀者不存在" << endl; Sleep(1000); system("cls"); break; } cout<<"讀者刪除成功"<<endl; Sleep(1000); system("cls"); r->delreader(); break; case 4: cout << "讀入讀者編號:"; cin >> readerid; r=query(readerid); if (r==NULL) { cout <<"該讀者不存在"<< endl; Sleep(1000); system("cls"); break; } system("cls"); cout<<"您所查詢的讀者資訊如下"<<endl; r->disp(); break; case 5: system("cls"); cout<<"所有讀者資訊如下"<<endl; disp(); break; case 6: clear(); cout<<"所有讀者已刪除"<<endl; Sleep(1000); system("cls"); break; } } }