Test/C

전화번호부 v2.0 : 파일을 통해 관리하는 전화번호부

kiostory 2018. 12. 30. 15:22


phonebook.c


phonebook.exe



* 전화번호를 저장하고 보여주는 프로그램 작성

- 프로그램 실행시 사용법이 안내되고

- 전화번호 저장 파일(D:\phonebook.txt)이 메모리로 로딩된다. 시작전에 해당 경로에 파일하나 만들어놓고 시작.

- status, add, delete, find, search, load, save 전역함수 사용

- 전화번호를 추가하면 이름 첫글자순으로 정렬한다.

- 전화번호를 삭제하면 빈 메모리공간은 이후 데이터를 하나씩 당겨서 메꾼다.

- 중복제거 기능은 없다.


-------------------------------------------------------------------------------------------


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


#define CAPACITY 100
#define BUFFER_SIZE 20

char * names[CAPACITY];          // names
char * numbers[CAPACITY];        // phone numbers
int n = 0;                         // number of people in phone directory




int search(char *name)     // 매칭되는 이름을 찾아 index값을 반환하는 함수
{
 int i = 0;
 for (i = 0; i < n; i++)
 {
  if (strcmp(name, names[i]) == 0)
  {
   return i;
  }
 }
 return -1;
}




void add()        // 전화번호 추가 명령
{

 char buf1[BUFFER_SIZE], buf2[BUFFER_SIZE];        //지역변수들
 scanf_s("%s", buf1, sizeof(buf1));
 scanf_s("%s", buf2, sizeof(buf2));

 int i = n - 1;


 while (i >= 0 && strcmp(names[i], buf1) > 0)
 {
  names[i + 1] = names[i];
  numbers[i + 1] = numbers[i];
  i--;

 }


 names[i + 1] = strdup(buf1);
 numbers[i + 1] = strdup(buf2);

 n++;


 printf("%s was added successfully.\n\n", buf1);

}




void find()                      // 전화번호 찾기 명령

 char buf[BUFFER_SIZE];        //지역변수들
 scanf_s("%s", buf, sizeof(buf));

 int index = search(buf);
 if (index == -1)
  printf("No person named '%s' exists.(find)\n", buf);
 else
  printf("%s\n\n", numbers[index]);
}




void status()                    // 전화번호 저장 리스트를 보여주는 명령
{
 int i = 0;
 for (i = 0; i < n; i++)
  printf("%s %s\n", names[i], numbers[i]);
 printf("\nTotal %d persons.\n\n", n);
}




void del()                              // 전화번호 삭제 명령
{

 char buf[BUFFER_SIZE];
 scanf_s("%s", buf, sizeof(buf));

 int index = search(buf);
 

 if (index == -1)
 {
  printf("No person named '%s' exists.\n\n", buf);
  return;
 }

 int j = 0;
 for (j=index; j < n - 1; j++)
 {
  names[j] = names[j + 1];
  numbers[j] = numbers[j + 1];
 }

 n--;
 printf("'%s' was deleted successfully. \n\n", buf);
}




void load()                           // 전화번호 초기 로딩 함수
{
 
 char buf1[BUFFER_SIZE];
 char buf2[BUFFER_SIZE];


 FILE *fp = fopen("d:\phonebook.txt", "r");

 if (fp == NULL)
 {
  printf("!! File Open Failed. There's no file 'phonebook.txt' in d:\.\n\n");
  return;
 }

 while ((fscanf_s(fp, "%s", buf1, sizeof(buf1)) != EOF))
 {
  fscanf_s(fp, "%s", buf2, sizeof(buf2));
  names[n] = strdup(buf1);
  numbers[n] = strdup(buf2);
  n++;
 }
 printf("This 'Phonebook' program needs file 'phonebook.txt' in d:\.\n");
 printf("The data type in this file is like this.\n");
 printf("[name] [phone-number]\n\n");
 printf("Phonebook loaded successfully to memory.\n\n");
 fclose(fp);
}




void save()                           // 전화번호 저장 명령
{
 int i = 0;
 

 FILE *fp = fopen("d:\phonebook.txt", "w");

 if (fp == NULL)
 {
  printf("!! File Open Failed. There's no file 'phonebook.txt' in d:\.\n\n");
  return;
 }

 for (i = 0; i < n; i++)
 {
  fprintf_s(fp, "%s %s\n", names[i], numbers[i]);
 }
 printf("Data saved successfully to 'phonebook.txt' in d:\.\n\n");
 fclose(fp);
}




int main(void)                 // 메인 함수
{
 printf("---------------------\n");
 printf("★ Phone Book V2.0 ★\n");
 printf("---------------------\n\n");
 printf("    usage) status                           --> list phonebook\n");
 printf("           add [name] [phone-number]        --> add phonenumber\n");
 printf("           delete [name]                    --> delete phonenumber\n");
 printf("           find [name]                      --> find phonenumber\n");
 printf("           save                             --> save phonebook\n\n");
 load();


 char command[BUFFER_SIZE];

 while (1)
 {
  printf("$ ");
  scanf_s("%s", command,sizeof(command));

  if (strcmp(command, "add") == 0)
   add();
  else if (strcmp(command, "find") == 0)
   find();
  else if (strcmp(command, "status") == 0)
   status();
  else if (strcmp(command, "delete") == 0)
   del();
  else if (strcmp(command, "save") == 0)
   save();
  else if (strcmp(command, "exit") == 0)
   break;

 }

 return 0;
}


phonebook.c
0.0MB
phonebook.exe
0.04MB