這篇文章給大家分享的是有關C++如何實現小型圖書管理系統的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
具體內容如下
因為課程設計的原因,需要實現一個小型圖書管理系統
問題描述:
設計一個系統,對圖書信息進行管理,信息描述:有關該系統基本信息的描述,如:圖書名稱、圖書編號、單價、作者、存在狀態、借書人姓名、性別、學號等。
基本功能:
1、新進圖書基本信息的輸入。
2、圖書基本信息的查詢。
3、對撤消圖書信息的刪除。
4、為借書人辦理注冊。
5、辦理借書手續(非注冊會員不能借書)。
6、辦理還書手續。
7、統計圖書庫存、已借出圖書數量。
需要創建三個文本文件:record.txt book.txt reader.txt
operating.h的頭文件:
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include<sstream>
#include<vector>
#include <iomanip>
using namespace std;
int all_stock = 0;
int out_stock = 0;
int times=0;
void outData(vector<string> res,int n) // n為txt中 每行數據個數
{
for(int i=0;i<res.size();i+=n){
for(int j=0;j<n;j++)
cout<<setw(12)<<res[i+j]<<" ";
cout<<endl;
}
}
void BookEntry()
{
double price;
string bookname,writer;
fstream out;
out.open("book.txt",ios::app);
if(!out)
{
cerr<<"打開文件失敗!"<<endl;
}
time_t tt = time(NULL);//這句返回的只是一個時間cuo
cout<<"請輸入書籍名稱"<<endl;
cin>>bookname;
cout<<"請輸入書籍作者"<<endl;
cin>>writer;
cout<<"請輸入書籍價格"<<endl;
while(! (cin>>price) || price <= 0 )
{
cin.clear();
cin.ignore(100,'\n');
cout<<"請輸入正確的價格"<<endl;
}
out<<tt<<" "<<bookname<<" "<<writer<<" "<<price<<" "<<"0"<<"\n";
out.close();
}
void BookMes()
{
fstream in;
string line;
//用于存放分割后的字符串
vector<string> res;
string temp; //暫存字符串
in.open("book.txt",ios::in);
if(!in)
{
cerr<<"打開文件失敗!"<<endl;
}
all_stock = 0;
while(getline(in,line))
{
all_stock++;
//cout<<line<<endl;
//將字符串讀到input中
stringstream input(line); //將line切割 通過input存入temp,然后存入res中
while(input>>temp)
{
res.push_back(temp);
}
}
// 0 書籍編號 1 書籍名稱 2作者 3價格 4書籍狀態
//輸出res
cout<<endl<<setw(12)<<"書籍編號"<<" "<<setw(12)<<"書籍名稱"<<" "<<setw(12)<<"作者"<<" "<<setw(12)<<"價格"<<" "<<setw(12)<<"在館0,不在1"<<"\n";
outData(res,5);
in.close();
}
void DelBook()
{
string del_book;
string line;
vector<string>res;
string temp;
bool flag=false;
fstream in;
in.open("book.txt",ios::in);
if(!in)
{
cerr<<"打開錯誤文件"<<endl;
}
cout<<"請輸入需要刪除的圖書ID"<<endl;
cin>>del_book;
while(getline(in,line))
{
//cout<<line<<endl;
//將字符串讀到input中
stringstream input(line); //將line切割 通過input存入temp,然后存入res中
times=0;
while(input>>temp)
{
if(del_book == temp && times==0)
{
for(int i=0;i<3;i++) //因為一共五個 第一個temp已經是del_book 所以這里取得是四個
{
input>>temp;
}
input>>temp;
if(temp != "0")
{
cout<<"書籍狀態不對";
in.close();
return ;
}
flag=true;
cout<<"\n找到了喔,應該刪除成功了\n";
continue;
}
res.push_back(temp);
times++;
}
}
//outData(res,5);
in.close();
if(!flag)
{
cout<<"\n錯誤的書籍ID\n";
return ;
}
fstream out;
out.open("book.txt",ios::out);
if(!out)
{
cerr<<"打開文件失敗!"<<endl;
}
for(int j=0;j<res.size();j+=5)
{
line = res[j] + " " + res[j+1] + " " + res[j+2] + " " + res[j+3] + " " + res[j+4] + "\n";
out<<line;
}
out.close();
}
void ReaderEntry()
{
string readername,sex_str;
int sex;
fstream out;
out.open("reader.txt",ios::app);
if(!out)
{
cerr<<"打開文件失敗!"<<endl;
}
time_t readerid = time(NULL);//這句返回的只是一個時間cuo
cout<<"請輸入讀者姓名"<<endl;
cin>>readername;
do
{
cout<<"請輸入讀者性別:0為女,1為男"<<endl;
while(! (cin>>sex) )
{
cin.clear();
cin.ignore(100,'\n');
cout<<"請輸入正確的0或1"<<endl;
}
}while(sex != 0 && sex!=1);
if(sex == 1)
{
sex_str = "男";
}else if (sex == 0){
sex_str = "女";
}else{
out.close();
return ;
}
out<<readerid<<" "<<readername<<" "<<sex_str<<"\n";
out.close();
}
/*讀者信息*/
void ReaderMes()
{
fstream in;
string line;
//用于存放分割后的字符串
vector<string> res;
string temp; //暫存字符串
in.open("reader.txt",ios::in);
if(!in)
{
cerr<<"打開文件失敗!"<<endl;
}
while(getline(in,line))
{
//cout<<line<<endl;
//將字符串讀到input中
stringstream input(line); //將line切割 通過input存入temp,然后存入res中
while(input>>temp)
res.push_back(temp);
}
// 0讀者學號 1讀者姓名 2讀者性別
//輸出res
cout<<endl<<setw(12)<<"讀者編號"<<" "<<setw(12)<<"讀者"<<" "<<setw(12)<<"性別"<<"\n";
outData(res,3);
in.close();
}
/* 借閱書籍 */
void BorrowBook()
{
string book[5];
string readerid;
string readername;
string line;
vector<string>res; //取書籍狀況,并且更新
string temp;
bool flag_book = false; //用于判斷書籍是否存在 讀者是否存在
bool flag_reader = false;
/* 取book的圖書情況,并判斷是否在館*/
fstream in;
in.open("book.txt",ios::in);
if(!in)
{
cerr<<"打開錯誤文件"<<endl;
}
cout<<"請輸入需要借的圖書ID"<<endl;
cin>>book[0];
while(getline(in,line))
{
//cout<<line<<endl;
//將字符串讀到input中
stringstream input(line); //將line切割 通過input存入temp,然后存入res中
times=0;
while(input>>temp)
{
if(book[0] == temp && times ==0)
{
res.push_back(temp);
for(int i=0;i<3;i++) //從書籍名稱開始取,一直取到價錢
{
input>>temp; //讀取了書籍編號,要及時寫入res,以后要寫進文本
book[1+i]=temp;
res.push_back(temp);
}
input>>temp; //取書籍狀態,如果0在館 如果1不在館
if(temp == "0")
{
book[4]="1";
temp="1";
res.push_back(temp);
flag_book=true;
}else{
cout<<"\n書籍不在館\n";
in.close();
return ;
}
continue; //繼續取
}
res.push_back(temp);
times++;
}
}
in.close();
if(!flag_book)
{
cout<<"錯誤的書籍ID"<<endl;
return ;
}
in.open("reader.txt",ios::in);
if(!in)
{
cerr<<"打開錯誤文件"<<endl;
}
cout<<"\n請輸入讀者ID\n";
cin>>readerid;
while(getline(in,line))
{
//cout<<line<<endl;
//將字符串讀到input中
stringstream input(line); //將line切割 通過input存入temp,然后存入res中
times=0;
while(input>>temp)
{
if(readerid == temp && times==0)
{
input>>temp;
readername=temp;
flag_reader=true;
break;
}
times++;
}
}
if(!flag_reader)
{
cout<<"錯誤的讀者ID"<<endl;
in.close();
return ;
}
in.close();
fstream out;
out.open("record.txt",ios::app);
if(!out)
{
cerr<<"打開錯誤文件"<<endl;
}
line = book[0] + " " + book[1] + " " + readername + '\n';
out<<line;
cout<<"\n辦理借書成功\n";
out.close();
out.open("book.txt",ios::out);
if(!out)
{
cerr<<"打開文件失敗!"<<endl;
}
for(int j=0;j<res.size();j+=5)
{
line = res[j] + " " + res[j+1] + " " + res[j+2] + " " + res[j+3] + " " + res[j+4] + "\n";
out<<line;
}
out.close();
}
void BorrowMes()
{
fstream in;
string line;
//用于存放分割后的字符串
vector<string> res;
string temp; //暫存字符串
in.open("record.txt",ios::in);
if(!in)
{
cerr<<"打開文件失敗!"<<endl;
}
out_stock=0;
while(getline(in,line))
{
out_stock++;
//cout<<line<<endl;
//將字符串讀到input中
stringstream input(line); //將line切割 通過input存入temp,然后存入res中
while(input>>temp)
res.push_back(temp);
}
// 0書籍編號 1書籍名稱 2讀者姓名
//輸出res
cout<<endl<<setw(12)<<"書籍編號"<<" "<<setw(12)<<"書籍名稱"<<" "<<setw(12)<<"讀者"<<"\n";
outData(res,3);
in.close();
}
void RtnBook()
{
string rtn_book;
string line;
vector<string>res;
string temp;
bool flag=false;
fstream in;
in.open("record.txt",ios::in); //先打開record 查看是否有借這本書
if(!in)
{
cerr<<"打開錯誤文件"<<endl;
}
cout<<"請輸入需要歸還的書籍ID"<<endl;
cin>> rtn_book;
while(getline(in,line))
{
//cout<<line<<endl;
//將字符串讀到input中
stringstream input(line); //將line切割 通過input存入temp,然后存入res中
times=0;
while(input>>temp)
{
if(rtn_book == temp && times==0) //如果有的話
{
flag=true;
for(int i=0;i<2;i++) //因為一共三個 第一個temp已經是del_book 所以這里取得是兩個
{
input>>temp;// 將刪除的東西不輸出到向量中
}
continue;
}
res.push_back(temp);
times++;
}
}
//outData(res,3);
in.close();
if(!flag)
{
cout<<"該圖書不存在或者沒有被外借"<<endl;
return ;
}
fstream out;
out.open("record.txt",ios::out); //record已經刪除成功
if(!out)
{
cerr<<"打開文件失敗!"<<endl;
}
for(int j=0;j<res.size();j+=3)
{
line = res[j] + " " + res[j+1] + " " + res[j+2] + "\n";
out<<line;
}
out.close();
vector<string>res_book;
in.open("book.txt",ios::in); //開始取 被修改的書籍
if(!in)
{
cerr<<"打開錯誤文件"<<endl;
}
while(getline(in,line))
{
//cout<<line<<endl;
//將字符串讀到input中
stringstream input(line); //將line切割 通過input存入temp,然后存入res中
times=0;
while(input>>temp)
{
if(rtn_book == temp && times==0)
{
res_book.push_back(temp);
for(int i=0;i<3;i++) //因為一共五個 第一個temp已經是rtn_book 所以這里取得是四個
{
input>>temp;
res_book.push_back(temp);
}
input>>temp;//最后一個取得是書籍狀態,需要修改書籍狀態
temp = "0";
res_book.push_back(temp);
continue;
}
res_book.push_back(temp);
times++;
}
}
//outData(res,5);
in.close();
out.open("book.txt",ios::out); //再存入文本中;
if(!out)
{
cerr<<"打開文件失敗!"<<endl;
}
for(int j=0;j<res_book.size();j+=5)
{
line = res_book[j] + " " + res_book[j+1] + " " + res_book[j+2] + " " + res_book[j+3] + " " + res_book[j+4] + "\n";
out<<line;
}
out.close();
cout<<"\n找到了喔,應該還書成功了\n";
}
void CountBook()
{
cout<<"\n圖書館書籍情況";
BookMes();
cout<<"圖書館一共有:"<<all_stock<<" 本書\n\n\n";
cout<<"\n圖書館書籍外借情況";
BorrowMes();
cout<<"圖書館目前外借:"<<out_stock<<" 本書\n\n";
cout<<"\n\n圖書館當前在館書籍還有:"<<all_stock - out_stock<<" 本書\n";
}main.cpp的主函數
#include "operating.h"
int main()
{
int order;
do
{
order = -1;
cout<<"\n";
cout<<"----------------------------------------------------------\n";
cout<<"| 1. 圖書信息錄入 2. 圖書信息查詢 3. 圖書信息刪除 |\n";
cout<<"| 4. 讀者辦理注冊 5. 讀者信息查詢 6. 辦理借書手續 |\n";
cout<<"| 7. 辦理還書手續 8 已借出圖書 9.統計圖書庫存 |\n";
cout<<"| 按 \"0\"退出 |\n";
cout<<"----------------------------------------------------------\n";
cout<<" 請輸入相應序號進行相應操作:";
cin>>order;
cin.clear();//清除緩沖區中后面的字符
cin.ignore(100,'\n');
switch(order)
{
case 1:
BookEntry();
break;
case 2:
BookMes();
break;
case 3:
DelBook();
break;
case 4:
ReaderEntry();
break;
case 5:
ReaderMes();
break;
case 6:
BorrowBook();
break;
case 7:
RtnBook();
break;
case 8:
BorrowMes();
break;
case 9:
CountBook();
break;
case 0:
break;
default:
cout<<"錯誤的命令行"<<endl;
break;
}
}while(order != 0);
system("pause");
return 0;
}感謝各位的閱讀!關于“C++如何實現小型圖書管理系統”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。