#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h>
#define FILEDB "BOOK.DB"
#pragma pack(1) typedef struct _book { char book[30]; char author[15]; char publisher[10]; char price[10]; char year[5]; struct _book * stNext; }Book; #pragma pack(4)
void Book_Node(Book ** ); void Book_Make_Node(Book ** ); void Book_Load(Book ** ); void Book_Insertfile(Book ** ); void Book_Save(Book * ); Book * Book_Search(Book * , Book *); // Search void Book_Free(Book * ); void Book_Print(Book * ); void Book_Insert(Book ** , Book *); // *stTemp를 삽입해주는 것으로 변경 void Book_Init(Book * ); int Input(void); void Menu(void);
void Menu() { printf("1. insert \n"); printf("2. list \n"); printf("3. quit \n");
return; }
int Input() { int iMenu;
while(1) { printf("번호를 입력하시오 (1 ~ 3)\n"); scanf("%d", &iMenu); if((1> iMenu) || (3 < iMenu)) { getchar(); continue; }
break; }
return iMenu; }
void Book_Init(Book * stTemp) { int iR;
if(0 == stTemp) { return; }
memset(stTemp, 0x00, sizeof(Book)); printf("책이름을 입력하시오\n"); iR = read(0, stTemp->book,sizeof(stTemp->book)); stTemp->book[iR-1] = 0; printf("작가를 입력하시오\n"); iR = read(0, stTemp->author, sizeof(stTemp->author)); stTemp->author[iR-1] = 0; printf("출판사를 입력하시오\n"); iR = read(0, stTemp->publisher, sizeof(stTemp->publisher)); stTemp->publisher[iR-1] = 0; printf("가격을 입력하시오\n"); iR = read(0, stTemp->price, sizeof(stTemp->price)); stTemp->price[iR-1] = 0; printf("출판년도를 입력하시오\n"); iR = read(0, stTemp->year, sizeof(stTemp->year)); stTemp->year[iR-1] = 0; printf("============================================\n");
return; }
void Book_Insert(Book ** stHead, Book *stTemp) // *stObj를 삽입해주는 것으로 변경 {
Book * stSeeker; // 들어갈 자리 찾기 Book * stHelp;
if(0 == stHead) { printf("Book_Insert :: ERROR. No Linked List \n"); return; }
if(0 == stTemp) { printf("Book_Insert :: ERROR. New Node Empty \n"); return; }
stTemp->stNext = 0;
stHelp = stSeeker = *stHead; while(0 != stSeeker) // 삽입할 위치 검색 { if(0 > strcmp(stTemp->book , stSeeker->book)) //정렬// 삽입할 위치를 찾았을 경우 { break; } stHelp = stSeeker; stSeeker = stSeeker->stNext; }
if(0 == *stHead) // 노드 없을 경우 { *stHead = stTemp;
} else if(stSeeker == stHelp) // 넣을 위치가 맨 앞일 경우 { stTemp->stNext = stSeeker; *stHead = stTemp; } else // 중간 삽입 // 끝 삽입 { stTemp->stNext = stSeeker; stHelp->stNext = stTemp; }
return; }
void Book_Print(Book * Temp) { printf("============================================\n"); while(0 != Temp ) { printf("책이름 : %s \n", Temp->book); printf("작 가 : %s \n", Temp->author); printf("출판사 : %s \n", Temp->publisher); printf("가 격 : %s \n", Temp->price); printf("연 도 : %s \n", Temp->year); printf("============================================\n"); Temp = Temp->stNext; } printf("출력종료\n"); //putchar('\n');
return; }
void Book_Free(Book * Head) { Book * Temp;
while(0 != Head) { Temp = Head->stNext; free(Head); Head = Temp; } return; }
Book * Book_Search(Book * stHead, Book *stTemp) // Search {
Book * stSeeker; // 들어갈 자리 찾기
if(0 == stTemp) { printf("Book_Search :: ERROR. New Node Empty \n"); return 0; }
stSeeker = stHead; while(0 != stSeeker) // 삽입할 위치 검색 { if(0 == strcmp(stTemp->book , stSeeker->book)) //정렬// 삽입할 위치를 찾았을 경우 { break; } stSeeker = stSeeker->stNext; }
return stSeeker; }
void Book_Save(Book * stHead) { int iFd;
if(0 == stHead) { return; }
iFd = open(FILEDB, O_CREAT | O_TRUNC | O_RDWR | O_BINARY); // O_TRUNC - 기존파일 삭제 // , S_IREAD | S_IWRITE if(0 > iFd) { printf("Book_Save :: ERROR. OPEND FAILED"); return; }
while(0 != stHead) { write(iFd, stHead, sizeof(Book)); stHead = stHead->stNext; }
close(iFd);
printf("Save Complete\n"); return; }
void Book_Insertfile(Book ** stHead) { Book * stTemp = 0; Book * stSeeker; // 들어갈 자리 찾기 Book * stHelp;
if(0 == stHead) { printf("Book_Insertfile :: ERROR. No Linked List \n"); return; }
stTemp = malloc(sizeof(Book));
if(0 == stTemp) { printf("Book_Insertfile :: ERROR. No Memery Available \n"); return; }
Book_Init(stTemp);
stHelp = stSeeker = *stHead; while(0 != stSeeker) // 삽입할 위치 검색 { if(0 > strcmp(stTemp->book , stSeeker->book)) //정렬// 삽입할 위치를 찾았을 경우 { break; } stHelp = stSeeker; stSeeker = stSeeker->stNext; }
if(0 == *stHead) // 노드 없을 경우 { *stHead = stTemp;
} else if(stSeeker == stHelp) // 넣을 위치가 맨 앞일 경우 { stTemp->stNext = stSeeker; *stHead = stTemp; } else // 중간 삽입 // 끝 삽입 { stTemp->stNext = stSeeker; stHelp->stNext = stTemp; } return; }
void Book_Load(Book ** stHead) { int iFd; Book *stTemp; int iRet;
if(0 == stHead) { return; }
iFd = open(FILEDB, O_RDONLY); // // , S_IREAD | S_IWRITE if(0 > iFd) { printf("Book_Load :: ERROR. OPEN FAILED"); return; }
while(1) { stTemp = malloc(sizeof(Book)); // 동적 할당
if(0 == stTemp) { printf("Book_Load :: ERROR. No Memery Available \n"); break; }
iRet = read(iFd, stTemp, sizeof(Book)); if(sizeof(Book) != iRet) { free(stTemp); break; } Book_Insert(stHead, stTemp); }
close(iFd);
printf("LOAD COMPLETED\n");
return; }
void Book_Make_Node(Book ** stTemp) { *stTemp = malloc(sizeof(Book)); // 동적 할당
if(0 == *stTemp) { printf("Book_Make_Node :: ERROR. No Memery Available \n"); return; }
Book_Init(*stTemp); return; }
void Book_Node(Book ** stHead) { Book * stTemp;
Book_Make_Node(&stTemp); Book_Insert(stHead, stTemp);
return; }
int main() { Book * Head = 0; int iCmd;
while(1) { Menu(); iCmd = Input();
switch(iCmd) { case 1: Book_Node(&Head); break;
case 2: Book_Print(Head); break;
case 3: Book_Free(Head); break; } if(3 == iCmd) break; }
return 0; } |