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;
}
더 많은 학습 자 료 는 주제 인 에 주목 하 세 요.이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Visual Studio에서 파일 폴더 구분 (포함 경로 설정)Visual Studio에서 c, cpp, h, hpp 파일을 폴더로 나누고 싶었습니까? 어쩌면 대부분의 사람들이 있다고 생각합니다. 처음에 파일이 만들어지는 장소는 프로젝트 파일 등과 같은 장소에 있기 때문에 파일...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.