C++도서 관리 시스템 구현

한가 할 때 C++로 도서 관리 시스템 을 만 들 었 는데 주로 책 을 빌 리 거나 책 을 돌려 주 거나 도서 관리,사용자 관리 등 기능 이 있 고 주로 사용 하 는 기술 은 용기 와 문서,그리고 이런 포장 이 있다.

#include <iostream>
#include <list>
#include <algorithm>
#include <string.h>
#include <fstream>
#include <stdlib.h>
 
using namespace std;
class Mybook;
class Book;
 
class Book{
public:
 int id;              //  
 char book_name[20];        //  
 int state;            //    
 char place[20];          //      
 char stu_number[20];       //  
 char stu_name[20];        //    
public:
 Book();
 friend class Mybook;
};
 
class User{
public:
 char stu_number[20];        //  
 char stu_name[20];         //    
public:
 User()
 {
  strcpy(stu_number,"000");
  strcpy(stu_name,"0");
 }
 friend class Mybook;
};
 
class Mybook{
private:
 list<Book> link_book;       //      
 list<User> link_user;       //      
public:
 int main_menu();          //   
 void getmenu();          //     
 int menu();            //      
 void getchoice();         //    
 void add_book();          //    
 void display_book();        //      
 void del_book();          //      
 void search_book();        //      
 void update_book();        //      
 void borrow_book();        //  
 void return_book();        //  
 int menu_user();         //      
 void add_user();          //    
 void del_user();          //    
 void display_user();        //    
 void update_user();        //    
 void look_borrow();        //         
 void get_user();          //    
 void recv_file();         //         
 void read_file();         //         
 void recv_user();         //          
 void read_user();         //          
};
 
Book::Book()
{
 state = 0;
 strcpy(stu_number,"000");
 strcpy(stu_name,"0");
}
//         
void Mybook::recv_file()
{
 ofstream outfile("library.txt",ios::out);
 if(!outfile)
 {
 cout<<"      "<<endl;
 return ;
 }
 sleep(1);
 list<Book>::iterator p = link_book.begin();
 while(p != link_book.end())
 {
 outfile<<p->id<<endl;
 outfile<<p->book_name<<endl;
 outfile<<p->state<<endl;
 outfile<<p->place<<endl;
 outfile<<p->stu_number<<endl;
 outfile<<p->stu_name<<endl;
 p++;
 }
 cout<<"    !"<<endl;
 outfile.close();
}
 
void Mybook::read_file()
{
 ifstream infile("library.txt",ios::in);
 Book new_book;
 if(!infile)
 {
 cout<<"read fail"<<endl;
 return ;
 }
 char temp[20];
 while(!infile.eof())
 {
 infile.getline(temp,'
'); new_book.id = atoi(temp); memset(temp,0,20); infile.getline(new_book.book_name,'
'); infile.getline(temp,'
'); new_book.state = atoi(temp); memset(temp,0,20); infile.getline(new_book.place,'
'); infile.getline(new_book.stu_number,'
'); infile.getline(new_book.stu_name,'
'); if(infile.eof()) { break; } link_book.push_back(new_book); } infile.close(); } // void Mybook::recv_user() { ofstream outfile("user.txt",ios::out); if(!outfile) { cout<<" "<<endl; return ; } sleep(1); list<User>::iterator p = link_user.begin(); while(p != link_user.end()) { outfile<<p->stu_number<<endl; outfile<<p->stu_name<<endl; p++; } cout<<" !"<<endl; outfile.close(); } // void Mybook::read_user() { ifstream infile("user.txt",ios::in); if(!infile) { cout<<" !"<<endl; return ; } User new_user; while(!infile.eof()) { infile.getline(new_user.stu_number,'
'); infile.getline(new_user.stu_name,'
'); if(infile.eof()) { break; } link_user.push_back(new_user); } infile.close(); } // int Mybook::main_menu() { int choice = 0; cout<<"\t\t\t\t===================================================="<<endl; cout<<"\t\t\t\t* *"<<endl; cout<<"\t\t\t\t*--------------------------------------------------*"<<endl; cout<<"\t\t\t\t* 1、 *"<<endl; cout<<"\t\t\t\t* 2、 *"<<endl; cout<<"\t\t\t\t* 3、 *"<<endl; cout<<"\t\t\t\t* 4、 *"<<endl; cout<<"\t\t\t\t* 0、 *"<<endl; cout<<"\t\t\t\t*__________________________________________________*"<<endl; cout<<"\t\t\t\t\t :"; cin>>choice; while(!(choice >= 0 && choice <= 4)) { cout<<" , :"; cin>>choice; } return choice; } // void Mybook::getmenu() { int choice = 0; read_user(); read_file(); while(1) { system("clear"); choice = main_menu(); switch(choice) { case 1: { borrow_book(); break; } case 2: { return_book(); break; } case 3: { getchoice(); break; } case 4: { get_user(); break; } case 0: { cout<<" , ....."<<endl; recv_file(); exit(0); } } cout<<endl<<endl<<endl; cout<<" ..."; getchar(); getchar(); } } // void Mybook::borrow_book() { int flag = 0; int flag1 = 0; int flag2 = 0; char id[20]; char name[20]; char book_name[20]; cout<<" :"; cin>>id; list<User>::iterator it = link_user.begin(); while(it != link_user.end()) { if(strcmp(it->stu_number,id) == 0) { strcpy(name,it->stu_name); flag2 = 1; break; } it++; } if(flag2 == 0) { cout<<" ! "<<endl; return ; } cout<<" :"; cin>>book_name; list<Book>::iterator p = link_book.begin(); while(p != link_book.end()) { if(strcmp(p->book_name,book_name) == 0) { cout<<"======================================="<<endl; cout<<" :"<<p->id<<endl; cout<<" :"<<p->book_name<<endl; if(p->state == 0) { cout<<" : !"<<endl; } else { cout<<" : !"<<endl; } cout<<" "<<p->place<<endl; flag1 = 1; } p++; } if(flag1 == 0) { cout<<" , !"; return ; } p = link_book.begin(); cout<<" , :"; int book_id; cin>>book_id; while(p != link_book.end()) { if(strcmp(p->book_name,book_name) == 0 && p->id == book_id && p->state == 0) { strcpy(p->stu_number,id); strcpy(p->stu_name,name); p->state = 1; cout<<" !"<<endl; flag = 1; break; } p++; } if(flag == 0) { cout<<" , !"; } } // void Mybook::return_book() { char stu_id[20]; cout<<" "; cin>>stu_id; int flag = 0; int flag1 = 0; list<Book>::iterator p = link_book.begin(); while(p != link_book.end()) { if(strcmp(p->stu_number,stu_id) == 0) { cout<<"==========================================="<<endl; cout<<" :"<<p->id<<endl; cout<<" :"<<p->book_name<<endl; flag = 1; } p++; } if(flag == 0) { cout<<" "<<endl; return ; } else if(flag == 1) { cout<<" , :"<<endl; } int id; cin>>id; p = link_book.begin(); while(p != link_book.end()) { if(strcmp(p->stu_number,stu_id) == 0&&p->id == id) { strcpy(p->stu_name ,"000"); strcpy(p->stu_number , "0"); p->state = 0; cout<<" !"<<endl; flag1 = 1; break; } p++; } if(flag1 == 0) { cout<<" , !"<<endl; } } // int Mybook::menu_user() { int choice = 0; cout<<"\t\t\t\t=========================================="<<endl; cout<<"\t\t\t\t* *"<<endl; cout<<"\t\t\t\t*----------------------------------------*"<<endl; cout<<"\t\t\t\t* 1、 *"<<endl; cout<<"\t\t\t\t* 2、 *"<<endl; cout<<"\t\t\t\t* 3、 *"<<endl; cout<<"\t\t\t\t* 4、 *"<<endl; cout<<"\t\t\t\t* 5、 *"<<endl; cout<<"\t\t\t\t* 6、 *"<<endl; cout<<"\t\t\t\t*----------------------------------------*"<<endl; cout<<"\t\t\t\t\t :"; cin>>choice; while(!(choice >= 1 && choice <= 6)) { cout<<" , :"; cin>>choice; } return choice; } // void Mybook::get_user() { int choice = 0; while(1) { system("clear"); choice = menu_user(); system("clear"); switch(choice) { case 1: { add_user(); break; } case 2: { display_user(); break; } case 3: { del_user(); break; } case 4: { update_user(); break; } case 5: { look_borrow(); break; } case 6: { recv_user(); return ; } } cout<<endl<<endl<<endl; cout<<" ..."; getchar(); getchar(); } } // void Mybook::add_user() { User new_user; cout<<" :"; cin>>new_user.stu_number; cout<<" :"; cin>>new_user.stu_name; link_user.push_back(new_user); cout<<" !"; cout<<" (y/n)"; char ch; cin>>ch; while(!(ch == 'Y'||ch == 'y'||ch == 'N'||ch == 'n')) { cout<<" , :"; cin>>ch; } if(ch == 'Y'||ch == 'y') { system("clear"); add_user(); } } // void Mybook::display_user() { list<User>::iterator it = link_user.begin(); while(it != link_user.end()) { cout<<"====================================="<<endl; cout<<" :"<<it->stu_number<<endl; cout<<" :"<<it->stu_name<<endl<<endl; it++; } } // void Mybook::del_user() { char id[20]; cout<<" :"; cin>>id; int flag = 0; list<User>::iterator it = link_user.begin(); while(it != link_user.end()) { if(strcmp(it->stu_number,id) == 0) { link_user.erase(it); flag = 1; break; } it++; } if(flag == 1) { cout<<" !"<<endl; } else { cout<<" , !"<<endl; } } // void Mybook::update_user() { cout<<" :"<<endl; char number[20]; cin>>number; int flag = 0; list<User>::iterator it = link_user.begin(); while(it != link_user.end()) { if(strcmp(it->stu_number,number) == 0) { cout<<" :"; cin>>it->stu_number; cout<<" :"; cin>>it->stu_name; flag = 1; break; } it++; } if(flag == 1) { cout<<" !"<<endl; } else { cout<<" !"<<endl; } } // void Mybook::look_borrow() { int flag = 0; list<Book>::iterator p = link_book.begin(); while(p != link_book.end()) { if(p->state == 1) { cout<<"==================================================="<<endl; cout<<" :"<<p->stu_name<<endl; cout<<" :"<<p->stu_number<<endl; cout<<" :"<<p->book_name<<endl; cout<<" :"<<p->place<<endl; flag = 1; } p++; } if(flag == 1) { cout<<" "<<endl; } else { cout<<" !"<<endl; } } // int Mybook::menu() { int choice = 0; cout<<"\t\t\t\t\t============================================"<<endl; cout<<"\t\t\t\t\t* *"<<endl; cout<<"\t\t\t\t\t*------------------------------------------*"<<endl; cout<<"\t\t\t\t\t* 1、 2、 *"<<endl; cout<<"\t\t\t\t\t*------------------------------------------*"<<endl; cout<<"\t\t\t\t\t* 3、 4、 *"<<endl; cout<<"\t\t\t\t\t*------------------------------------------*"<<endl; cout<<"\t\t\t\t\t* 5、 6、 *"<<endl; cout<<"\t\t\t\t\t*------------------------------------------*"<<endl; cout<<"\t\t\t\t\t :"; cin>>choice; while(!(choice >= 1 && choice <= 6)) { cout<<" , :"; cin>>choice; } return choice; } // void Mybook::getchoice() { int choice = 0; while(1) { system("clear"); choice = menu(); system("clear"); switch(choice) { case 1: { add_book(); break; } case 2: { display_book(); break; } case 3: { del_book(); break; } case 4: { search_book(); break; } case 5: { update_book(); break; } case 6: { return ; } } cout<<endl<<endl<<endl; cout<<" ....."<<endl; getchar(); getchar(); } } // void Mybook::add_book() { Book new_book; cout<<" :"; cin>>new_book.id; cout<<" :"; cin>>new_book.book_name; cout<<" :"; cin>>new_book.place; link_book.push_back(new_book); cout<<" !"; cout<<" (y/n)"; char ch; cin>>ch; while(!(ch == 'Y'||ch == 'y'||ch == 'N'||ch == 'n')) { cout<<" , :"; cin>>ch; } if(ch == 'Y'||ch == 'y') { system("clear"); add_book(); } } // void Mybook::display_book() { list<Book>::iterator p = link_book.begin(); while(p != link_book.end()) { cout<<"======================================="<<endl; cout<<" :"<<p->id<<endl; cout<<" :"<<p->book_name<<endl; if(p->state == 0) { cout<<" : !"<<endl; } else { cout<<" : !"<<endl; } cout<<" "<<p->place<<endl<<endl; p++; } } // void Mybook::del_book() { list<Book>::iterator p = link_book.begin(); int num = 0; int flag = 0; cout<<" :"; cin>>num; while(p != link_book.end()) { if(p->id == num) { link_book.erase(p); flag = 1; break; } p++; } if(flag == 1) { cout<<"

!"; } else { cout<<"

, !"<<endl; } } // void Mybook::search_book() { list<Book>::iterator p = link_book.begin(); char book_name[20]; int flag = 0; cout<<" :"; cin>>book_name; while(p != link_book.end()) { if(strcmp(p->book_name,book_name) == 0) { cout<<"======================================="<<endl; cout<<" :"<<p->id<<endl; cout<<" :"<<p->book_name<<endl; if(p->state == 0) { cout<<" : !"<<endl; } else { cout<<" : !"<<endl; } cout<<" "<<p->place<<endl; flag = 1; } p++; } if(flag == 1) { cout<<"

!"; } else { cout<<"

!"<<endl; } } // void Mybook::update_book() { list<Book>::iterator p = link_book.begin(); int num = 0; int flag = 0; cout<<" :"; cin>>num; while(p != link_book.end()) { if(p->id == num) { cout<<" "; cin>>p->book_name; cout<<" :"; cin>>p->place; flag = 1; } p++; } if(flag == 1) { cout<<"

!"; } else { cout<<"

, !"<<endl; } } int main() { Mybook b; b.getmenu(); return 0; }
더 많은 학습 자 료 는 주제 인 에 주목 하 세 요.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기