圖書館源碼
㈠ C++圖書館管理系統源代碼
/*****************************************************************************************/
#include <iostream>
#include <iomanip>
#include <cstring>
#include <fstream>//輸入/輸出文件流類
using namespace std;
const int Maxr=100;//最多的讀者
const int Maxb=100;//最多的圖書
const int Maxbor=5;//每位讀者最多借五本書
//讀者類,實現對讀者的信息的描述
class Reader
{
private:
int tag; //刪除標記 1:已刪 0:未刪
int no; //讀者編號
char name[10]; //讀者姓名
int borbook[Maxbor];//所借圖書
public:
Reader() {}
char *getname() {return name;} //獲取姓名
int gettag() {return tag;} //獲取刪除標記
int getno() {return no;} //獲取讀者編號
void setname(char na[]) //設置姓名
{
strcpy(name,na);
}
void delbook(){ tag=1; }//設置刪除標記 1:已刪 0:未刪
void addreader(int n,char *na)//增加讀者
{
tag=0;
no=n;
strcpy(name,na);
for(int i=0;i<Maxbor;i++)
borbook[i]=0;
}
void borrowbook(int bookid)//借書操作
{
for(int i=0;i<Maxbor;i++)
{
if (borbook[i]==0)
{
borbook[i]=bookid;
return;
}
}
}
int retbook(int bookid)//還書操作
{
for(int i=0;i<Maxbor;i++)
{
if(borbook[i]==bookid)
{
borbook[i]=0;
return 1;
}
}
return 0;
}
void disp()//讀出讀者信息
{
cout << setw(5) << no <<setw(10) << 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 (1)
{
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].getno()==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;
char rname[20];
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 << "輸入讀者編號:";
cin >> readerid;
cout << "輸入讀者姓名:";
cin >> rname;
addreader (readerid,rname);
break;
case '2':
cout << "輸入讀者編號:";
cin >> readerid;
r=query(readerid);
if (r==NULL)
{
cout << " 該讀者不存在 "<<endl;
break;
}
cout << "輸入新的姓名:";
cin >> rname;
r->setname(rname);
break;
case '3':
cout << " 輸入讀者編號:";
cin >> readerid;
r=query(readerid);
if (r==NULL)
{
cout <<" 該讀者不存在" << endl;
break;
}
r->delbook();
break;
case '4':
cout << "讀入讀者編號:";
cin >> readerid;
r=query(readerid);
if (r==NULL)
{
cout <<"該讀者不存在"<< endl;
break;
}
r->disp();
break;
case '5':
disp();
break;
case '6':
clear();
break;
default:cout<<"輸入錯誤,請從新輸入:";break;
}
}
}
//圖書類,實現對圖書的描述,圖書的編號,書名,借出,還入等
class Book
{
private:
int tag;//刪除標記 1:已刪 0:未刪
int no;//圖書編號
char name[20];//書名
int onshelf;//是否再架 1:再架 2:已借
public:
Book(){}
char *getname() { return name; }//獲取姓名
int getno(){ return no; }//獲取圖書編號
int gettag(){ return tag; }//獲取刪除標記
void setname(char na[])//設置書名
{
strcpy(name,na);
}
void delbook(){ tag=1;}//刪除圖書
void addbook(int n,char *na)//增加圖書
{
tag=0;
no=n;
strcpy(name,na);
onshelf=1;
}
int borrowbook()//借書操作
{
if (onshelf==1)
{
onshelf=0;
return 1;
}
return 0;
}
void retbook()//還書操作
{
onshelf=1;
}
void disp()//輸出圖書
{
cout << setw(6) << no << setw(18) << name << setw(10)
<<(onshelf==1? "在架":"已借") <<endl;
}
};
//圖書庫類,實現對圖書的維護,查找,刪除等
class BDatabase
{
private:
int top; //圖書記錄指針
Book book[Maxb]; //圖書記錄
public:
BDatabase()//構造函數,將book.txt讀到book[]中
{
Book b;
top=-1;
fstream file("book.txt",ios::in);
while (1)
{
file.read((char *)&b,sizeof(b));
if (!file) break;
top++;
book[top]=b;
}
file.close();
}
void clear()//全刪
{
top=-1;
}
int addbook(int n,char *na)//增加圖書
{
Book *p=query(n);
if (NULL==p)
{
top++;
book[top].addbook(n,na);
return 1;
}
return 0;
}
Book *query(int bookid)//查找圖書
{
for (int i=0;i<=top;i++)
if (book[i].getno()==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;
char bname[40];
int bookid;
Book *b;
while (choice!='0')
{
cout <<"\n\n\n\t\t\t圖 書 維 護 "<<endl<<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 << "輸入圖書編號:"<<endl;
cin >> bookid;
cout << "輸入圖書書名:"<<endl;
cin >> bname;
addbook(bookid,bname);
break;
case '2':
cout << "輸入圖書編號:"<<endl;
cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout << " 該圖書不存在 "<<endl;
break;
}
cout << "輸入新的書名:"<<endl;
cin >> bname;
b->setname(bname);
break;
case '3':
cout <<" 讀入圖書編號:"<<endl;
cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout <<" 該圖書不存在" << endl;
break;
}
b->delbook();
break;
case '4':
cout << " 讀入圖書編號:"<<endl;
cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout <<" 該圖書不存在"<< endl;
break;
}
b->disp();
break;
case '5':
disp();
break;
case '6':
clear();
break;
default:cout<<"輸入錯誤,請從新輸入:";
}
}
}
//main() 函數的實現,程序的主界面的引導
int main(void)
{
char choice;
int bookid,readerid;
RDatabase ReaderDB;
Reader *r;
BDatabase BookDB;
Book *b;
while(choice!='0')
{
cout <<endl<<endl<<"\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\t0 離 開"<<endl;
cin >> choice;
switch (choice)
{
case '1':
cout <<" 借書 讀者編號:";
cin >>readerid;
cout <<" 圖書編號: ";
cin >>bookid;
r=ReaderDB.query(readerid);//按編號查找
if (NULL==r)
{
cout <<" 不存在該讀者,不能借書"<< endl;
break;
}
b=BookDB.query(bookid);
if (b==NULL)
{
cout <<" 不存在該圖書,不能借書"<< endl;
break;
}
if (b->borrowbook()==0)
{
cout << " 該圖書已借出,不能借書"<< endl;
break;
}
r->borrowbook(b->getno());
break;
case '2':
cout<<"還書\n 讀者編號:";
cin >>readerid;
cout << " 圖書編號:";
cin >>bookid;
r=ReaderDB.query(readerid);
if (r==NULL)
{
cout <<" 不存在該讀者,不能還書" << endl;
break;
}
b=BookDB.query(bookid);
if (b==NULL)
{
cout <<" 不存在該圖書,不能還書" <<endl;
break;
}
b->retbook();
r->retbook(b->getno());
break;
case '3':
BookDB.bookdata();
break;
case '4':
ReaderDB.readerdata();
break;
default:cout<<"輸入錯誤,請從新輸入:";
}
}
return 0;
}
㈡ 用C語言編寫的"圖書館管理系統"的源代碼,刪減版
#include <iostream>
#include <iomanip>
#include <string>
//輸入/輸出文件流類
#include <fstream>
using namespace std;
//最多的讀者
const int Maxr=100;
//最多的圖書
const int Maxb=100;
//每位讀者最多借五本書
const int Maxbor=5;
//讀者類,實現對讀者的信息的描述
class Reader
{
private:
//刪除標記 1:已刪 0:未刪
int tag;
//讀者編號
int no;
//讀者姓名
char name[10];
//所借圖書
int borbook[Maxbor];
public:
Reader()
{
;
}
//獲取姓名
char *getname()
{
return name;
}
//獲取刪除標記
int gettag()
{
return tag;
}
//獲取讀者編號
int getno()
{
return no;
}
//設置姓名
void setname(char na[])
{
strcpy(name,na);
}
//設置刪除標記 1:已刪 0:未刪
void delbook()
{
tag=1;
}
//增加讀者
void addreader(int n,char *na)
{
tag=0;
no=n;
strcpy(name,na);
for(int i=0;i<Maxbor;i++)
borbook[i]=0;
}
//借書操作
void borrowbook(int bookid)
{
for(int i=0;i<Maxbor;i++)
{
if (borbook[i]==0)
{
borbook[i]=bookid;
return;
}
}
}
//還書操作
int retbook(int bookid)
{
for(int i=0;i<Maxbor;i++)
{
if(borbook[i]==bookid)
{
borbook[i]=0;
return 1;
}
}
return 0;
}
//讀出讀者信息
void disp()
{
cout << setw(5) << no <<setw(10) << 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:
//構造函數,將reader.txt讀到read[]中
RDatabase()
{
Reader s;
top=-1;
//打開一個輸入文件
fstream file("reader.txt",ios::in);
while (1)
{
file.read((char *)&s,sizeof(s));
if (!file)break;
top++;
read[top]=s;
}
//關閉 reader.txt
file.close();
}
//刪除所有讀者信息
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].getno()==readerid &&
read[i].gettag()==0)
{
return &read[i];
}
return NULL;
}
//輸出所有讀者信息
void disp()
{
for (int i=0;i<=top;i++)
read[i].disp();
}
//讀者庫維護
void readerdata();
//析構函數,將read[]寫到reader.txt文件中
~RDatabase()
{
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;
char rname[20];
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 << "輸入讀者編號:";
cin >> readerid;
cout << "輸入讀者姓名:";
cin >> rname;
addreader (readerid,rname);
break;
case '2':
cout << "輸入讀者編號:";
cin >> readerid;
r=query(readerid);
if (r==NULL)
{
cout << " 該讀者不存在 "<<endl;
break;
}
cout << "輸入新的姓名:";
cin >> rname;
r->setname(rname);
break;
case '3':
cout << " 輸入讀者編號:";
cin >> readerid;
r=query(readerid);
if (r==NULL)
{
cout <<" 該讀者不存在" << endl;
break;
}
r->delbook();
break;
case '4':
cout << "讀入讀者編號:";
cin >> readerid;
r=query(readerid);
if (r==NULL)
{
cout <<"該讀者不存在"<< endl;
break;
}
r->disp();
break;
case '5':
disp();
break;
case '6':
clear();
break;
default:cout<<"輸入錯誤,請從新輸入:";break;
}
}
}
//圖書類,實現對圖書的描述,圖書的編號,書名,借
private:
//刪除標記 1:已刪 0:未刪
int tag;
//圖書編號
int no;
//書名
char name[20];
//是否再架 1:再架 2:已借
int onshelf;
public:
Book(){}
//獲取姓名
char *getname() { return name; }
//獲取圖書編號
int getno(){ return no; }
//獲取刪除標記
int gettag(){ return tag; }
//設置書名
void setname(char na[])
{
strcpy(name,na);
}
//刪除圖書
void delbook(){ tag=1;}
//增加圖書
void addbook(int n,char *na)
{
tag=0;
no=n;
strcpy(name,na);
onshelf=1;
}
//借書操作
int borrowbook()
{
if (onshelf==1)
{
onshelf=0;
return 1;
}
return 0;
}
//還書操作
void retbook()
{
onshelf=1;
}
//輸出圖書
void disp()
{
cout << setw(6) << no << setw(18) << name << setw(10)
<<(onshelf==1? "在架":"已借") <<endl;
}
};
//圖書庫類,實現對圖書的維護,查找,刪除等
class BDatabase
{
private:
//圖書記錄指針
int top;
//圖書記錄
Book book[Maxb];
public:
//構造函數,將book.txt讀到book[]中
BDatabase()
{
Book b;
top=-1;
fstream file("book.txt",ios::in);
while (1)
{
file.read((char *)&b,sizeof(b));
if (!file) break;
top++;
book[top]=b;
}
file.close();
}
//全刪
void clear()
{
top=-1;
}
//增加圖書
int addbook(int n,char *na)
{
Book *p=query(n);
if (NULL==p)
{
top++;
book[top].addbook(n,na);
return 1;
}
return 0;
}
//查找圖書
Book *query(int bookid)
{
for (int i=0;i<=top;i++)
if (book[i].getno()==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();
}
//析構函數,將book[]寫到book.txt文件中
~BDatabase()
{
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;
char bname[40];
int bookid;
Book *b;
while (choice!='0')
{
cout <<"\n\n\n\t\t\t圖 書 維 護 "<<endl<<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; {
case '1':
cout << "輸入圖書編號:"<<endl;
cin >> bookid;
cout << "輸入圖書書名:"<<endl;
cin >> bname;
addbook(bookid,bname);
break;
case '2':
cout << "輸入圖書編號:"<<endl;
cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout << " 該圖書不存在 "<<endl;
break;
}
cout << "輸入新的書名:"<<endl;
cin >> bname;
b->setname(bname);
break;
case '3':
cout <<" 讀入圖書編號:"<<endl;
cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout <<" 該圖書不存在" << endl;
break;
}
b->delbook();
break;
case '4':
cout << " 讀入圖書編號:"<<endl;
cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout <<" 該圖書不存在"<< endl;
break;
}
b->disp();
break;
case '5':
disp();
break;
case '6':
clear();
break;
default:cout<<"輸入錯誤,請從新輸入:";
}
}
}
//main() 函數的實現,程序的主界面的引導
void main()
{
char choice;
int bookid,readerid;
RDatabase ReaderDB;
Reader *r;
BDatabase BookDB;
Book *b;
while(choice!='0')
{
cout <<endl<<endl<<"\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\t0 離 開"<<endl;
cin >> choice;
switch (choice)
{
case '1':
cout <<" 借書 讀者編號:";
cin >>readerid;
cout <<" 圖書編號: ";
cin >>bookid;
//按編號查找
r=ReaderDB.query(readerid);
if (NULL==r)
{
cout <<" 不存在該讀者,不能借書"<< endl;
break;
}
b=BookDB.query(bookid);
if (b==NULL)
{
cout <<" 不存在該圖書,不能借書"<< endl;
break;
}
if (b->borrowbook()==0)
{
cout << " 該圖書已借出,不能借書"<< endl;
break;
}
r->borrowbook(b->getno());
break;
case '2':
cout<<"還書\n 讀者編號:";
cin >>readerid;
cout << " 圖書編號:";
cin >>bookid;
r=ReaderDB.query(readerid);
if (r==NULL)
{
cout <<" 不存在該讀者,不能還書" << endl;
break;
}
b=BookDB.query(bookid);
if (b==NULL)
{
cout <<" 不存在該圖書,不能還書" <<endl;
break;
}
b->retbook();
r->retbook(b->getno());
break;
case '3':
BookDB.bookdata();
break;
case '4':
ReaderDB.readerdata();
break;
default:cout<<"輸入錯誤,請從新輸入:";
}
}
}
㈢ c++圖書館藏書管理系統源代碼
#include"stdio.h"
#include"string.h"
struct book /*定義結構體*/
{ char number[20];
char name[20];
char press[20];
char writer[20];
int n; /*現有量*/
int m; /*總量*/
}a[10];
int b; /*輸入書的數量*/
void input() /*輸入圖書信息函數*/
{
FILE *fp;
char str2[10]={" "};
int i;
if((fp=fopen("d:\\wen","w"))==NULL)
{ printf("Open failed\n");
return; }
printf("\nHow many books do you want to input?\n");
scanf("%d",&b);
for(i=0;i<b;i++)
{
printf("Enter the %d book's number name press writer now all\n",i+1);
scanf("%s%s%s%s%d%d",&a[i].number,&a[i].name,&a[i].press,&a[i].writer,&a[i].n,&a[i].m);
fwrite(&a[i],sizeof(struct book),1,fp);
}
fclose(fp);
}
void Search() /*查詢書籍*/
{
FILE *fp;
int w,i; /*w查詢方式使用的數據*/
char str[20]; /*查詢書籍的編碼書名出版社作者*/
if((fp=fopen("d:\\wen","r"))==NULL)
{ printf("Open failed\n");
return; }
for(i=0;i<b;i++)
if(fread(&a[i],sizeof(struct book),1,fp)!=1)
printf("file read error\n");
printf("search by number--6\n");
printf("search by name--7\n");
printf("search by writer--8\n");
printf("search by press--9\n");
scanf("%d",&w);
printf("Enter the word you want to search:\n");
scanf("%s",str);
switch(w)
{
case 6:
{for(i=0;i<b;i++)
if(strcmp(a[i].number,str)==0)
{printf("number: %s name: %s press: %s writer: %s now: %d all:%d\n",a[i].number,a[i].name,a[i].press,a[i].writer,a[i].n,a[i].m);
break;
}
}
break;
case 7:
{for(i=0;i<b;i++)
if(strcmp(a[i].name,str)==0)
{printf("number: %s name: %s press: %s writer: %s now: %d all:%d\n",a[i].number,a[i].name,a[i].press,a[i].writer,a[i].n,a[i].m);
break;}
}
break;
case 8:
{for(i=0;i<b;i++)
if(strcmp(a[i].press,str)==0)
{printf("number: %s name: %s press: %s writer: %s now: %d all:%d\n",a[i].number,a[i].name,a[i].press,a[i].writer,a[i].n,a[i].m);
break;}
}
break;
case 9:
{for(i=0;i<b;i++)
if(strcmp(a[i].writer,str)==0)
{ printf("number: %s name: %s press: %s writer: %s now: %d all:%d\n",a[i].number,a[i].name,a[i].press,a[i].writer,a[i].n,a[i].m);
break; }
}
}
fclose(fp);
}
void borrow() /*借閱圖書函數*/
{ FILE *fp;
int i;
char xuehao[20],number2[20];
char jieyue[20]={"borrow:"};
if((fp=fopen("d:\\wen00.txt","w"))==NULL)
{ printf("Open failed\n");
return; }
printf("Enter your number:\n");
scanf("%s",xuehao);
printf("Enter the book's number which you want to borrow\n");
scanf("%s",number2);
for(i=0;i<10;i++)
if(strcmp(a[i].number,number2)==0)
if(a[i].n>0)
{a[i].n--;
printf("borrow sucess!\n");
if(fwrite(&xuehao[20],sizeof(xuehao[20]),1,fp)!=1)
printf("file write error\n");
if(fwrite(&jieyue[20],sizeof(jieyue),1,fp)!=1)
printf("file write error\n");
if(fwrite(&a[i],sizeof(struct book),1,fp)!=1)
printf("file write error\n");
break;
}
else
printf("borrow failed!");
fclose(fp);
}
void giveback() /*歸還圖書函數*/
{FILE *fp;
int i;
char xuehao[20],number2[20];
char guihuan2[20]={"give back"};
if((fp=fopen("d:\\wen00.txt","w"))==NULL)
{printf("cannot open file\n");
return;
}
printf("Enter your number:\n");
scanf("%s",xuehao);
printf("Enter the book's number which you want to give back\n");
scanf("%s",number2);
for(i=0;i<1000;i++)
if(strcmp(a[i].number,number2)==0)
{ if(fwrite(&xuehao[20],sizeof(xuehao),1,fp)!=1)
printf("file write error\n");
if(fwrite(&guihuan2[20],sizeof(guihuan2),1,fp)!=1)
printf("file write error\n");
if(fwrite(&a[i],sizeof(a[i]),1,fp)!=1)
printf("file write error\n");
break;
}
printf("give back sucess!\n");
fclose(fp);
}
void adddelet() /*增加刪除功能*/
{ FILE *fp;
char number3[20];
int s,i,j,t; /*t為增加或刪除 s增加書的數量*/
if((fp=fopen("d:\\wen00.txt","w"))==NULL)
{printf("cannot open file\n");
return ;
}
printf("Chose what you want to do:");
printf("add books--11\n");
printf("delet books--12\n");
scanf("%d",&t);
switch(t)
{
case 11:
{printf("Enter the book'number which you want to add:");
scanf("%s",number3);
printf("How many books do you want to add?:");
scanf("%d",&s);
for(i=0;i<10;i++)
if(strcmp(a[i].number,number3)==0)
a[i].m=a[i].m+s;
} break;
default :
{printf("Enter the book'number which you want to delet:");
scanf("%s",number3);
for(i=0;i<10;i++)
if(strcmp(a[i].number,number3)==0)
for(;i<10;i++)
{ for(j=0;j<20;j++)
a[i].number[j]=a[i+1].number[j];
for(j=0;j<20;j++)
a[i].name[j]=a[i+1].name[j];
for(j=0;j<20;j++)
a[i].press[j]=a[i+1].press[j];
for(j=0;j<20;j++)
a[i].writer[j]=a[i+1].writer[j];
a[i].n=a[i+1].n;
a[i].m=a[i+1].m;
}
}
}
fclose(fp);
}
void main() /*主函數*/
{
printf("Welcome to labrary!\nChose what you want to do:\n");
printf("Input book's information--1;\n");
printf("serach books--2;\n");
printf("borrow books--3;\n");
printf("add or delet books--4;\n");
printf("give back books--5;\n");
printf("quit--6;\n");
printf("Please chose zhe number:");
while(1)
{
switch(getchar())
{
case '1': input();break;
case '2': Search();break;
case '3': borrow() ;break;
case '4': adddelet();break;
case '5': giveback();break;
}
}
}
㈣ 求圖書館管理系統C++源代碼
#include<iostream>
#include<fstream>
#include<windows.h>
#include<stdlib.h>
using namespace std;
void mainmenu();
void Manage_Book();
void input_Book();
void input_Reader();
void New_Book();
void Seek_Book();
void Borrow_Manage();
void Borrow_Book();
void Return_Book();
void Output_Message();
void Exit_System();
int u,v=3,k=0;
struct Book
{
char name[20];
int booknumber;
int num_present;
int num_all;
}book[1000];
struct Reader
{
char name[20];
int readernumber;
int card;
}reader[3];
void mainmenu()
{
int Num_Choice1,flag=1;
system("color E");
for(;k<=0;k++)
{
input_Book();
input_Reader();
}
while(flag==1)
{
system("cls");
cout<<"\t\t\t◆◆◆◆◆主菜單◆◆◆◆◆"<<endl
<<"\t\t\t☆ 1.圖書管理 ☆"<<endl
<<"\t\t\t☆ 2.借閱管理 ☆"<<endl
<<"\t\t\t☆ 3.輸出信息 ☆"<<endl
<<"\t\t\t☆ 4.退出系統 ☆"<<endl
<<"\t\t\t◆◆◆◆◆◆◆◆◆◆◆◆◆"<<endl;
cout<<"請輸入所需要的服務編碼"<<endl;
cin>>Num_Choice1;
switch(Num_Choice1)
{
case 1:
Manage_Book();
break;
case 2:
Borrow_Manage();
break;
case 3:
Output_Message();
break;
case 4:
Exit_System();
flag=0;
break;
default:
flag=0;
cout<<"輸入有誤!!"<<endl;
}
}
}
void Manage_Book()
{
int flag1=1,Num_Choice2;
system("cls"); while(flag1==1)
{
cout<<"\t\t\t◆◆◆◆◆◆◆◆◆◆◆◆◆"<<endl
<<"\t\t\t☆ 1.新書入庫 ☆"<<endl
<<"\t\t\t☆ 2.圖書查詢 ☆"<<endl
<<"\t\t\t☆ 3.返回主菜單 ☆"<<endl
<<"\t\t\t◆◆◆◆◆◆◆◆◆◆◆◆◆"<<endl;
cin>>Num_Choice2;
switch(Num_Choice2)
{
case 1:
flag1=2;
New_Book();
break;
case 2:
flag1=2;
Seek_Book();
break;
case 3:
mainmenu();
default:
flag1=1;
cout<<"輸入有誤!"<<endl;
system("pause");
system("cls");
}
}}
void input_Book()
{
cout<<"請依照提示輸入現有的三本圖書的信息:"<<endl;
for(int i=0;i<3;i++)
{
cout<<"書名:"<<endl;
cin>>book[i].name;
cout<<"書號:"<<endl;
cin>>book[i].booknumber;
cout<<"庫存量:"<<endl;
cin>>book[i].num_all;
cout<<"現存量:"<<endl;
cin>>book[i].num_present;
}
}
void New_Book()
{
system("cls");
int j;
char newbook_name[20];
cout<<"請輸入新書書名:"<<endl;
cin>>newbook_name;
for(j=0;j<1000;j++)
{
if(strcmp(newbook_name,book[j].name)==0)
{
book[j].num_all++;
book[j].num_present++;
break;
}
}
if(j==1000)
{
cout<<"請為此新書編號:"<<endl;
v++;
cin>>book[v].booknumber;
strcpy(book[v].name,newbook_name);
book[v].num_all=1;
book[v].num_present=1;
}
else cout<<"已有此書,庫存量加一"<<endl;
system("pause");
}
void Seek_Book()
{
system("cls");
int k,booknumber;
cout<<"請輸入所要查詢的書號:"<<endl;
cin>>booknumber;
for(k=0;k<1000;k++)
{
if(book[k].booknumber==booknumber)
{
cout<<"書號:"<<booknumber<<endl;
cout<<"書名:"<<book[k].name<<endl;
cout<<"庫存量:"<<book[k].num_all<<endl;
cout<<"現存量:"<<book[k].num_present<<endl;
system("pause");
break;
}
}
if(k==1000)
{
cout<<"未找到此書!!"<<endl;
system("pause");
}
}
void Borrow_Manage()
{
int flag=1;
system("cls");
int Num_Choice3;
while(flag==1)
{
cout<<"\t\t\t◆◆◆◆◆◆◆◆◆◆◆◆◆"<<endl
<<"\t\t\t☆ 1.借書登記 ☆"<<endl
<<"\t\t\t☆ 2.還書管理 ☆"<<endl
<<"\t\t\t☆ 3.返回主菜單 ☆"<<endl
<<"\t\t\t◆◆◆◆◆◆◆◆◆◆◆◆◆"<<endl;
cin>>Num_Choice3;
switch(Num_Choice3)
{
case 1:
flag=2;
Borrow_Book();
break;
case 2:
flag=2;
Return_Book();
break;
case 3:
mainmenu();
default:
flag=1;
cout<<"輸入有誤!!"<<endl;
system("pause");
}
system("cls");
}
}
void input_Reader()
{
system("cls");
cout<<"請依照提示輸入現有的三位讀者的信息:"<<endl;
for(int m=0;m<3;m++)
{
cout<<"姓名:"<<endl;
cin>>reader[m].name;
cout<<"編號:"<<endl;
cin>>reader[m].readernumber;
reader[m].card=0;
}
}
void Borrow_Book()
{
system("cls");
char reader_name[20];
int n,book_number;
cout<<"請輸入讀者姓名:"<<endl;
cin>>reader_name;
for(n=0;n<3;n++)
{
if(strcmp(reader_name,reader[n].name)==0)
{
if(reader[n].card!=0)
{
cout<<"已借書,不能再借,請按任意鍵返回"<<endl;
system("pause");
mainmenu();
}
else
break;
}
}
if(n==3)
{
cout<<"無此讀者!輸入有誤!!按任意鍵返回主菜單"<<endl;
system("pause");
mainmenu();
}
cout<<"請輸入所借圖書書號:"<<endl;
cin>>book_number;
for(n=0;n<v;n++)
{
if(strcmp(reader_name,reader[n].name)==0)
reader[n].card=book_number;
}
for(n=0;n<v;n++)
{
if(book[n].booknumber==book_number)
{
if(book[n].num_present==0)
{
cout<<"該書已經被借完,請按任意鍵返回"<<endl;
system("pause");
for(n=0;n<v;n++)
{
if(strcmp(reader_name,reader[n].name)==0)
reader[n].card=0;
}
mainmenu();
}
else
{
book[n].num_present--;
cout<<"借書成功,請按任意鍵返回"<<endl;
system("pause");
break;
}
}
}
if(n==v)
{
for(n=0;n<v;n++)
{
if(strcmp(reader_name,reader[n].name)==0)
reader[n].card=0;
}
cout<<"所輸入的書號不存在,請按任意鍵返回主菜單"<<endl;
system("pause");
mainmenu();
}
}void Return_Book()
{
system("cls");
int j;
char reader_name[20];
int book_number;
cout<<"請輸入讀者姓名:"<<endl;
cin>>reader_name;
for(j=0;j<3;j++)
{
if(strcmp(reader_name,reader[j].name)==0)
break;
}
if(j==3)
{
cout<<"無此讀者!輸入有誤!!按任意鍵返回主菜單"<<endl;
system("pause");
mainmenu();
}
cout<<"請輸入所還圖書書號:"<<endl;
cin>>book_number;
for(j=0;j<3;j++)
{
if(strcmp(reader_name,reader[j].name)==0)
{
if(book_number==reader[j].card)
{
reader[j].card=0;
for(j=0;j<v;j++)
{
if(book[j].booknumber==book_number)
{
book[j].num_present++;
cout<<"還書成功,請按任意鍵返回"<<endl;
system("pause");
break;
}
}
}
else
{
cout<<"該讀者未藉此書"<<endl;
system("pause");
}
}
if(j==v)
cout<<"所輸入的書號不存在:"<<endl;
}
}
void Output_Message()
{
system("cls");
int q;
cout<<"以下是圖書信息"<<endl<<endl;
for(q=0;q<v;q++)
{
cout<<"書號:"<<book[q].booknumber<<'\t';
cout<<"書名:"<<book[q].name<<'\t';
cout<<"庫存量:"<<book[q].num_all<<'\t';
cout<<"現存量:"<<book[q].num_present<<endl;
}
cout<<"以下是讀者信息"<<endl<<endl;
for(q=0;q<3;q++)
{
cout<<"姓名:"<<reader[q].name<<'\t'
<<"編號:"<<reader[q].readernumber<<'\t'
<<"所借書號:"<<reader[q].card<<endl;
}
system("pause");
}
void Exit_System()
{
system("cls");
cout<<"歡迎下次使用,謝謝"<<endl;
system("pause");
}
void main()
{
mainmenu();
}
㈤ 圖書館管理系統代碼
/*****************************************************************************************/
#include <iostream>
#include <iomanip>
#include <cstring>
#include <fstream>//輸入/輸出文件流類
using namespace std;
const int Maxr=100;//最多的讀者
const int Maxb=100;//最多的圖書
const int Maxbor=5;//每位讀者最多借五本書
//讀者類,實現對讀者的信息的描述
class Reader
{
private:
int tag; //刪除標記 1:已刪 0:未刪
int no; //讀者編號
char name[10]; //讀者姓名
int borbook[Maxbor];//所借圖書
public:
Reader() {}
char *getname() {return name;} //獲取姓名
int gettag() {return tag;} //獲取刪除標記
int getno() {return no;} //獲取讀者編號
void setname(char na[]) //設置姓名
{
strcpy(name,na);
}
void delbook(){ tag=1; }//設置刪除標記 1:已刪 0:未刪
void addreader(int n,char *na)//增加讀者
{
tag=0;
no=n;
strcpy(name,na);
for(int i=0;i<Maxbor;i++)
borbook[i]=0;
}
void borrowbook(int bookid)//借書操作
{
for(int i=0;i<Maxbor;i++)
{
if (borbook[i]==0)
{
borbook[i]=bookid;
return;
}
}
}
int retbook(int bookid)//還書操作
{
for(int i=0;i<Maxbor;i++)
{
if(borbook[i]==bookid)
{
borbook[i]=0;
return 1;
}
}
return 0;
}
void disp()//讀出讀者信息
{
cout << setw(5) << no <<setw(10) << 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 (1)
{
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].getno()==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;
char rname[20];
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 << "輸入讀者編號:";
cin >> readerid;
cout << "輸入讀者姓名:";
cin >> rname;
addreader (readerid,rname);
break;
case '2':
cout << "輸入讀者編號:";
cin >> readerid;
r=query(readerid);
if (r==NULL)
{
cout << " 該讀者不存在 "<<endl;
break;
}
cout << "輸入新的姓名:";
cin >> rname;
r->setname(rname);
break;
case '3':
cout << " 輸入讀者編號:";
cin >> readerid;
r=query(readerid);
if (r==NULL)
{
cout <<" 該讀者不存在" << endl;
break;
}
r->delbook();
break;
case '4':
cout << "讀入讀者編號:";
cin >> readerid;
r=query(readerid);
if (r==NULL)
{
cout <<"該讀者不存在"<< endl;
break;
}
r->disp();
break;
case '5':
disp();
break;
case '6':
clear();
break;
default:cout<<"輸入錯誤,請從新輸入:";break;
}
}
}
//圖書類,實現對圖書的描述,圖書的編號,書名,借出,還入等
class Book
{
private:
int tag;//刪除標記 1:已刪 0:未刪
int no;//圖書編號
char name[20];//書名
int onshelf;//是否再架 1:再架 2:已借
public:
Book(){}
char *getname() { return name; }//獲取姓名
int getno(){ return no; }//獲取圖書編號
int gettag(){ return tag; }//獲取刪除標記
void setname(char na[])//設置書名
{
strcpy(name,na);
}
void delbook(){ tag=1;}//刪除圖書
void addbook(int n,char *na)//增加圖書
{
tag=0;
no=n;
strcpy(name,na);
onshelf=1;
}
int borrowbook()//借書操作
{
if (onshelf==1)
{
onshelf=0;
return 1;
}
return 0;
}
void retbook()//還書操作
{
onshelf=1;
}
void disp()//輸出圖書
{
cout << setw(6) << no << setw(18) << name << setw(10)
<<(onshelf==1? "在架":"已借") <<endl;
}
};
//圖書庫類,實現對圖書的維護,查找,刪除等
class BDatabase
{
private:
int top; //圖書記錄指針
Book book[Maxb]; //圖書記錄
public:
BDatabase()//構造函數,將book.txt讀到book[]中
{
Book b;
top=-1;
fstream file("book.txt",ios::in);
while (1)
{
file.read((char *)&b,sizeof(b));
if (!file) break;
top++;
book[top]=b;
}
file.close();
}
void clear()//全刪
{
top=-1;
}
int addbook(int n,char *na)//增加圖書
{
Book *p=query(n);
if (NULL==p)
{
top++;
book[top].addbook(n,na);
return 1;
}
return 0;
}
Book *query(int bookid)//查找圖書
{
for (int i=0;i<=top;i++)
if (book[i].getno()==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;
char bname[40];
int bookid;
Book *b;
while (choice!='0')
{
cout <<"\n\n\n\t\t\t圖 書 維 護 "<<endl<<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 << "輸入圖書編號:"<<endl;
cin >> bookid;
cout << "輸入圖書書名:"<<endl;
cin >> bname;
addbook(bookid,bname);
break;
case '2':
cout << "輸入圖書編號:"<<endl;
cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout << " 該圖書不存在 "<<endl;
break;
}
cout << "輸入新的書名:"<<endl;
cin >> bname;
b->setname(bname);
break;
case '3':
cout <<" 讀入圖書編號:"<<endl;
cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout <<" 該圖書不存在" << endl;
break;
}
b->delbook();
break;
case '4':
cout << " 讀入圖書編號:"<<endl;
cin >> bookid;
b=query(bookid);
if (b==NULL)
{
cout <<" 該圖書不存在"<< endl;
break;
}
b->disp();
break;
case '5':
disp();
break;
case '6':
clear();
break;
default:cout<<"輸入錯誤,請從新輸入:";
}
}
}
//main() 函數的實現,程序的主界面的引導
int main(void)
{
char choice;
int bookid,readerid;
RDatabase ReaderDB;
Reader *r;
BDatabase BookDB;
Book *b;
while(choice!='0')
{
cout <<endl<<endl<<"\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\t0 離 開"<<endl;
cin >> choice;
switch (choice)
{
case '1':
cout <<" 借書 讀者編號:";
cin >>readerid;
cout <<" 圖書編號: ";
cin >>bookid;
r=ReaderDB.query(readerid);//按編號查找
if (NULL==r)
{
cout <<" 不存在該讀者,不能借書"<< endl;
break;
}
b=BookDB.query(bookid);
if (b==NULL)
{
cout <<" 不存在該圖書,不能借書"<< endl;
break;
}
if (b->borrowbook()==0)
{
cout << " 該圖書已借出,不能借書"<< endl;
break;
}
r->borrowbook(b->getno());
break;
case '2':
cout<<"還書\n 讀者編號:";
cin >>readerid;
cout << " 圖書編號:";
cin >>bookid;
r=ReaderDB.query(readerid);
if (r==NULL)
{
cout <<" 不存在該讀者,不能還書" << endl;
break;
}
b=BookDB.query(bookid);
if (b==NULL)
{
cout <<" 不存在該圖書,不能還書" <<endl;
break;
}
b->retbook();
r->retbook(b->getno());
break;
case '3':
BookDB.bookdata();
break;
case '4':
ReaderDB.readerdata();
break;
default:cout<<"輸入錯誤,請從新輸入:";
}
}
return 0;
}
希望能解決您的問題。