C++實現一個簡單圖書借閱流程
總共實現了myDate類,book類,student類,圖書借閱記錄record類
//
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <ctime>
#include <cstdio>
using namespace std;
//主要就基於C庫封裝了一個獲取時間戳的資料成員和相關方法
class myDate
{
time_t _time;
public:
myDate()
{
time (&this ->_time);
cout<<"get time sucessful"<<endl;
}
friend ostream& operator<<(ostream &out,myDate &d);
};
ostream& operator <<(ostream &out,myDate &d)
{
out<<ctime(&(d._time));
return out;
}
//book類主要封裝一個圖書資訊(書名和數量)
class book
{
//string bookid;
string bookname;
int bookcount;
public:
explicit book(string bname,int bcount):
bookname(bname),bookcount(bcount)
{
}
int insertBook()
{
if(bookcount < 0)
return -1;
bookcount += 1;
return 0;
}
int removeBook()
{
if(bookcount <= 0)
return -1;
bookcount -= 1;
return 0;
}
string& getname()
{
return bookname;
}
void print()
{
cout<<"bname:"<<bookname<<",count:"<<bookcount<<endl;
}
};
//record類封裝一條借閱記錄<who,do,bookname>
class recordPer
{
bool f ;//true brrow -- false return
string sname;
string bname;
myDate time;
public:
recordPer(string &bn,string &sn,bool &b):
bname(bn),sname(sn),time(),f(b)
{
}
friend ostream& operator <<(ostream &out,recordPer &r);
};
ostream& operator <<(ostream &out,recordPer &r)
{
if(r.f == true)
out<<"at "<<r.time<<" "<<r.sname<<"--brrow->"<<r.bname;
else
out<<"at "<<r.time<<" "<<r.sname<<"--return->"<<r.bname;
return out;
}
//其實這個類封裝的顯得多餘,第二版本後面再優化
class bookRecord
{
public:
vector<recordPer > _record;
void print()
{
for(unsigned int i=0;i< _record.size();++i)
{
cout<<_record.at(i)<<endl;
}
}
int insert(recordPer r)
{
_record.push_back(r);
return 0;
}
};
//下面就是整個流程的靈魂,人,借書還書都是人的動作
class student
{
//string stuid;
string stuname;
bookRecord __record;
list<string> needreturn;
public:
student(string &s):stuname(s){}
int borrowBook(string &bookname,vector<book> &v);
int returnBook(string &bookname,vector<book> &v);
string& getname()
{
return stuname;
}
void printAll()
{
this->__record.print();
}
};
int student::borrowBook(string &bookname,vector<book> &v)
{
bool b = true;
for(int i=0;i<v.size();++i)
{
if(v.at(i).getname() == bookname)
{
if(v.at(i).removeBook() != 0)
return -1;
this->__record.insert( recordPer(bookname,this->stuname,b));
this->needreturn.push_back(bookname);
return 0;
}
}
return 1;
}
int student::returnBook(string &bookname,vector<book> &v)
{
bool b = false;
for(int i=0;i<v.size();++i)
{
if(v.at(i).getname() == bookname)
{
this->needreturn.remove(bookname);
if(v.at(i).insertBook() != 0)
return -1;
this->__record.insert( recordPer(bookname,this->stuname,b));
//this->needreturn.remove(bookname);
return 0;
}
}
return 1;
}
int main()
{
vector<book> _book;
vector<student> _stu;
string name;
string id;
int count;
int code;
while(1)
{
cout<<"----------BOOK-------------"<<endl;
cout<<"1 new book"<<endl;
cout<<"2 new user"<<endl;
cout<<"3 borrow book"<<endl;
cout<<"4 return book"<<endl;
cout<<"5 find all record of some student"<<endl;
cout<<"6 find all book"<<endl;
cout<<"----------BOOK-------------"<<endl;
cout<<"input op:";
cin>>code;
//code = getchar();
//code -= 48;
if(code <1 || code >6)
{
cout<<"input error\n";
continue;
}
if(code == 1)
{
cout<<"input book infomation:name(string) count(int)"<<endl;
cin>>name>>count;
_book.push_back(book(name,count));
}
else if(code == 2)
{
cout<<"input student information:name(string)\n";
cin>>name;
_stu.push_back(student(name));
}
else if(code == 3)//brrow
{
cout<<"input student name && book name :";
int flag = 0;
cin>>name>>id;
int i;
for( i=0;i<_stu.size();++i)
{
if(_stu[i].getname() == name)
{
flag = 1;
break;
}
}
if(flag != 1)
cout<<"student "<<name<<"not found\n";
if(0 == _stu.at(i).borrowBook(id,_book))
cout<<"brrowbook sucessful \n";
else
cout<<"brrowbook failed \n";
}
else if(code == 4)//return
{
cout<<"input student name && book name :";
int flag = 0;
cin>>name>>id;
int i;
for( i=0;i<_stu.size();++i)
{
if(_stu[i].getname() == name)
{
flag = 1;
break;
}
}
if(flag != 1)
cout<<"student "<<name<<"not found\n";
if(0 == _stu.at(i).returnBook(id,_book))
cout<<"returnbook sucessful \n";
else
cout<<"returnbook failed \n";
}
else if(code == 5)
{
cout<<"input student name:";
int flag = 0;
cin>>name;
int i;
for( i=0;i<_stu.size();++i)
{
if(_stu[i].getname() == name)
{
_stu.at(i).printAll();
flag = 1;
break;
}
}
if(flag == 0)
cout<<"student "<<name<<"not found"<<endl;
}
else if(code == 6)
{
for(int i=0;i<_book.size();++i)
{
_book.at(i).print();
}
}
}
return 0;
}
不合理的地方在後期需要改進的地方:
取消掉record類,需要載入上bookid和studentid,或者增加繼承的結構
相關推薦
C++實現一個簡單圖書借閱流程
總共實現了myDate類,book類,student類,圖書借閱記錄record類 // #include <iostream> #include <string> #include <vector> #include &l
用 C# 實現一個簡單的 Rest Service 供外部調用
message [] operation rem adk www span method title 用 C# 實現一個簡單的 Restful Service 供外部調用,大體總結為4點: The service contract (the methods it o
c++實現一個簡單的字串類
// string.cpp : 定義控制檯應用程式的入口點。 //c++語言基礎,實現一個簡單的string類 #include <iostream> #include <string> #include "stdlib.h" #include
C++實現一個簡單的雙執行緒MVC框架
花了一上午,初步實現一個簡單的MVC框架,原理如下: 兩個執行緒一個控制執行緒(處理業務邏輯的工作執行緒),一個檢視使用執行緒(往往應該就是UI執行緒)。所有檢視操作應該在UI執行緒中操作,業務邏輯和對model的修改在工作執行緒中做。 實現控制器類Prin
C#實現一個簡單的定時任務
場景一:微博、微信、郵件等需要定時傳送時,怎麼辦? 場景二:朋友明天過生日,我今天先把郵件寫了,定時明天幾點幾分幾秒傳送,如何做? 基於以上問題,我們自己實現一個小小的定時器,現在分解一下,可以使用在WinForm、Web等程式中 首先建一個類TaskTime,繼承於.ne
c++ — 實現一個簡單的string類
string類的實現 C++使用起來非常方便,原因的它裡面包含的類,這種面向物件的思想讓我們程式設計變得異常方便。今天我們來實現一個string類,這
C++11實現一個簡單的線程池
start art AI fun con var func iostream any 為了不讓手生,邊復習邊手擼了一個線程池,代碼量比較少,如下,用了一些C++11的實現,語言標準嘛,就是跨平臺的: thread_poo.h #ifndef _THREAD_POOL_ #
自己寫一個C#數據結構:用List<T>實現一個簡單的Stack
count 實現簡單 ole exceptio tac on() rem linq -- 在C#中利用List<T>實現一個簡單的Stack 需要實現的功能:壓棧、彈棧、查看棧頂元素、查看元素個數、查看Socket是否為空,判斷元素是否在Socket中、清空So
用c語言實現一個簡單的通訊錄
通訊錄的c語言實現原始碼 簡單通訊錄的實現還是包括三個原始檔,test.c(實現通訊錄主邏輯),txl.c(實現用到的各個函式),txl.h(存放txl中用到的各種標頭檔案與宣告)。 txl.h #ifndef __TXL_H__//**txl.h** #defi
**c++基於tcp協議的socket程式設計實現一個簡單伺服器**
c++基於tcp協議的socket程式設計實現一個簡單伺服器 基於tcp的通訊,可以利益socket套接字實現。通訊,顧名思義需要伺服器和客戶端兩者進行資訊互動。 通過流程圖我們可以看到程式設計實現伺服器和客戶端的步驟大致相同,而伺服器則更為複雜一些。本文之給出了一個簡單的伺服器程式設計和
【 C 】轉移表(理論與實踐)(實現一個簡單的計算器)
首先借用《C 與指標》上對於轉移表的解釋,然後我們自己程式設計序操作下: 轉移表最好用個例子來解釋。下面的程式碼段取自一個程式,它用於實現一個袖珍式計算器。程式的其他部分已經讀入兩個數(op1和op2
實現一個簡單的Web伺服器(C語言)
Web伺服器 github地址 該專案的第二部分是在第一部分的基礎上繼續完善Web伺服器。 第二部分主要是完成兩個功能:記錄日誌和HTTP響應。 記錄日誌 該部分比較簡單,只要簡單地將一些伺服器的資訊新增到指定日誌檔案即可, 也不需要完成很複雜的功能,有了日誌功
c++11實現一個簡單的lexical_cast
boost中有一個lexical_cast可以用統一的方式來做基本型別之間的轉換,比如字串到數字,數字到字串,bool和字串及數字之間的相互轉換。boost::lexical_cast的用法比較簡單: #include <boost/lexical_cast.
C語言-------實現一個簡單的單向連結串列
編寫一個連結串列程式,在程式中實現簡單的功能#include <stdio.h> #include <stdlib.h> struct node{ int num; char name[20]; struct node* nex
C語言實現一個簡單的伺服器
C/S結構流程圖 服務端 socket函式 為了執行網路I/O,一個程序必須做的第一件事情就是建立一個socket函式 /* family 表示協議族 AF_INET(IPv4協議)、AF_INET6(IPv6協議)、AF_L
一個C++實現的簡單區塊鏈
原始碼 https://github.com/lzj112/C-Plus-Plus/tree/master/%E8%BD%AF%E4%BB%B6%E6%9D%AF/blockchain/test1 block.h定義區塊資料結構 #ifndef
自己動手實現一個簡單c編譯器
這學期的編譯課程設計需要做一個類c編譯器,準確的說是完善上學期做的大實驗。 上學期的實驗中,使用antlr完成的編譯器識別的語法很有限,基本上是個計算器的語法,於是這次決定弄語法一個更加完整。 語法支援: 宣告,賦值,函式,if-else,while,for。 首先是詞法分
C++中實現一個簡單的單向連結串列
最近在linux了一個簡單的單向連結串列,直接上程式碼 連結串列標頭檔案 list.h #ifndef _LIST_H_ #define _LIST_H_ typedef int T; template<typename T> class List { pr
用連結串列實現一個簡單的學生操作管理系統C語言版
#include <stdio.h> #include <math.h> #include <string.h> #include <malloc.h> #include <stdlib.h> #def
用c#自己實現一個簡單的JSON解析器
## 一、JSON格式介紹 * JSON(JavaScript Object Notation) 是一種輕量級的資料交換格式。相對於另一種資料交換格式 XML,JSON 有著很多優點。例如易讀性更好,佔用空間更少等。在 web 應用開發領域內,得益於 JavaScript 對 JSON 提供的良好支援,JSO