#include <stdio.h>
#include <string.h>
#include <windows.h>
#define TRUE 1
#define FALSE 0
#define REG 1
#define VIEW 2
#define SEARCH 3
#define SAVE 4
#define LOAD 5
#define EXIT 6
#define S_ITEM 1
#define S_NAME 2
#define S_PHONE 3
#define S_ADDRESS 4
#define S_GROUP 5
#define S_MD 6
#define S_EXIT 7
#define LIST_MAX 100
typedef struct _list
{
char name[255];
char phone[255];
char address[255];
char group[255];
int used;
int searched;
}LIST;
void Main_Menu(int *total); // 메인 메뉴 출력
void Exit(void); // 프로그램 종료
void Registration(LIST *member, int *total); // 새 리스트 등록
int Auto_num(LIST* member); // 자동 번지 설정
void Contents_input(char *title, char *contents); // 내용 저장
void List_view(LIST *member, int total); // 리스트 보기
void File_save(LIST *member, int total); // 리스트 파일 저장
void File_load(LIST *member, int *total); // 리스트 파일 읽기
void Search(LIST *member, int *total); // 리스트 검색
void Search_Menu(void); // 리스트 검색 메뉴
void Search_init(LIST *member); // 리스트 검색 초기화
void Search_view(LIST *member); // 리스트 검색 결과 출력
void Search_item(LIST *member); // 리스트 항목별 검색
void Search_string(LIST *member, int s_title); // 리스트 문자별 검색
void Search_MD_Menu(void); // 리스트 수정/삭제 메뉴
void Search_MD(LIST *member, int *total); // 리스트 수정/삭제
void Re_registration(LIST *member, int s_item, int title); // 리스트 수정 등록
/* MAIN START */
void main()
{
LIST member[LIST_MAX] = {0}; // 멤버 등록 정보
int choice = 0; // 메뉴 선택
int total =0; // 리스트 등록 수
while(TRUE)
{
Main_Menu(&total);
printf("Choice >> ");
scanf("%d", &choice);
fflush(stdin);
switch(choice)
{
case REG : { Registration(member, &total); break; }
case VIEW : { List_view(member, total); break; }
case SEARCH : { Search(member, &total); break; }
case SAVE : { File_save(member, total); break; }
case LOAD : { File_load(member, &total); break; }
case EXIT : { Exit(); break; }
}
system("pause");
}
}
/* 메인 메뉴를 출력 합니다. */
void Main_Menu(int *total)
{
system("cls");
puts("");
puts("┏━━━━━━━━━━━━━━━━━━┓");
puts("┃ MY ADDRESS BOOK ┃");
puts("┣━━━━━━━━━━━━━━━━━━┫");
puts("┃ 1. REGISTRATION ┃");
puts("┃ 2. LIST VIEW ┃");
puts("┃ 3. SEARCH <MODIFY / DELETE> ┃");
puts("┃ 4. FILE SAVE ┃");
puts("┃ 5. FILE LOAD ┃");
puts("┃ 6. EXIT ┃");
puts("┣━━━━━━━━━━━━━━━━━━┫");
printf("┃ Total of list : %3d item. ┃\n", *total);
puts("┗━━━━━━━━━━━━━━━━━━┛");
puts("");
}
/* 프로그램을 종료 합니다. */
void Exit(void)
{
puts("");
puts("┏━━━━━━━━━━━━━━━━━━┓");
puts("┃ PROGRAM END ┃");
puts("┗━━━━━━━━━━━━━━━━━━┛");
puts("");
exit(TRUE);
}
/* 새로운 주소를 등록 합니다. */
void Registration(LIST *member, int *total)
{
int num, flag, i;
char group[5][10] = {"FAMILY", "RELATIVE", "FRIEND", "COLLEAGUE", "Ete."};
char item[5], decision;
do
{
system("cls");
if(*total == LIST_MAX)
{
puts("LIST FULL, Registration failed !");
return;
}
do
{
flag = FALSE;
num = Auto_num(member);
printf("Auto register : [%3d] >> ", ++num);
gets(item);
if(item[0] != '\0')
{
num = atoi(item);
if(member[num-1].used != 0)
{
flag = TRUE;
printf("%d of list is already registered.\n", num);
puts("Please, try again");
system("pause");
system("cls");
}
}
}while(flag == TRUE);
printf("\nRegistration : [%3d]\n", num);
Contents_input("NAME\t", member[num-1].name);
Contents_input("PHONE\t", member[num-1].phone);
Contents_input("ADDRESS\t", member[num-1].address);
do
{
for(i = 0; i < 5; i++)
printf("\t%d. %s", i+1, group[i]);
printf("\nGROUP\t>> ");
scanf("%d", &i);
fflush(stdin);
}while (i < 0 && i > 6);
strcpy(member[num-1].group, group[i-1]);
printf("\nDo you want save? <N/n> or other keys >> ");
decision = getchar();
fflush(stdin);
puts("");
if( strchr("Nn", decision) != NULL )
puts("SAVE Canceled.\n");
else
{
member[num-1].used = 1;
printf("SAVE Complete. Total of list : %3d item.\n", ++*total);
}
printf("\nDo you want input continue? <Y/y> or other keys >> ");
decision = getchar();
fflush(stdin);
puts("");
}while(strchr("Yy", decision) != NULL);
}
/* 빈번지를 자동으로 설정 합니다. */
int Auto_num(LIST *member)
{
int i=0;
while(member[i].used != 0)
i++;
return (i);
}
/* 타이틀을 출력하고 입력된 내용을 등록 합니다. */
void Contents_input(char *title, char *contents)
{
do
{
printf("%s>> ", title);
gets(contents);
}while(contents[0] == '\0');
}
/* 등록된 리스트를 출력 합니다. */
void List_view(LIST *member, int total)
{
int i;
system("cls");
if(total == 0)
{
puts("List is empty.");
return;
}
puts("No.\t NAME\t PHONE\t ADDRESS\t GROUP\t");
for(i = 0; i < LIST_MAX ; i++)
{
if(member[i].used != 0)
{
printf("[%3d]\t %s\t %s\t %s\t %s\t\n",
i+1, member[i].name, member[i].phone, member[i].address, member[i].group);
}
}
}
/* 리스트를 파일로 저장 합니다. */
void File_save(LIST *member, int total)
{
FILE *fp;
int i;
if(total == 0)
{
puts("List is empty.");
puts("SAVE Canceled.\n");
return;
}
fp = fopen("my_list.txt", "w");
if(fp == NULL)
{
puts("Don't have enough of storage capacity.");
puts("SAVE Canceled.\n");
return;
}
for(i = 0; i < LIST_MAX ; i++)
{
if(member[i].used == 1)
{
fprintf(fp, "%d>%s>%s>%s>%s\n",
i, member[i].name, member[i].phone, member[i].address, member[i].group);
printf("[%3d]\t %s\t %s\t %s\t %s\t\n",
i+1, member[i].name, member[i].phone, member[i].address, member[i].group);
}
}
puts("SAVE Complete !!!\n");
fclose(fp);
}
/* 리스트 파일을 읽어 옵니다. */
void File_load(LIST *member, int *total)
{
FILE *fp;
char decision, ch;
int i, num;
fp = fopen("my_list.txt", "r");
if(fp == NULL)
{
puts("Can't read [my_list.txt] file.");
puts("LOAD Canceled.\n");
return;
}
if(*total != 0)
{
printf("\nDo you want file load? Your current data will be lost !\n");
printf("<Y/y> or other keys >> ");
decision = getchar();
fflush(stdin);
puts("");
if( strchr("Yy", decision) != NULL )
{
*total = 0;
for(i = 0; i < LIST_MAX; i++)
member[i].used = 0;
puts("Memory Initialized.\n");
}
else
{
puts("LOAD Canceled.\n");
fclose(fp);
}
}
puts("Data Loading...\n");
while( (ch = fgetc(fp)) != EOF )
{
num = 0;
do
{
num = num * 10 + (ch -'0');
}while((ch = fgetc(fp)) != '>');
i = 0;
while((ch = fgetc(fp)) != '>')
member[num].name[i++] = ch;
member[num].name[i] = '\0';
i = 0;
while((ch = fgetc(fp)) != '>')
member[num].phone[i++] = ch;
member[num].phone[i] = '\0';
i = 0;
while((ch = fgetc(fp)) != '>')
member[num].address[i++] = ch;
member[num].address[i] = '\0';
i = 0;
while((ch = fgetc(fp)) != '\n')
member[num].group[i++] = ch;
member[num].group[i] = '\0';
member[num].used = 1;
++*total;
printf("[%3d]\t %s\t %s\t %s\t %s\t\n",
num+1, member[num].name, member[num].phone, member[num].address, member[num].group);
Sleep(300);
}
puts("\nLOAD Complete !!!\n");
fclose(fp);
}
/* 검색 메뉴를 출력 합니다. */
void Search_Menu(void)
{
system("cls");
puts("");
puts("┏━━━━━━━━━━━━━━━━━━┓");
puts("┃ SEARCH MENU ┃");
puts("┣━━━━━━━━━━━━━━━━━━┫");
puts("┃ 1. Item Number Search ┃");
puts("┃ 2. Name Search ┃");
puts("┃ 3. Phone Number Search ┃");
puts("┃ 4. Address Search ┃");
puts("┃ 5. Group Search ┃");
puts("┃ 6. Modify / Delete ┃");
puts("┃ 7. EXIT ┃");
puts("┗━━━━━━━━━━━━━━━━━━┛");
puts("");
}
/* 수정/검색 메뉴를 출력 합니다. */
void Search_MD_Menu(void)
{
puts("");
puts("┏━━━━━━━━━━━━━━━━━━┓");
puts("┃ 1. Modify ┃");
puts("┃ 2. Delete ┃");
puts("┃ 3. EXIT ┃");
puts("┗━━━━━━━━━━━━━━━━━━┛");
puts("");
}
/* 리스트를 항목별로 검색 합니다. */
void Search(LIST *member, int *total)
{
int choice, cnt = 0;
while(TRUE)
{
Search_Menu();
if(*total == 0)
{
puts("List is empty.");
puts("SEARCH Canceled.\n");
return;
}
printf("Choice >> ");
scanf("%d", &choice);
fflush(stdin);
switch(choice)
{
case S_ITEM : { Search_item(member); break; }
case S_NAME : { Search_string(member, S_NAME); break; }
case S_PHONE : { Search_string(member, S_PHONE); break; }
case S_ADDRESS : { Search_string(member, S_ADDRESS); break; }
case S_GROUP : { Search_string(member, S_GROUP); break; }
case S_MD : { Search_MD(member, total); break; }
case S_EXIT : { return; }
}
}
}
/* 검색 조건을 초기화 합니다. */
void Search_init(LIST *member)
{
int i;
for(i = 0; i < LIST_MAX; i++)
member[i].searched = 0;
}
/* 검색된 결과를 출력 합니다. */
void Search_view(LIST *member)
{
int i;
puts("No.\t NAME\t PHONE\t ADDRESS\t GROUP\t");
for(i = 0; i < LIST_MAX ; i++)
{
if(member[i].searched == 1)
{
printf("[%3d]\t %s\t %s\t %s\t %s\t\n",
i+1, member[i].name, member[i].phone, member[i].address, member[i].group);
}
}
}
/* 항목 번호로 검색을 수행 합니다. */
void Search_item(LIST *member)
{
int num;
char decision;
do
{
Search_init(member);
printf("Item Number >> ");
scanf("%d", &num);
fflush(stdin);
if(member[num-1].used == 0)
{
printf("[%3d] Item is Empty !!!\n", num);
printf("Do you want item search continue? (Y/y) >> ");
decision = getchar();
fflush(stdin);
}
else
{
member[num-1].searched = 1;
Search_view(member);
printf("Do you want item search continue? (Y/y) >> ");
decision = getchar();
fflush(stdin);
}
}while(strchr("Yy", decision) != NULL);
}
/* 타이틀별로 검색을 수행 합니다. */
void Search_string(LIST *member, int s_title)
{
char s_buf[255], decision;
int i, cnt;
do
{
cnt = 0;
Search_init(member);
printf("Search >> ");
scanf("%s", &s_buf);
fflush(stdin);
for(i = 0; i < LIST_MAX; i++)
{
if(member[i].used != 0)
{
switch(s_title)
{
case S_NAME :
{
if(strstr(member[i].name, s_buf) != NULL)
{
member[i].searched = 1;
cnt++;
}
}
case S_PHONE :
{
if(strstr(member[i].phone, s_buf) != NULL)
{
member[i].searched = 1;
cnt++;
}
}
case S_ADDRESS :
{
if(strstr(member[i].address, s_buf) != NULL)
{
member[i].searched = 1;
cnt++;
}
}
case S_GROUP :
{
if(strstr(member[i].group, s_buf) != NULL)
{
member[i].searched = 1;
cnt++;
}
}
}
}
}
if(cnt == 0)
{
printf("[%s] is not match !!!", s_buf);
printf("Do you want search continue? (Y/y) >> ");
decision = getchar();
fflush(stdin);
}
else
{
Search_view(member);
printf("Do you want search continue? (Y/y) >> ");
decision = getchar();
fflush(stdin);
}
}while(strchr("Yy", decision) != NULL);
}
/* 검색된 목록에서 리스트를 수정/삭제 합니다. */
void Search_MD(LIST *member, int *total)
{
int i, flag=0;
char decision, mode, choice, title;
for(i = 0; i < LIST_MAX ; i++)
{
if(member[i].searched == TRUE)
{
flag = TRUE;
}
}
if(flag == FALSE)
{
puts("Search result don't exist");
system("pause");
return;
}
else
{
do
{
flag = FALSE;
Search_MD_Menu();
printf("Choice >> ");
mode = getchar();
fflush(stdin);
Search_view(member);
if(mode == '1')
{
do
{
printf("Motify item choice >> ");
choice = getchar();
fflush(stdin);
if(member[choice-'0'-1].searched == FALSE)
{
puts("This item is not searched.");
}
}while(member[choice-'0'-1].searched != TRUE);
printf("\n\t\t1.NAME 2.PHONE 3.ADDRESS 4.GROUP\n");
printf("Motify title choice >> ");
title = getchar();
fflush(stdin);
Re_registration(member, choice - '0', title-'0'+1);
printf("\nDo you want Motify item continue? (Y/y) or other keys >> ");
decision = getchar();
fflush(stdin);
}
else if(mode == '2')
{
do
{
printf("\nDelete item choice >> ");
choice = getchar();
fflush(stdin);
if(member[choice-'0'-1].searched == FALSE)
{
puts("This item is not searched.");
}
}while(member[choice-'0'-1].searched != TRUE);
printf("\nDo you want Delete? (Y/y) or other keys >> ");
decision = getchar();
fflush(stdin);
if(strchr("Yy", decision) != NULL)
{
member[(choice-1) - '0'].used = 0;
printf("\nDELETE Complete. <Total of list : %3d item.>\n", --*total);
}
else
{
puts("DELETE Canceled");
}
printf("\nDo you want Delete item continue? (Y/y) or other keys >> ");
decision = getchar();
fflush(stdin);
}
else return;
}while(strchr("Yy", decision) != NULL);
}
}
/* 리스트를 수정 등록 합니다. */
void Re_registration(LIST *member, int s_item, int title)
{
int i;
char group[5][10] = {"FAMILY", "RELATIVE", "FRIEND", "COLLEAGUE", "Ete."};
char decision;
printf("\nRegistration : [%3d]\n", s_item);
switch(title)
{
case S_NAME : {Contents_input("NAME\t", member[s_item-1].name); break;}
case S_PHONE : {Contents_input("PHONE\t", member[s_item-1].phone); break;}
case S_ADDRESS : {Contents_input("ADDRESS\t", member[s_item-1].address); break;}
case S_GROUP :
{
Contents_input("GROUP\t", member[s_item-1].group);
do
{
for(i = 0; i < 5; i++)
printf("\t%d. %s", i+1, group[i]);
printf("\nGROUP\t>> ");
scanf("%d", &i);
fflush(stdin);
}while (i < 0 && i > 6);
strcpy(member[s_item-1].group, group[i-1]);
break;
}
}
printf("\nDo you want save? <N/n> or other keys >> ");
decision = getchar();
fflush(stdin);
puts("");
if( strchr("Nn", decision) != NULL )
puts("MODIFY Canceled.\n");
else
{
member[s_item-1].used = 1;
puts("MODIFY Complete.\n");
}
}