IT_Programming/C · C++

[C_구조체 동적할당] 콘솔 기반 고객 관리 프로그램 (memmove() 함수 사용)

JJun ™ 2009. 6. 17. 17:11

 

================================================================================================

작성자 : creazier (블로그 주인장)

   목적  : 구조체 동적 할당 연습

================================================================================================

 

[ 실행화면 ]

 

 

 

 

 

: 대충 이런 기능을 합니다...쩝...-_-a

 

================================================================================================

 

[소스 코드]

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

 

#define SIZE   20
#define SAFE_FREE(p) if(p) { free(p); p=NULL; }

 

typedef struct
{
     char name[20];
     int age;
     char phone[20];
} customer; // struct 사용자 타입 선언

 

void menu_print();
void insert_info(customer *cus, int *location);
void delete_info(customer *cus, int *location, char *name);
void search_info(customer *cus, int *location, char *name);
void printList(customer *cus, int *location);

 

int main(void)
{
     int menu_num, index_location = 0;                                 // index_location : 추가할 인덱스의 위치
     char *name = (char *)malloc(sizeof(char) * SIZE);         // 이름을 입력 받을 동적 메모리 할당

    

      // struct 사용자 정의 타입 동적 메모리 할당
     customer *cus = (customer *)malloc(sizeof(customer) * SIZE);

 

     while(1)
     {
          menu_print();
          scanf("%d", &menu_num);

          

          switch(menu_num) // 입력된 메뉴별 실행
          {
                   case 1  : insert_info(cus, &index_location);  break;
                   case 2  : delete_info(cus, &index_location, name); break;
                   case 3  : search_info(cus, &index_location, name); break;
                   case 4  : printList(cus, &index_location);   break;
                   case 5  : printf("종료합니다.\n\n");    exit(0);
                   default : printf("메뉴 번호를 확인하세요\n\n");  break;
          }
     }

 

     SAFE_FREE(name); // 동적 메모리 해제
     SAFE_FREE(cus);  // 동적 메모리 해제

 

     return 0;
}

 

void menu_print() // 메뉴 출력
{
     printf("\n*****************************************\n");
     printf("* 1. 입력 \n");
     printf("* 2. 삭제 \n");
     printf("* 3. 검색 \n");
     printf("* 4. 목록 출력 \n");
     printf("* 5. 종료 \n");
     printf("*****************************************\n");
     printf("메뉴 번호를 입력하세요 : ");
}

 

void insert_info(customer *cus, int *location) // 데이터 삽입
{
     printf("\n[입력 메뉴]\n추가할 정보를 입력하세요\n");
 
     if(*location < SIZE)
     {
          printf("  이름 : "); scanf(" %s", &cus[*location].name);
          printf("  나이 : "); scanf("%d", &cus[*location].age);
          printf("연락처 : "); scanf(" %s", &cus[*location].phone);
          printf("%s님의 정보가 추가되었습니다.\n", cus[*location].name);
          (*location)++;
     }
}

 

void delete_info(customer *cus, int *location, char *name) // 데이터 삭제
{

     int i, flag = 0;
 
     if(*location > 0)
     {
          printf("[삭제 메뉴]\n삭제할 정보를 입력하세요\n");
          printf("  이름 : ");

          memset(name, '\0', SIZE); 
          fflush(stdin); // 키보드 입력 버퍼 비우기
          fgets(name, SIZE, stdin);
          *(name + (strlen(name) - 1)) = '\0';

//       scanf(" %s", name);

 

          for(i=0; i<SIZE; ++i)
          {
                   if(!strcmp(cus[i].name, name))
                   {
                        printf("%s님의 정보가 삭제 되었습니다.\n", cus[i].name);

 

                        // 메모리 공간을 앞으로 땡겨서 삭제!

                        memmove(cus + i, cus + i + 1, sizeof(cus)*SIZE - i); 

                        (*location)--;
                        flag = 1;
                   }
          } 

  

          if(flag == 0)
          {
                   printf("삭제할 데이터가 없습니다.\n");
          }
     }
     else
     {
              printf("삭제할 데이터가 없습니다.\n");
     }


     printf("\n");
}

 

void search_info(customer *cus, int *location, char *name) // 데이터 검색
{
     int i;
     printf("\n이름을 입력하세요 : ");

    

     memset(name, '\0', SIZE); 
     fflush(stdin); // 키보드 입력 버퍼 비우기
     fgets(name, SIZE, stdin);
     *(name + (strlen(name) - 1)) = '\0';

//  scanf(" %s", name);

 

     printf("\n******* [검색 결과] *******\n");


     for(i=0; i<(*location); ++i)
     {        
          if(!strcmp(cus[i].name, name))
          {
               printf(" %5s   %5d   %5s\n", cus[i].name, cus[i].age, cus[i].phone);
          }

     }
     printf("***************************\n\n");
}

 

void printList(customer *cus, int *location) // 데이터 리스트 출력
{
     int i;

     

     if(*location > 0)
     {
          printf("\n[목록출력]\n");
          printf("----------------------------------\n");
          printf("    이름      나이       연락처   \n");
          printf("----------------------------------\n");

          for(i=0; i<(*location); ++i)
          {
                printf("[%d] %5s %7d     %7s\n", i+1, cus[i].name, cus[i].age, cus[i].phone);
          }
     }
     else
     {
          printf("데이터가 없습니다.\n");
     }
 

    printf("\n");
}

 

================================================================================================