~cpp #ifndef BOOK_H_ #define BOOK_H_ class Book { private: char name[20]; char writer[20]; char ISBN[20]; bool isBorrow; public: Book * next; Book(); char * get_name(); char * get_writer(); char * get_ISBN(); bool get_state(); void set_state(bool state); void input(); void operator=(Book & a); }; #endif
~cpp #include "book.h" #include <iostream> #include <cstring> using namespace std; Book::Book() { name[0] = writer[0] = ISBN[0] = NULL; isBorrow = false; next = NULL; } void Book::input() { cout << "책 명 : "; cin >> name; cout << "작가 : "; cin >> writer; cout << "ISBN : "; cin >> ISBN; } char * Book::get_ISBN() { return ISBN; } char * Book::get_name() { return name; } char * Book::get_writer() { return writer; } bool Book::get_state() { return isBorrow; } void Book::set_state(bool state) { isBorrow = state; } void Book::operator =(Book & a) { strcpy(name, a.name); strcpy(writer, a.writer); strcpy(ISBN, a.ISBN); next = a.next; }
~cpp #ifndef MANAGEBOOK_H_ #define MANAGEBOOK_H_ #include "book.h" class ManageBook { private: Book * book_list; public: ManageBook(); void regist(); void insert_list(Book & b); void borrow(); void restore(); void search(Book & b, bool kind_order); void show_list(); }; #endif
~cpp #include "manageBook.h" #include <iostream> #include <cstring> using namespace std; ManageBook::ManageBook() { book_list = NULL; } void ManageBook::regist() { Book * b = new Book[1]; cout << "등록할 도서정보를 입력하세요. \n"; (*b).input(); insert_list(*b); } void ManageBook::insert_list(Book & b) { if (!book_list) { book_list = &b; return; } Book * temp = book_list; while (temp->next != NULL) temp = temp->next; temp->next = &b; } void ManageBook::borrow() { cout << "찾을 도서정보를 입력하세요. \n"; Book b; b.input(); search(b, false); } void ManageBook::search(Book & b, bool kind_order) { Book * temp = book_list; while (temp != NULL) { if (strcmp((*temp).get_ISBN(), b.get_ISBN()) == 0 && strcmp((*temp).get_name(), b.get_name()) == 0) { if ((*temp).get_state() == kind_order) { (*temp).set_state(!kind_order); cout << "성공\n"; } else cout << "실패\n"; return; } temp = temp->next; } cout << "없는 도서입니다.\n"; } void ManageBook::restore() { cout << "반납할 도서정보를 입력하세요. \n"; Book b; b.input(); search(b, true); } void ManageBook::show_list() { Book * temp = book_list; cout.width(10); cout << "책 이름"; cout.width(10); cout << "저자"; cout.width(10); cout << "ISBN\n"; while (temp != NULL) { cout.width(10); cout << (*temp).get_name(); cout.width(10); cout << (*temp).get_writer(); cout.width(10); cout << (*temp).get_ISBN() << endl; temp = temp->next; } }
~cpp #include "manageBook.h" #include <iostream> using namespace std; int main() { ManageBook test; int command; while (1) { cout << "** 도서관리 프로그램 **\n"; cout << "1. 책 등록\n"; cout << "2. 책 대여\n"; cout << "3. 책 반납\n"; cout << "4. 책 목록\n"; cout << "5. 종료\n"; cin >> command; switch (command) { case 1: test.regist(); break; case 2: test.borrow(); break; case 3: test.restore(); break; case 4: test.show_list(); break; case 5: return 0; default: break; } } return 1; }