⑴ 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>
#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;
}
希望能解決您的問題。
⑸ 求圖書館管理系統的源代碼,幫忙啊
有很多的。如下:
凌鵬圖書館管理系統 7.7.4
http://download.zol.com.cn/detail/16/154117.shtml
立天圖書館管理系統 8.2.3破解版
http://www.anxz.com/down/2162.html
笑天圖書館管理系統
http://www.yescode.cn/soft/373/386/593/Soft_46003.html
北極星圖書館管理系統 V3.0
http://www.xdowns.com/soft/17/61/2006/Soft_23490.html
⑹ 急求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();
}
changemploy()
{
FILE *fp,*fp2;
char employname[10],choice;
int i;
fp=fopen("employ.txt","r");
fp2=fopen("bookl.txt","w");
printf("請你輸入要修改的職工的名字\n");
scanf("%s",employname);
for(i=0;fread(&employ[i],sizeof(struct employ),1,fp)!=0;i++)
{
if(strcmp(employ[i].employname,employname)==0)
{
printf("你所要修改的職工的資料如下,請選擇你要修改的內容\n");
printf("<職工號:%d職工名:%s 年齡:%d 性別:%s 學歷:%s 工資:%d>\n",employ[i].employnum,employ[i].employname,employ[i].employage,employ[i].employsex,employ[i].employleve,employ[i].employtage);
printf("2:修改職工名\n");
printf("3:修改職工年齡\n");
printf("4:修改職工工資\n");
printf("5:修改職工學歷\n");
printf("請輸入1-5:");
scanf("%s",&choice);
switch(choice)
{
case '1':
{
printf("請輸入新的職工號\n");
scanf("%d",&employ[i].employnum);
fwrite(&employ[i],sizeof(struct employ),1,fp2);
}break;
case '2':
{
printf("請輸入新的職工姓名\n");
scanf("%s",employ[i].employname);
fwrite(&employ[i],sizeof(struct employ),1,fp2);
}break;
case '3':
{
printf("請輸入新的年齡\n");
scanf("%d",&employ[i].employage);
fwrite(&employ[i],sizeof(struct employ),1,fp2);
}break;
case '4':
{
printf("請輸入新的職工工資\n");
scanf("%d",&employ[i].employtage);
fwrite(&employ[i],sizeof(struct employ),1,fp2);
}break;
case '5':
{
printf("請輸入新的職工學歷\n");
scanf("%s",employ[i].employleve);
fwrite(&employ[i],sizeof(struct employ),1,fp2);
}
default:printf("沒有這樣的操作");break;
}
continue;
}
fwrite(&employ[i],sizeof(struct employ),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("employ.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&employ[i],sizeof(struct employ),1,fp2)!=0;i++)
{
fwrite(&employ[i],sizeof(struct employ),1,fp);
}
fclose(fp);
fclose(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意鍵返回\n");
getchar();
return;
}
delemploy()
{
FILE *fp,*fp2;
int i;
char employname[10],choice;
fp=fopen("employ.txt","r");
fp2=fopen("bookl.txt","w");
printf("請輸入你要刪除的職工名\n");
printf("如果你輸入的職工存在,系統自動刪除該信息!如果不存在,系統不做任何改動\n");
scanf("%s",employname);
for(i=0;fread(&employ[i],sizeof(struct employ),1,fp)!=0;i++)
{
if(strcmp(employname,employ[i].employname)!=0)
{
fwrite(&employ[i],sizeof(struct employ),1,fp2);
}
}
fclose(fp);
fclose(fp2);
printf("是否真的要刪除該職工信息?刪除後的所有信息將無法恢復《Y/N》\n");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
fp=fopen("employ.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&employ[i],sizeof(struct employ),1,fp2)!=0;i++)
{
fwrite(&employ[i],sizeof(struct employ),1,fp);
}
fclose(fp);
fclose(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意鍵返回\n");
getch();
return TRUE;
}
else
{
printf("按任意鍵返回\n");
getch();
return FALSE;
}
}
employcount()
{
FILE *fp;
int i,n=0;
fp=fopen("employ.txt","r");
for(i=0;fread(&employ[i],sizeof(struct employ),1,fp)!=0;i++)
{
printf("第%d職工的信息如下:\n<職工號:%d 職工名:%s 年齡:%d 性別:%s 學歷:%s 工資:%d>\n",n+1,employ[i].employnum,employ[i].employname,employ[i].employage,employ[i].employsex,employ[i].employleve,employ[i].employtage);
n++;
}
fclose(fp);
printf("目前共有%d個職工\n",n);
printf("按任意鍵返回\n");
}
addemploy()
{
FILE *fp;
char choice='y';
int i=1;
fp=fopen("employ.txt","a+");
while(choice=='y'||choice=='Y')
{
printf("請你輸入職工號碼\n");
scanf("%d",&employ[i].employnum);
printf("請你輸入職工名\n");
scanf("%s",employ[i].employname);
printf("請輸入職工年齡\n");
scanf("%d",&employ[i].employage);
printf("請你輸入性別\n");
scanf("%s",employ[i].employsex);
printf("請你輸入職工的學歷水平\n");
scanf("%s",employ[i].employleve);
printf("請輸入職工的工資\n");
scanf("%d",&employ[i].employtage);
fwrite(&employ[i],sizeof(struct employ),1,fp);
printf("是否要輸入下個職工信息?\n");
scanf("%s",&choice);
}
printf("按任意鍵返回\n");
fclose(fp);
}
addbook()
{
FILE *fp;
int i=0;
char choice='y';
fp=fopen("book.txt","a+");
while(choice=='y'||choice=='Y')
{
printf("請你輸入第%d本書的序號\n",i+1);
scanf("%d",&book[i].booknum);
printf("請你輸入書名\n");
scanf("%s",book[i].bookname);
printf("請你輸入書的作者\n");
scanf("%s",book[i].bookcreat);
printf("請設為1或0,1代表書還沒人借,0表示書已經借出,設其他值,程序運行時無法得出正常結果\n");
printf("請你設定書的狀態\n");
scanf("%d",&book[i].turefalse);
fwrite(&book[i],sizeof(struct book),1,fp);
printf("是否要輸入下本書\n");
scanf("%s",&choice);
}
fclose(fp);
}
bookcount()
{
FILE *fp;
int i,n=0;
fp=fopen("book.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(book[i].booknum!=0&&strlen(book[i].bookname)!=0&&strlen(book[i].bookcreat)!=0)
{
printf("第%d本書<序號:%d 書名:%s 作者:%s 狀態:%d>\n",i+1,book[i].booknum,book[i].bookname,book[i].bookcreat,book[i].turefalse);
n=n+1;
}
}
fclose(fp);
printf("目前共有%d本書\n",n);
printf("按任意鍵\n");
}
delbook()
{
FILE *fp,*fp2;
int i;
char bookname[10],choice;
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
printf("請輸入你要刪除的書名\n");
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)
{
fwrite(&book[i],sizeof(struct book),1,fp2);
}
}
fclose(fp);
fclose(fp2);
printf("是否真的要刪除該書籍?刪除後該書籍的所有信息將無法恢復《Y/N》\n");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
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);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意鍵返回\n");
getch();
return;
}
else
{
printf("按任意鍵返回\n");
getch();
return;
}
}
changebook()
{
FILE *fp,*fp2;
char bookname[10],choice;
int i;
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
printf("請你輸入要修改的書籍的書字\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(book[i].bookname,bookname)==0)
{
printf("你所要修改的書的資料如下,請選擇你要修改的內容\n");
printf("序號:〈%d〉 書名:〈%s〉 作者:〈%s〉 \n",book[i].booknum,book[i].bookname,book[i].bookcreat);
printf("1:修改書的序號\n");
printf("2:修改書名\n");
printf("3:修改作者\n");
printf("請輸入1-3:");
scanf("%s",&choice);
switch(choice)
{
case '1':
{
printf("請輸入新的序號\n");
scanf("%d",&book[i].booknum);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
case '2':
{
printf("請輸入新的書名\n");
scanf("%s",book[i].bookname);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
case '3':
{
printf("請輸入新的作者\n");
scanf("%s",book[i].bookcreat);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
defaut:printf("沒有這樣的操作");break;
}
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(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意鍵返回\n");
getchar();
return;
}
main()
{
char ch1,ch2,ch3,ch4,ch5;
do
{
printf("\t\t請你選擇操作類型:\n");
printf(" 1:<查閱圖書>\n");
printf(" 2:<借閱圖書>\n");
printf(" 3:<管理系統>\n");
printf(" 4:<還書>\n");
printf(" 0:<退出>\n");
printf("請輸入0--4\n");
scanf("%s",&ch1);
switch(ch1)
{
case '1':chabook();break;
case '2':lendbook();;break;
case '3':{
do
{
printf("---------------歡迎進入管理系統!--------------\n");
printf(" 1:<增加圖書>\n");
printf(" 2:<刪除圖書>\n");
printf(" 3:<修改圖書資料>\n");
printf(" 4:<書籍統計>\n");
printf(" 5:<職工管理系統>\n");
printf(" 6:<學生管理系統>\n");
printf(" 0:<返回>\n");
printf("請輸入0--6,其他輸入非法!\n");
scanf("%s",&ch2);
switch(ch2)
{
case '1':addbook();
break;
case '2':delbook();break;
case '3':changebook();break;
case '4':bookcount();getch();break;
case '5':{do
{
printf("---------------歡迎進入職工管理系統!--------------\n");
printf(" 1:<增加員工>\n");
printf(" 2:<刪除員工>\n");
printf(" 3:<修改員工資料>\n");
printf(" 4:<員工統計>\n");
printf(" 0:<返回>\n");
printf("請輸入0--4,其他輸入非法!\n");
scanf("%s",&ch3);
switch(ch3)
{
case '1':addemploy();getch();break;
case '2':delemploy();break;
case '3':changemploy();break;
case '4':employcount();getch();break;
case '0':break;
default:printf("無此操作\n");getch();break;
}
}while(ch3!='0');}break;
case '6':{
do
{
printf("---------------歡迎進入學生管理系統!--------------\n");
printf(" 1:<申請新卡>\n");
printf(" 2:<刪除卡號>\n");
printf(" 3:<借書統計>\n");
printf(" 4:<卡號統計>\n");
printf(" 0:<返回>\n");
printf("請輸入0--4,其他輸入非法!\n");
scanf("%s",&ch4);
switch(ch4)
{
case '1':addcar();break;
case '2':delcar();break;
case '3':lendcount();break;
case '4':carcount();break;
case '0':break;
default:printf("無此操作\n");getch();break;
}
}while(ch4!='0');}break;
case '0':break;
default:printf("無此操作\n");getch();break;
}
}while(ch2!='0');}break;
case '4':huanbook();break;
case '0':break;
default:printf("無此操作\n");getch();break;
}
}while(ch1!='0');
}
⑺ 求圖書館管理系統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();
}