#include#includeclassBook{//書籍基類protected:charTitle[40];//書名longCode;//條碼intType;//0表示書,1表示雜志public:Book();Book(char*title,longcode);voidSetCode(longcode){Code=code;}voidSetTitle(char*tl){strcpy(Title,tl);}voidSetType(booltype){Type=type;}intGetType(){returnType;}longGetCode(){returnCode;}virtualvoidShow();//顯示書的信息friendostream&operator>(istream&is,Book&);//重載提取運算符Book*Next;//為創建每個讀者所借書鏈表而定義指針};classItem:publicBook{//書的款目//charTitle[40];//書名charAuthor[20];//著者名charIndexCode[10];//分類號//longCode;//條碼public:Item();Item(char*author,char*title,char*index,intcode);Item(Item&);voidSetAuthor(char*);voidSetIndexCode(char*);virtualvoidShow();//顯示書的信息friendostream&operator>(istream&is,Item&);//重載提取運算符public:};classMagazine:publicBook{//雜志類intVolume;enumLANG{CHINESE=1,ENGLISH}Lang;//枚舉變數,確定語言類別public:Magazine():Book(){Volume=0;Lang=CHINESE;Type=1;}Magazine(char*title,intvol,LANGlang,intcode);Magazine(Magazine&);voidSetVolume(intvol){Volume=vol;}voidSetLang(intlang){Lang=(LANG)lang;}virtualvoidShow();//顯示雜志的信息friendostream&operator>(istream&is,Magazine&);//重載提取運算符};classReader{//讀者信息類charName[20];//姓名charPosition[20];//職務intAge;//年齡longCode;//借書證號Book*items;//所借書鏈表public:Reader();Reader(char*name,char*posi,intage,intcode);//Reader(Reader&);~Reader();longGetCode(){returnCode;}voidSetName(char*);voidSetPosition(char*);voidSetAge(int);voidSetCode(long);voidAddBook(Item);//添加所借書voidAddBook(Magazine);//添加所借雜志voidDelBook(Bookit);//還書後減少所借書voidShowBooks();//顯示所借書voidShow();//顯示讀者信息friendostream&operator>(istream&is,Reader&);//重載提取運算符public:intCounter;//計數器,統計所借書數目};classManager{//管理員類charName[20];//姓名intAge;//年齡intCode;//工號friendclassLibrary;//將圖書館類聲明為友元public:Manager(){}Manager(char*,int,int);longGetCode(){returnCode;}voidShow();friendostream&operator>(istream&is,Manager&);//重載提取運算符};classLoan{//借閱信息類intType;//0表示書,1表示雜志Itemitem;//借閱書Magazinemag;//借閱雜志Readerreader;//借閱者Managermanager;//借書操作員intCode;friendclassLibrary;//將圖書館類聲明為友元public:Loan(){}Loan(Loan&l);intGetCode(){returnCode;}voidShow();friendostream&operator>(istream&is,Loan&);//重載提取運算符};、、、、、、、、、、、、、、、、、、、、、、、#include#includeclassReader;templateclassDblList;templateclassDblNode{public:TInfo;//數據域DblNode*llink,*rlink;//前驅(左鏈)、後繼(右鏈)指針public:DblNode(Tdata);//一般結點DblNode();//頭結點TGetInfo(){returnInfo;};friendclassDblList;friendclassLibrary;};templateclassDblList{DblNode*head,*current;public:DblList();~DblList();voidInsert(constT&data);DblNode*Remove(DblNode*p);voidPrint();intLength();//計算鏈表長度DblNode*Find(Tdata);//搜索數據與定值相同的結點DblNode*Find(intdata);//按某個關鍵字查找voidMakeEmpty();//清空鏈表voidShowList();//顯示鏈表各結點friendistream&operator>>(istream&,DblList&);//重載輸入流運算符friendostream&operator&dlist);//重載輸出流運算符friendclassLibrary;//其它操作};templateDblNode::DblNode(){llink=rlink=NULL;}templateDblNode::DblNode(Tdata){info=data;llink=NULL;rlink=NULL;}templateDblList::DblList(){//建立表頭結點head=newDblNode();head->rlink=head->llink=head;current=NULL;}templateDblList::~DblList(){MakeEmpty();//清空鏈表deletehead;}templatevoidDblList::MakeEmpty(){DblNode*tempP;while(head->rlink!=head){tempP=head->rlink;head->rlink=tempP->rlink;//把頭結點後的第一個節點從鏈中脫離tempP->rlink->llink=head;//處理左指針deletetempP;//刪除(釋放)脫離下來的結點}current=NULL;//current指針恢復}templatevoidDblList::Insert(constT&data){//新節點在鏈尾current=newDblNode;current->Info=data;current->rlink=head;//注意次序current->llink=head->llink;head->llink->rlink=current;head->llink=current;//最後做}templateDblNode*DblList::Remove(DblNode*p){//刪除結點current=head->rlink;while(current!=head&¤t!=p)current=current->rlink;if(current==head)current=NULL;else{//結點摘下p->llink->rlink=p->rlink;p->rlink->llink=p->llink;p->rlink=p->llink=NULL;}returncurrent;}templateDblNode*DblList::Find(Tdata){//按結點查找current=head->rlink;while(current!=head&¤t->Info!=data)current=current->rlink;if(current==head)current=NULL;returncurrent;}templateDblNode*DblList::Find(intdata){//按數據值查找結點current=head->rlink;inttemp=current->Info.GetCode();while(current!=head&&temp!=data){current=current->rlink;temp=current->Info.GetCode();}if(current==head)current=NULL;returncurrent;}templatevoidDblList::Print(){//輸出鏈表current=head->rlink;while(current!=head){coutInforlink;}coutintDblList::Length(){//取得鏈表長度intcount=0;current=head->rlink;while(current!=head){count++;current=current->rlink;}returncount;}templatevoidDblList::ShowList(){//輸出鏈表各結點intcount=0;current=head->rlink;while(current!=head){current->GetInfo().Show();current=current->rlink;}return;}templateistream&operator>>(istream&is,DblList&dlist){intlen;Ttt;//定義局部變數讀入一個結點is>>len;for(inti=0;i>tt;dlist.Insert(tt);}returnis;}templateostream&operator&dlist){DblNode*tempP;intlen=dlist.Length();osrlink;while(tempP!=dlist.head){//循環輸出鏈表各結點osInfo;tempP=tempP->rlink;}returnos;}#include"class.h"#include"dblist.h"#includeclassLibrary{//封裝圖書館流通業務的類DblListitem;//在館圖書鏈表DblListmag;//在館雜志鏈表DblListreader;//讀者鏈表DblListloan;//借閱信息鏈表DblListmanager;//管理員信息鏈表intitemNum;//記錄在館圖書數目intmagNum;//記錄在館雜志數目intreaderNum;//記錄讀者數目intloanNum;//記錄借閱信息數目intmanagerNum;//記錄管理員數目ofstreamitemFileOut;//文件流對象,保存圖書館書籍數據ifstreamitemFileIn;//文件流對象,讀入圖書館書籍數據ofstreammagFileOut;//文件流對象,保存圖書館雜志數據ifstreammagFileIn;//文件流對象,讀入圖書館雜志數據ofstreamreaderFileOut;//文件流對象,保存圖書館讀者數據ifstreamreaderFileIn;//文件流對象,讀入圖書館讀者數據ofstreamloanFileOut;//文件流對象,保存圖書館借閱信息數據ifstreamloanFileIn;//文件流對象,讀入圖書館借閱信息ofstreammanagerFileOut;//文件流對象,保存圖書館管理員數據ifstreammanagerFileIn;//文件流對象,讀入圖書館管理員數據public:Library();//構造函數~Library();//析構函數voidRun();//圖書館類的運行函數voidCreateBibliotheca();//創建書目voidCreateReader();//創建讀者庫voidCreateManager();//創建管理員信息intShowMainMenu();//顯示主菜單函數voidBorrow();//借書操作voidReturn();//還書操作voidRequire();//查詢操作voidSaveInfo();//保存圖書館信息voidOpenInfo();//讀入圖書館信息};
⑵ 圖書管理系統c語言設計,求代碼
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#define STACK_INIT_SIZE 10
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
struct student /*定義學生類型,用於存放借出的書籍*/
{
int carnum;
char lendbook[10];
}student[1000];
struct employ /*定義職工類型*/
{
int employnum;
char employname[15];
int employage;
char employsex[2];
char employleve[10];
long int employtage;
}employ[50];
struct book /*定義書的類型*/
{
int booknum;
char bookname[10];
char bookcreat[10];
int turefalse; /*用於借書和還書模塊判斷一本書是否借出的條件*/
}book[1000];
struct car /*借書卡的數據類型*/
{
int carnum;
char studentname[10];
int studentclass;
}car[100];
huanbook() /*還書函數*/
{
FILE *fp,*fp2; /*定義兩個文件指針,fp2用於修改數據時設立臨時文件用,防止數據遭破壞*/
int i,n;
int carnum;
char lendbook[10];
printf("請你輸入你的卡號\n");
scanf("%d",&carnum);
fp=fopen("car.txt","r"); /*讀取卡號記錄*/
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++) /*for循環判斷卡號是否存在*/
{
if(car[i].carnum==carnum) /*卡號存在,進入下一循環*/
{
n=i;
fclose(fp);
printf("請輸入你要還的書的名字\n");
scanf("%s",lendbook);
fp=fopen("record.txt","r");
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++) /*判斷是否借閱了輸入的書*/
{
if(strcmp(student[i].lendbook,lendbook)==0) /*借閱了該書,進入下一循環,否則出錯顯示*/
{
fclose(fp);
fp=fopen("record.txt","r");
fp2=fopen("bookl.txt","w");
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++)
{
if(strcmp(student[i].lendbook,lendbook)==0)
{
continue; /*刪除還掉的書的借書記錄*/
}
fwrite(&student[i],sizeof(struct student),1,fp2); /*寫入原來沒還的書的記錄*/
}
fclose(fp);
fclose(fp2);
fp=fopen("record.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&student[i],sizeof(struct student),1,fp2)!=0;i++)
{
fwrite(&student[i],sizeof(struct student),1,fp); /*將借書記錄信息寫回*/
}
fclose(fp);
fclose(fp2);
fopen("bookl.txt","w"); /*清臨時文件的記錄*/
fclose(fp2);
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++) /*將書的記錄寫入臨時文件,防止因為修改信息破壞以前的記錄*/
{
if(i==n)
{
book[i].turefalse=1;
fwrite(&book[i],sizeof(struct book),1,fp2); /*將還的書的原來狀態設為無人借閱的*/
continue;
}
fwrite(&book[i],sizeof(struct book),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("book.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
{
fwrite(&book[i],sizeof(struct book),1,fp); /*將臨時文件寫回*/
}
fclose(fp);
fclose(fp2);
fopen("bookl.txt","w"); /*清臨時文件*/
fclose(fp2);
printf("還書完畢,按任意鍵返回\n");
getch();
return 1;
}
}
printf("你沒有借這樣的書,任意鍵返回\n"); /*出錯提示*/
fclose(fp);
getch();
return 0;
}
}
printf("系統沒這樣的卡,和管理員聯系,按任意鍵返回\n"); /*出錯提示*/
fclose(fp);
getch();
}
findbook()
{
FILE *fp;
char bookname[10];
int ture,i;
fp=fopen("book.txt","r");
printf("請輸入你要查找的書名\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(bookname,book[i].bookname)==0)
{
if(book[i].turefalse==1)
{
printf("這本書的詳細資料是:%d %s %s 此書現在無人借閱\n按任意鍵返回\n",book[i].booknum,book[i].bookname,book[i].bookcreat);
}
else {printf("這本書已經有人借出\n");fclose(fp);return 0;}
fclose(fp);
return FALSE;
}
}
printf("沒有你要查詢的書籍\n");
fclose(fp);
return FALSE;
}
findbook1()
{
FILE *fp;
char bookcreat[10];
int ture,i;
fp=fopen("book.txt","r");
printf("請輸入你要查找的作者名\n");
scanf("%s",bookcreat);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(bookcreat,book[i].bookcreat)==0)
{
if(book[i].turefalse==1)
{
printf("這本書的詳細資料是:%d %s %s 此書現在無人借閱\n按任意鍵返回\n",book[i].booknum,book[i].bookname,book[i].bookcreat);
}
else {printf("這本書已經有人借出\n");fclose(fp);return 0;}
fclose(fp);
return FALSE;
}
}
printf("沒有你要查詢的書籍\n");
fclose(fp);
return FALSE;
}
lendcount()
{
FILE *fp;
int i,n=0;
fp=fopen("record.txt","r");
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++)
{
printf("卡號:%d 借出的書籍:%s \n",student[i].carnum,student[i].lendbook);
n=n+1;
}
fclose(fp);
printf("目前共有%d本書借出\n",n);
printf("按任意鍵\n");
getch();
return n;
}
chabook()
{
char ch5;
do
{
printf("---------------歡迎進入圖書查詢系統!--------------\n");
printf(" 1:<按書名查找>\n");
printf(" 2:<按作者查找>\n");
printf(" 0:<返回>\n");
printf("請輸入0--2,其他輸入非法!\n");
scanf("%s",&ch5);
switch(ch5)
{
case '1':findbook();getch();break;
case '2':findbook1();getch();break;
case '0':break;
default:printf("無此操作\n");getch();break;
}
}while(ch5!='0');
return FALSE;
}
lendbook()
{
FILE *fp,*fp2;
int i,n;
int carnum;
printf("請你輸入你的卡號\n");
scanf("%d",&carnum);
fp=fopen("car.txt","r");
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++)
{
if(car[i].carnum==carnum)
{
n=i;
fclose(fp);
printf("請輸入你要借閱的書的名字\n");
scanf("%s",student[n].lendbook);
fp=fopen("book.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(book[i].bookname,student[n].lendbook)==0)
{
if(book[i].turefalse==0) {printf("對不起,此書有人借出,請借其他書\n");fclose(fp);getch();return;}
else
fclose(fp);
fp=fopen("record.txt","a+");
student[n].carnum=carnum;
fwrite(&student[n],sizeof(struct student),1,fp);
fclose(fp);
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(book[i].bookname,student[n].lendbook)==0)
{
book[i].turefalse=0;
fwrite(&book[i],sizeof(struct book),1,fp2);
continue;
}
fwrite(&book[i],sizeof(struct book),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("book.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
{
fwrite(&book[i],sizeof(struct book),1,fp);
}
fclose(fp);
fclose(fp2);
fopen("bookl.txt","w");
fclose(fp2);
printf("借書完畢,按任意鍵返回\n");
getch();
return;
}
}
printf("不存在這樣的書,任意鍵返回\n");
fclose(fp);
getch();
return;
}
}
printf("你的卡號不存在,請申請新卡,按任意鍵返回\n");
fclose(fp);
getch();
}
carcount()
{
FILE *fp;
int i,n=0;
fp=fopen("car.txt","r");
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++)
{
printf("第%d張卡<卡號:%d 姓名:%s 班級:%d>\n",i+1,car[i].carnum,car[i].studentname,car[i].studentclass);
n=n+1;
}
fclose(fp);
printf("目前共有%d本書\n",n);
printf("按任意鍵\n");
getch();
}
delcar()
{
FILE *fp,*fp2;
int i;
int carnum;
char choice;
fp=fopen("car.txt","r");
fp2=fopen("bookl.txt","w");
printf("請輸入你要刪除的卡號\n");
printf("如果你輸入的卡號存在,系統自動刪除該信息!如果不存在,系統不做任何改動\n");
scanf("%d",&carnum);
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++)
{
if(car[i].carnum!=carnum)
{
fwrite(&car[i],sizeof(struct car),1,fp2);
}
}
fclose(fp);
fclose(fp2);
printf("是否真的要刪除該卡?刪除後該書籍的所有信息將無法恢復《Y/N》\n");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
fp=fopen("car.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&car[i],sizeof(struct car),1,fp2)!=0;i++)
{
fwrite(&car[i],sizeof(struct car),1,fp);
}
fclose(fp);
fclose(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意鍵返回\n");
getch();
return;
}
else
{
printf("按任意鍵返回\n");
getch();
return;
}
}
addcar()
{
FILE *fp;
int i=0;
fp=fopen("car.txt","a+");
printf("請你輸入卡號\n");
scanf("%d",&car[i].carnum);
printf("請你輸入學生姓名\n");
scanf("%s",car[i].studentname);
printf("請你輸入班級\n");
scanf("%d",&car[i].studentclass);
fwrite(&car[i],sizeof(struct car),1,fp);
fclose(fp);
printf("輸入完畢,任意鍵返回\n");
getch();
}
⑶ 圖書館管理系統代碼
/*****************************************************************************************/
#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 <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct BOOK
{
int id,usr[10],total,store,days[10];
char name[31],author[21];
}books[100];
/*上面是結構體的定義,用於存放書籍及借書的信息。*/
void page_title(char *menu_item)
{
clrscr();
printf(">>> 圖 書 管 理 系 統 <<<\n\n- %s -\n\n",menu_item);
}
/*上面是列印頁眉的函數,同時通過參數menu_item,可以顯示當前的狀態。*/
void return_confirm(void)
{
printf("\n按任意鍵返回……\n");
getch();
}
/*上面是返回前請求確認的函數,以便在返回前觀察結果*/
int search_book(void)
{
int n,i;
printf("請輸入圖書序號:");
scanf("%d",&i);
for(n=0;n<100;n++)
{
if(books[n].id==i)
{
printf("書名:%s\n",books[n].name);
printf("作者:%s\n",books[n].author);
printf("存數:%d of ",books[n].store);
printf("%d\n",books[n].total);
return n;
}
}
printf("\n輸入錯誤或無效圖書序號.\n");
return -1;
}
/*上面的函數是在數組中找到圖書號匹配的記錄,顯示其信息並返
回數組下標,如果找不到相應記錄則提示錯誤並返回-1。*/
void book_out(void)
{
int n,s,l,d;
page_title("借閱圖書");
if((n=search_book())!=-1&&books[n].store>0)
{
printf("請輸入借書證序號:");
scanf("%d",&s);
printf("請輸入可借天數:");
scanf("%d",&d);
for(l=0;l<10;l++)
{
if(books[n].usr[l]==0)
{
books[n].usr[l]=s;
books[n].days[l]=d;
break;
}
}
books[n].store--;
}
if(n!=-1&&books[n].store==0) printf("此書已經全部借出.\n");
return_confirm();
}
/*上面是借書的函數,首先調用找書函數*/
void book_in(void)
{
int n,s,l;
page_title("歸還圖書");
if((n=search_book())!=-1&&books[n].store<books[n].total)
{
printf("借閱者圖書證列表:\n");
for(l=0;l<10;l++)
if (books[n].usr[l]!=0)
printf("[%d] - %d天\n",books[n].usr[l],books[n].days[l]);
printf("請輸入借書證序號:");
scanf("%d",&s);
for(l=0;l<10;l++)
{
if(books[n].usr[l]==s)
{
books[n].usr[l]=0;
books[n].days[l]=0;
break;
}
}
books[n].store++;
}
if(n!=-1&&books[n].store==books[n].total)
printf("全部入藏.\n");
return_confirm();
}
void book_add(void)
{
int n;
page_title("注冊新書");
for(n=0;n<100;n++)
if(books[n].id==0) break;
printf("序號:");
scanf("%d",&books[n].id);
printf("書名:");
scanf("%s",&books[n].name);
printf("作者:");
scanf("%s",&books[n].author);
printf("數量:");
scanf("%d",&books[n].total);
books[n].store=books[n].total;
return_confirm();
}
void book_del(void)
{
int n;
page_title("注銷舊書");
if((n=search_book())!=-1) books[n].id=0;
printf("該書已注銷.\n");
return_confirm();
}
void main(void)
{
menu: page_title("操作選單");
printf("請用數字鍵選擇操作\n\n");
printf("1 借閱圖書\n2 歸還圖書\n\n");
printf("3 注冊新書\n4 注銷舊書\n\n");
printf("\n0 退出\n");
switch(getch())
{
case '1' : book_out();break;
case '2' : book_in();break;
case '3' : book_add();break;
case '4' : book_del();break;
case '0' : exit(0);
}
goto menu;
}
{
int n;
page_title("標題");
if((n=search_book())!=-1) books[n].id=0;
printf("列出未還的書?.\n");
return_confirm();
}
void main(void)
{
menu: page_title("書名");
printf("\n序號\n");
printf("1 \n作者\n");
printf("3\n數量\n");
printf("\n0 書號\n");
switch(getch())
{
case '1' : book_out();break;
case '2' : book_in();break;
case '3' : book_add();break;
case '4' : book_del();break;
case '0' : exit(0);
}
goto menu;
}
⑸ 圖書館管理系統代碼 要正確無誤的C程序代碼
在郵箱 來分