1 引言
任務背景:圖書管理系統是當前社會每個人不可缺少的信息系統,利用C語言和Dve c++製作圖書管理系統,從根本上改變圖書信息多、繁瑣、易丟失,從而提高信息管理和存儲效率。在對系統進行分析和設計的基礎上,創建各種項目組件,若干個功能模塊連接成圖書管理系統。
編寫目的:實現書籍的添加,刪除,修改,按編號查詢,及讀者從查詢系統。
任務概述:
(1)借閱資料管理(對相關資料進行添加,刪除,修改,查詢等操作)
(2)借閱管理(包括借出操作,還書操作)
(3)讀者管理(讀者等級:可分爲教師,學生。並定義每類讀者可借書數量)
(4)統計分析(當前借閱和相關資料狀態。統計分析借閱排行榜,資料狀態統計,借閱統計,顯示所有至當日到期未還書信息等功能分析)…
2 功能及操作介紹
3 系統設計分析
通過文件系統操作對所有圖書的信息的操作(添加,刪除,修改,查詢)在內存上進行,程序再次運行時能夠保留之前的所有操作,
系統共分爲七塊函數模塊分別爲圖書的添加、刪除、修改、查詢、顯示、讀者系統和退出。
代碼
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
FILE *fp; //創建文件指針
struct wmy {
char num[88]; //編號
char name[88]; //書名
char writer[88]; //作者
char price[88]; //價格
char lable[88]; //類別
char time[88]; //出版時間
char company[88]; //出版單位;
char kind ; //標記狀態是否被借出 1表示可以借閱 2表示已經借閱未歸還 0表示不存在這本書
} book;
int cnt=0;
int main() {
void reader(); // 讀者管理(讀者等級:可分爲教師、學生,並定義每類讀者可借書數量和相關的借閱時間等信息)
void add(); //添加圖書
void del(); //刪除圖書
char memu(); //菜單
void change(); //修改圖書信息
void search(); //查詢圖書信息
void output(); //顯示所有圖書信息
int zhanghao,mima; //帳號密碼均爲666
cout<<"請輸入登錄帳號:"<<endl;
cin>>zhanghao;
cout<<"密碼:"<<endl;
cin>>mima;
if(zhanghao==666&&mima==666) {
while(1) {
switch (memu()) {
case '1':
add();
break;
case '2':
output();
break;
case '3':
del();
break;
case '4':
change();
break;
case '5':
search();
break;
case '6':
reader();
break;
case '7':
return 0;
break;
}
}
} else {
cout<<"請重新輸入:"<<endl;
main();
}
return 0;
}
char memu() {
char x;
cout<<"歡迎使用圖書管理系統"<<endl<<"*********************************************"<<endl;
cout<<"1.輸入圖書信息"<<endl;
cout<<"2.顯示所有圖書信息"<<endl;
cout<<"3.刪除圖書信息" <<endl;
cout<<"4.修改圖書信息" <<endl;
cout<<"5.查詢圖書信息" <<endl;
cout<<"6.讀者管理" <<endl;
cout<<"7.退出圖書管理系統" <<endl;
cout<<"*********************************************"<<endl<<"請選擇菜單命令:"<<endl;
cin>>x;
return x; //作爲返回值返回到主函數選擇功能
}
void add() {
struct wmy ac[666]; //創建另一個結構體 作爲臨時儲存源文件信息,
int i=0;
FILE *xi=fopen("book.txt","rb+");
while(fread(&ac[i],sizeof(book),1,xi)==1)
i++; //每次一字節讀book源文件的信息
int x=i; //確定book裏有幾本書,從i開始繼續添加
fclose(xi);
cout<<"請輸入圖書編號"<<endl;
cin>>book.num;
cout<<"請輸入書名"<<endl;
cin>>book.name;
cout<<"請輸入作者"<<endl;
cin>>book.writer;
cout<<"請輸入出版社"<<endl;
cin>>book.company;
cout<<"請輸入出版時間"<<endl;
cin>>book.time;
cout<<"請輸入價格"<<endl;
cin>>book.price;
cout<<"請輸入分類"<<endl;
cin>>book.lable;
book.kind=1;
ac[x++]=book; //添加新的圖書
fp=fopen("book.txt","wb");
for(int j=0; j<x; j++)
fwrite(&ac[j],sizeof(book),1,fp ); //每次一個字節將新書 的信息寫入book
fclose(fp); //關閉文件,保存數據
}
void del() {
FILE *fp1;
FILE *fp2;
if((fp1=fopen("book.txt","rb"))== NULL) { // 以只讀的方式 打開,如果指針返回爲空,則無法打開
cout<<endl<<"不能打開book.txt文件"<<endl;
getchar();
exit(1);//跳出語句
}
if((fp2=fopen("temp.txt","wb"))==NULL) { //建立一個臨時文件temp,以只寫的方式打開當temp被佔用時無法打開 或指針返回爲空時
cout<<endl<<"不能打開book.txt文件"<<endl;
getchar();
exit(1);
}
char x[88];
cout<<"請輸入要刪除的數目編號:"<<endl;
cin>>x;
int y=0;
while(fread(&book,sizeof(book),1,fp1)==1) {
if(strcmp(book.num,x)==0) {
y=1;
} else {
fwrite(&book,sizeof(book),1,fp2);
}
}
fclose(fp1);
fclose(fp2);
if(y) {
cout<<"刪除成功"<<endl;
remove("book.txt");
rename("temp.txt","book.txt");
} else
cout<<"未找到圖書"<<endl;
getchar();
getchar();
}
void output() {
fp=fopen("book.txt","rb");
cout<<"編號 書名 作者 分類 出版單位 時間 價格"<<endl;
while(fread(&book,sizeof(book),1,fp)==1) {
printf("%s%10s%8s",book.num,book.name,book.writer);
printf("%10s%8s%12s%10s\n",book.lable,book.company,book.time,book.price );
}
fclose(fp);
getchar();
getchar();
}
void change() {
FILE *fp1,*fp2;
int y=1;
char temp[66],tempname[66],tempwriter[66],templable[66],tempprice[66],tempcompany[66],temptime[66];
cout<<"請輸入要修改的編號:"<<endl;
cin>>temp;
if((fp1=fopen("book.txt","rb"))== NULL) { // 以只讀的方式 打開,如果指針返回爲空,則無法打開
cout<<endl<<"不能打開book.txt文件"<<endl;
getchar();
exit(1);//跳出語句
}
if((fp2=fopen("temp.txt","wb"))==NULL) { //建立一個臨時文件temp,以只寫的方式打開當temp被佔用時無法打開 或指針返回爲空時
cout<<endl<<"不能打開book.txt文件"<<endl;
getchar();
exit(1);
}
while(fread(&book,sizeof(book),1,fp1)==1) {
if(strcmp(book.num,temp)==0) {
y=0;
cout<<"請輸入新的圖書名:"<<endl;
cin>>tempname;
strcpy(book.name,tempname);
cout<<"請輸入新的作者:"<<endl;
cin>>tempwriter;
strcpy(book.writer,tempwriter);
cout<<"請輸入新的分類:"<<endl;
cin>>templable;
strcpy(book.lable,templable);
cout<<"請輸入新的出版社:"<<endl;
cin>>tempcompany;
strcpy(book.company,tempcompany);
cout<<"請輸入新的出版時間:"<<endl;
cin>>temptime;
strcpy(book.time,temptime);
cout<<"請輸入新的價格:"<<endl;
cin>>tempprice;
strcpy(book.price,tempprice);
}
fwrite(&book,sizeof(book),1,fp2);
}
fclose(fp1);
fclose(fp2);
if(!y) {
cout<<"修改成功"<<endl;
remove("book.txt");
rename("temp.txt","book.txt");
}
getchar();
getchar();
}
void reader() {
int x;
cout<<"請輸入你的身份:(1學生/2老師)"<<endl;
cin>>x;
if(x==1)
cout<<"最多借閱圖書數量:4本"<<endl<<"歸還期限:一個月"<<endl;
else if(x==2)
cout<<"最多借閱圖書數量:8本"<<endl<<"歸還期限:兩個月"<<endl;
else
cout<<"輸入錯誤"<<endl;
getchar();
getchar();
}
void search() {
char x[88];
cout<<"請輸入要查詢的數目編號:"<<endl;
int y=0;
while(fread(&book,sizeof(book),1,fp)) {
if(strcmp(x,book.num)==0) {
y=1;
cout<<"編號 書名 作者 分類 出版單位 時間 價格"<<endl;
printf("%s%10s%10s ",book.num,book.name,book.writer);
printf("%10s%8s%10s%10s\n ",book.lable,book.company,book.time,book.price );
}
}
if(y==0)
cout<<"未找到書目"<<endl;
getchar();
getchar();
}