#include <iostream> #include "class.h" using namespace std; int input_book(); int book_process(int); book_database* datas; void main() { int select=0; datas=new book_database(); while (4!=select) { cout << "1.자료입력 2.대여 3. 반납 4.종료\n>>"; cin >> select; fflush(stdin); if (1==select) input_book(); else if (2==select || 3==select) book_process(select-2); } delete datas; } void print_line(char* temp_name, char* temp_writer, char* temp_isbn) { cout << "\n"; cout << "책의 이름 : " << temp_name << "\n"; cout << "책의 저자 : " << temp_writer << "\n"; cout << "책의 ISBN : " << temp_isbn << "\n"; } int input_book() { char temp_name[256]; char temp_writer[256]; char temp_isbn[256]; char y_n=0; cout << "책의 이름을 입력해주세요>>"; cin.getline(temp_name,256); cout << "책의 저자를 입력해주세요>>"; cin.getline(temp_writer,256); cout << "책의 ISBN을 입력해주세요>>"; cin.getline(temp_isbn,256); print_line(temp_name,temp_writer,temp_isbn); while (y_n!='n' && y_n!='N') { cout << "\n맞습니까?(Y/N)? "; cin >> y_n; fflush(stdin); if ('y'==y_n || 'Y'==y_n ) { datas->input_data(temp_name,temp_writer,temp_isbn); return 0; } } return -1; } int book_process(int select) { int selected=0, where; const char output_data[2][5]={"대여","반납"}; char temp_char[256], temp_name[256], temp_writer[256], temp_isbn[256]; char y_n=0; cout << output_data[select] << "하실 책을 무엇으로 검색하시겠습니까? (1.이름 2.ISBN)\n>>"; while (selected<1 || selected>2) { cin >> selected; } fflush(stdin); cout << "검색어를 입력해 주세요.\n>>"; cin.getline(temp_char,256); where=datas->such(temp_char,selected); if (-1==where) { cout << "검색어를 찾을 수 없습니다.\n"; return -1; } datas->return_line(where, temp_name, temp_writer, temp_isbn); print_line(temp_name,temp_writer,temp_isbn); while (y_n!='n' && y_n!='N') { fflush(stdin); cout << "\n맞습니까?(Y/N)? "; cin >> y_n; if ('y'==y_n || 'Y'==y_n ) { if (-1==datas->data_process(where, (select+1)%2)) cout << output_data[select] << "에 실패하였습니다.\n"; else cout << output_data[select] << "을 완료하였습니다.\n"; return 0; } } return -2; }
#include "class.h" int book_database::get_size(char* target) { int i=0; while (0!=target[i]) ++i; return i+1; } void book_database::str_copy(char* target, char* original) { for (register int i=0; 0!=original[i];++i) target[i]=original[i]; target[i]=0; } int book_database::str_cmp(char* target_a, char* target_b) { for (register int i=0; 0!=target_a[i];++i) { if (target_a[i]!=target_b[i]) return -1; } if (target_a[i]!=target_b[i]) return -1; return 0; } book_database::book_database() { number_data=0; for (register int i=0; i<MAX_HANGMOK; ++i) datas[i]=0; } book_database::~book_database() { for (register int i=0; i<number_data; ++i) { for (register int j=0; j<MAX_HANGMOK; ++j) delete datas[j][i]; } for (register int i=0; i<MAX_HANGMOK; ++i) delete datas[i]; } void book_database::input_data(char* input_name, char* input_writer, char* input_isbn) { char **temp_datas[MAX_HANGMOK]; for (register int i=0; i<MAX_HANGMOK; ++i) temp_datas[i]=new char*[number_data+1]; for (register int i=0; i<MAX_HANGMOK; ++i) { for(register int j=0; j<number_data; ++j) temp_datas[i][j]=datas[i][j]; } for (register int i=0; i<MAX_HANGMOK; ++i) delete datas[i]; temp_datas[0][number_data]=new char[get_size(input_name)]; str_copy(temp_datas[0][number_data],input_name); temp_datas[1][number_data]=new char[get_size(input_writer)]; str_copy(temp_datas[1][number_data],input_writer); temp_datas[2][number_data]=new char[get_size(input_isbn)]; str_copy(temp_datas[2][number_data],input_isbn); temp_datas[3][number_data]=new char; temp_datas[3][number_data][0]=0; for (register int i=0; i<MAX_HANGMOK; ++i) datas[i]=temp_datas[i]; ++number_data; } int book_database::such(char* target, int select_menu) { for (register int i=0; i<number_data; ++i) { char* temp_target; if (1==select_menu) temp_target=datas[0][i]; else temp_target=datas[2][i]; if (0==str_cmp(temp_target,target)) return i; } return -1; } void book_database::return_line(int number, char* temp_name, char* temp_writer, char* temp_isbn) { str_copy(temp_name,datas[0][number]); str_copy(temp_writer,datas[1][number]); str_copy(temp_isbn,datas[2][number]); } int book_database::data_process(int data_number, int state_number) { if (state_number==*datas[3][data_number]) return -1; *datas[3][data_number]=state_number; return 0; }
const int MAX_HANGMOK=4; class book_database{ private: int number_data; char **datas[MAX_HANGMOK]; int get_size(char*); void str_copy(char*, char*); int str_cmp(char* , char* ); public: book_database(); ~book_database(); void input_data(char* , char* , char* ); int such(char* , int ); void return_line(int , char* , char* , char* ); int data_process(int , int ); };