#include "stdafx.h"
typedef struct _book
{
char BookName[50];
struct _book *next;
struct _book *prev;
}BookList;
BookList *Head, *Tail;
void InitBookList()
{
Head = (BookList*)malloc(sizeof(BookList));
Tail = (BookList*)malloc(sizeof(BookList));
Head->next = Tail;
Tail->prev = Head;
}
void AddBookList(char *str)
{
BookList *add = (BookList*)malloc(sizeof(BookList));
strcpy(add->BookName, str);
Tail->prev->next = add;
add->prev = Tail->prev;
add->next = Tail;
Tail->prev = add;
}
void DelBookList(char *str)
{
BookList *del,*loof;
loof = Head->next;
while(loof != Tail)
{
if(strcmp(loof->BookName, str) == 0)
{
del = loof;
loof->prev->next = loof->next;
loof->next->prev = loof->prev;
free(del);
return;
}
loof = loof->next;
}
}
void SearchBookList(char *str)
{
BookList *loof;
loof = Head->next;
while(loof != Tail)
{
if(strcmp(loof->BookName, str) == 0)
{
printf("%s을 찾았습니다\n", str);
return;
}
loof = loof->next;
}
printf("%s을 찾지못했습니다\n", str);
}
void EditBookList(char *str)
{
BookList *loof;
loof = Head->next;
while(loof != Tail)
{
if(strcmp(loof->BookName, str) == 0)
{
printf("바꿀 책이름 입력:");
gets(loof->BookName);
return;
}
loof = loof->next;
}
}
void ShowBookList()
{
BookList *loof;
loof = Head->next;
printf("책 제목\n");
while(loof != Tail)
{
printf("%s\n", loof->BookName);
loof = loof->next;
}
}
void SaveData()
{
BookList *loof;
FILE *fp = fopen("book.txt", "w");
loof = Head->next;
while(loof != Tail)
{
fprintf(fp,"%s\n", loof->BookName);
loof = loof->next;
}
fclose(fp);
}
void LoadData()
{
FILE *fp = fopen("book.txt","r");
char str[50];
if(fp == NULL) return;
while(1)
{
if(fgets(str,50,fp) == NULL) break;
if(strlen(str) > 1)
{
str[strlen(str)-1] = NULL;
AddBookList(str);
}
}
fclose(fp);
}
int main()
{
int num;
char str[50];
InitBookList();
LoadData();
while(1)
{
do{
//system("cls");
printf("###########\n");
printf("1. 도서추가\n2. 도서수정\n3. 도서리스트\n4.도서 검색\n5.도서 삭제\n6.종 료\n");
printf("###########\n");
printf("숫자를 입력하세요 :");
scanf("%d", &num);
fflush(stdin);
}while(num<=0||num>6);
switch(num)
{
case 1:
printf("추가할 책 이름 입력:");
gets(str);
AddBookList(str);
break;
case 2:
printf("수정할 책 이름 입력:");
gets(str);
EditBookList(str);
break;
case 3:
ShowBookList();
break;
case 4:
printf("검색할 책 이름 입력:");
gets(str);
SearchBookList(str);
break;
case 5:
printf("삭제할 책 이름 입력:");
gets(str);
DelBookList(str);
break;
case 6:
SaveData();
return 0;
break;
}
}
return 0;
}
'C++ 프로그래밍' 카테고리의 다른 글
오버로딩 프로그램 예제 (0) | 2009.09.22 |
---|