#include <stdio.h>
#include <stdlib.h>
//------------------------------------------------ Struct -------------------------------//
typedef struct _Worker
{
int WorkerID;
char name[10];
char E_mail[30];
char Phone[16];
struct _Worker * next;
}Worker;
//------------------------------------------------ Header -------------------------------//
//------------------------------------------삽입 ----------------------------------------//
Worker * InsertWorker(Worker * Head)
{
Worker * Front = Head;
Worker * Rear = Head;
Worker * New = malloc(sizeof(Worker));
int SameNumber = 0;
int iCnt = 0;
int RightNum = 0;
int MailCk1 = 0;
while(1)
{
printf("직원 번호 입력 :: ");
scanf("%d", &New->WorkerID);
while(Rear != 0)
{
if(New->WorkerID == Rear->WorkerID)
{
SameNumber = 1;
}
Rear = Rear->next;
}
if(New->WorkerID > 0xFFFFFFFF)
{ printf("너무 큰 숫자를 입력하셨습니다 \n"); }
if(SameNumber == 1)
{ printf("중복된 번호가 존재합니다 다시 입력해주세요 \n"); }
else
{ break; }
}
printf("직원 번호 등록 완료! \n");
printf("이름 입력:: ");
scanf("%s", New->name);
fflush(stdin);
while(1)
{
scanf("%s", New->E_mail);
fflush(stdin);
for(iCnt = 0; New->E_mail[iCnt] != 0; ++iCnt)
{
if( New->E_mail[iCnt] == '@' )
{ MailCk1 = 1; }
if( MailCk1 == 1 && New->E_mail[iCnt] == '.')
{ MailCk1 = 2; }
}
if( MailCk1 == 2)
{ break; }
else
{ printf("E_mail.com 입력 양식에 맞게 작성해 주세요 \n"); }
}
while(1)
{
printf("전화번호 입력 ex) 010-1234-5678:: ");
scanf("%s", New->Phone);
fflush(stdin);
if(New->Phone[13] != 0)
{
printf("추가입력된 정보는 자동으로 삭제됩니다 \n");
New->Phone[13] = 0;
}
for(iCnt = 0; New->Phone[iCnt] != 0; ++iCnt)
{
if(iCnt !=3 || iCnt != 5)
{
if(New->Phone[iCnt] > 30 && New->Phone[iCnt] < 59)
{
RightNum = 1;
}
else
{
RightNum = 0;
break;
}
} // 0~9 번호 체크
if(New->Phone[3] == '-' && New->Phone[8] == '-')
{ RightNum = 1; }
else
{ RightNum = 0; } // '-' 표시 체크
}
if(RightNum == 1)
{ break; }
else
{ printf("잘못된 입력입니다. 다시 입력해주세요 \n"); }
}
//----------------------------------입력완료 -> 연결리스트 정렬 ----------------------------------
while(Rear != 0)
{
if( 0 > strcmp(New->name, Rear->name) ) //사전 정렬 :: 이름 정렬
{
break;
}
Front = Rear;
Rear = Rear->next;
}
if(Head != Rear)
{
New->next = Rear;
Front->next = New;
}
else
{
New->next = Rear;
Head = New;
}
return Head;
}
//------------------------------------------검색 ----------------------------------------//
void Worker_Search(Worker * Head)
{
int Success = 0;
Worker * Front = Head;
Worker * Rear = Head;
char Buffer[50];
printf("검색할 이름 입력:: ");
scanf("%s", Buffer);
fflush(stdin);
while(Rear != 0)
{
if( 0 == strcmp( Rear->name, Buffer ) ) //사전정렬
{
printf("찾았습니다!! \n");
printf("-----------------------------------------------------------------------\n");
printf("|직원번호\t|이름\t\t|E-Mail\t\t|PhoneNumber\t\t|\n");
printf("-----------------------------------------------------------------------\n");
printf("|%d\t\t", Rear->WorkerID);
printf("|%s\t\t", Rear->name);
printf("|%s\t\t", Rear->E_mail);
printf("|%s\t\t\n", Rear->Phone);
Success = 1;
break;
}
Front = Rear;
Rear = Rear->next;
}
if( Success ==0)
{
printf("검색에 실패 하였습니다. 조건을 정확히 입력해주세요 \n");
}
}
void Worker_SearchMail(Worker * Head)
{
int Success = 0;
Worker * Front = Head;
Worker * Rear = Head;
char Buffer[50];
printf("검색할 E_mail 입력:: ");
scanf("%s", Buffer);
fflush(stdin);
while(Rear != 0)
{
if( 0 == strcmp( Rear->E_mail, Buffer ) ) //사전정렬
{
printf("찾았습니다!! \n");
printf("-----------------------------------------------------------------------\n");
printf("|직원번호\t|이름\t\t|E-Mail\t\t|PhoneNumber\t\t|\n");
printf("-----------------------------------------------------------------------\n");
printf("|%d\t\t", Rear->WorkerID);
printf("|%s\t\t", Rear->name);
printf("|%s\t\t", Rear->E_mail);
printf("|%s\t\t\n", Rear->Phone);
Success = 1;
break;
}
Front = Rear;
Rear = Rear->next;
}
if( Success ==0)
{
printf("검색에 실패 하였습니다. 조건을 정확히 입력해주세요 \n");
}
}
void Worker_SearchID(Worker * Head)
{
int Success = 0;
Worker * Front = Head;
Worker * Rear = Head;
int Num;
printf("검색할 직원 번호 입력:: ");
scanf("%d", &Num);
fflush(stdin);
while(Rear != 0)
{
if( Rear->WorkerID == Num ) //사전정렬
{
printf("-----------------------------------------------------------------------\n");
printf("|직원번호\t|이름\t\t|E-Mail\t\t|PhoneNumber\t\t|\n");
printf("-----------------------------------------------------------------------\n");
printf("|%d\t", Rear->WorkerID);
printf("|%s\t\t", Rear->name);
printf("|%s\t\t", Rear->E_mail);
printf("|%s\t\t\n", Rear->Phone);
Success = 1;
break;
}
Front = Rear;
Rear = Rear->next;
}
if( Success ==0)
{
printf("검색에 실패 하였습니다. 조건을 정확히 입력해주세요 \n");
}
}
void Ser_Menu(Worker * Head)
{
char cNum = 0;
while(1)
{
printf("-------- 검색 메뉴 ------\n");
printf("1) 직원 번호 검색\n");
printf("2) 직원 이름 검색\n");
printf("3) 직원 E_Mail 검색\n");
printf("Q) 종료\n");
scanf("%c", &cNum);
fflush(stdin);
if(cNum > 96 && cNum < 123 ) // 대. 소문자 정리
{ cNum = cNum - 32; }
switch(cNum)
{
case '1':
Worker_SearchID(Head); // 직원 번호
break;
case '2':
Worker_Search(Head); // 직원 이름
break;
case '3':
Worker_SearchMail(Head); // 직원 Mail
break;
case 'Q':
return;
default:
printf(" 잘못된 입력입니다. 다시 선택해주세요 \n");
break;
}
}
}
//------------------------------------------출력 ----------------------------------------//
void Worker_Print(Worker * Head)
{
Worker * Rear = Head;
printf("-----------------------------------------------------------------------\n");
printf("|직원번호\t|이름\t\t|E-Mail\t\t|PhoneNumber\t\t|\n");
printf("-----------------------------------------------------------------------\n");
while(Rear != 0)
{
printf("|%d\t\t", Rear->WorkerID);
printf("|%s\t\t", Rear->name);
printf("|%s\t\t", Rear->E_mail);
printf("|%s\t\t\n", Rear->Phone);
Rear = Rear->next;
}
}
//------------------------------------------삭제 ----------------------------------------//
Worker * Quit_Del(Worker * Head)
{
Worker * Rear = Head;
while(Head != 0)
{
Rear = Head->next;
free(Head);
Head = Rear;
}
return Head;
}
Worker * ALL_Date(Worker * Head)
{
Worker * Rear;
char YES = 0;
printf("삭제된 데이터는 복구할 수 없습니다. 모두 삭제하시겠습니까? (Y or N)");
while(1)
{
scanf("%c", &YES);
fflush(stdin);
if(YES > 96 && YES < 123 )
{ YES = YES - 32; }
if(YES == 'Y') //선택 목록 삭제
{
while(Head != 0)
{
Rear = Head->next;
free(Head);
Head = Rear;
}
printf("삭제가 완료 되었습니다 \n");
break;
}
else
{
printf("삭제를 취소 합니다 \n");
break;
}
}
return Head;
}
Worker * Select_Del(Worker * Head)
{
int Success = 0;
char YES = 0;
Worker * Front = Head;
Worker * Rear = Head;
char Buffer[50];
printf("검색할 이름 입력:: ");
scanf("%s", Buffer);
fflush(stdin);
while(Rear != 0)
{
if( 0 == strcmp( Rear->name, Buffer ) ) //사전정렬
{
printf("찾았습니다!! \n");
printf("-----------------------------------------------------------------------\n");
printf("|직원번호\t|이름\t\t|E-Mail\t\t|PhoneNumber\t\t|\n");
printf("-----------------------------------------------------------------------\n");
printf("|%d\t", Rear->WorkerID);
printf("|%s\t\t", Rear->name);
printf("|%s\t\t", Rear->E_mail);
printf("|%s\t\t\n", Rear->Phone);
Success = 1;
break;
}
Front = Rear;
Rear = Rear->next;
}
if( Success ==0)
{
printf("검색에 실패 하였습니다. 조건을 정확히 입력 해주세요 \n");
}
else
{
printf("검색에 성공하였습니다. 해당 목록을 삭제하시겠습니까? (Y or N)");
while(1)
{
scanf("%c", &YES);
fflush(stdin);
if(YES > 96 && YES < 123 )
{ YES = YES - 32; }
if(YES == 'Y') //선택 목록 삭제
{
if(Rear == Head)
{
Head = Rear->next;
}
else
{
Front->next = Rear->next;
}
free(Rear);
printf("삭제가 완료 되었습니다 \n");
break;
}
else
{
printf("선택 삭제를 취소 합니다 \n");
break;
}
}
}
return Head;
}
Worker * Delete_Menu(Worker * Head) // 삭제 메뉴
{
char cNum = 0;
while(1)
{
printf("-----------------삭제 메뉴 ----------\n");
printf("1) 전체삭제 \n");
printf("2) 선택삭제 \n");
printf("Q) 뒤로가기 \n");
scanf("%c", &cNum);
fflush(stdin);
if(cNum > 96 && cNum < 123 )
{ cNum = cNum - 32; }
if( cNum == '1')
{
Head=ALL_Date(Head);
}
else if( cNum == '2')
{
Head=Select_Del(Head);
}
else if( cNum == 'Q')
{
break;
}
else
{
printf("잘못된 입력입니다 다시 입력해주세요! \n");
}
}
return Head;
}
//----------------------------------------ㅡ main -----------------------------------------------------------//
int main()
{
//char cNum = 0;
char cNum[5] = {0,};
Worker * Head = 0;
int iCnt = 0;
while(1)
{
printf("--------menu------\n");
printf("1) 입력\n");
printf("2) 출력\n");
printf("3) 검색\n");
printf("4) 삭제\n");
printf("Exit) 종료 ( 대소문자 구분 없음) \n");
scanf("%s", cNum);
fflush(stdin);
for(iCnt = 0;cNum[iCnt] != 0;++iCnt)
{
if(cNum[iCnt] > 96 && cNum[iCnt] < 123 ) // 대. 소문자 정리
{ cNum[iCnt] = cNum[iCnt] - 32; }
}
if(0 == strcmp(cNum, "1") )
{
Head = InsertWorker(Head);
}
else if(0 == strcmp(cNum , "2") )
{
Worker_Print(Head);
}
else if(0 == strcmp(cNum , "3") )
{
Ser_Menu(Head);
}
else if(0 == strcmp (cNum , "4") )
{
Head=Delete_Menu(Head);
}
else if(0 == strcmp (cNum ,"EXIT") )
{ Head=Quit_Del(Head);
exit(0);
}
else
{
printf(" 잘못된 입력입니다. 다시 선택해주세요 \n");
}
}
return 0;
}