Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.16 KB | None | 0 0
  1. // 헤더파일
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7.  
  8. //배열 크기
  9. #define MAX 100
  10.  
  11. //구조체 생성
  12. typedef struct card_struct{
  13.     char name[16];
  14.     char company[16];
  15.     char tel[16];
  16.     char position[16];
  17. }Card;
  18.  
  19. //명함 구초체 배열
  20. Card card[MAX];
  21. //명함 배열 갯수
  22. int card_index = 0;
  23. //이진검색 결과
  24. Card result_card;
  25. //파일 쓰기, 불러오기
  26. int write_file, read_file;
  27. //임시 문자열 저장공간
  28. char temp_string[16];
  29. //파일 이름
  30. char filename[]= "card.txt";
  31.  
  32. void showMenu(); // 메뉴 보여주기
  33. void showAllCards(); // 모든 명함 출력
  34. void inputCard(); // 명함 추가
  35. void searchCard(); // 명함 검색
  36. void deleteCard(); // 명함 삭제
  37. void updateCard(); //명함 수정
  38. void sortCard(Card * data, int left, int right); // 명함 정렬
  39. int binSrch(Card* scope, const int len, char* find); / / 명함 이진 탐색
  40. char * deleteSpace(char * string, int size); // 공백문자 제거
  41. char * inputSpace(char * string, int length, int size); // 공백문자 삽입
  42.  
  43. main()
  44. {
  45.     char string[20];
  46.     //파일이 없으면 생성
  47.     write_file = open(filename, O_WRONLY | O_CREAT, 0644);
  48.     close(write_file);
  49.     //파일 읽기
  50.     read_file = open(filename, O_RDONLY);
  51.     while (1) {
  52.         //16칸씩 읽음
  53.         if (read(read_file, card[card_index].name, 16) == 0) {
  54.             break;
  55.         }
  56.         read(read_file, card[card_index].company, 16);
  57.         read(read_file, card[card_index].tel, 16);
  58.         read(read_file, card[card_index].position, 16);
  59.         lseek(read_file, (off_t) 1, SEEK_CUR);
  60.         //공백 문자 제거
  61.         deleteSpace(card[card_index].name, 16);
  62.         deleteSpace(card[card_index].company, 16);
  63.         deleteSpace(card[card_index].tel, 16);
  64.         deleteSpace(card[card_index].position, 16);
  65.         card_index++;
  66.     }
  67.     close(read_file);
  68.     //파일 닫기
  69.    
  70.     while (1) {
  71.         //메뉴 화면
  72.         showMenu();
  73.         scanf("%s", string);
  74.         if(strcmp(string, "입력") == 0)
  75.             inputCard();
  76.         else if(strcmp(string, "출력") == 0)
  77.             showAllCards();
  78.         else if(strcmp(string, "검색") == 0)
  79.             searchCard();
  80.         else if(strcmp(string, "수정") == 0)
  81.             updateCard();
  82.         else if(strcmp(string, "삭제") == 0)
  83.             deleteCard();
  84.         else if(strcmp(string, "정렬") == 0)
  85.         {
  86.             int i;
  87.             sortCard(card, 0, card_index-1);
  88.             //정렬 후 저장
  89.             write_file = open(filename, O_WRONLY | O_TRUNC);
  90.             for (i = 0; i<card_index; i++){
  91.                 write(write_file, inputSpace(card[i].name, strlen(card[i].name), 16), 16);
  92.                 write(write_file, inputSpace(card[i].company, strlen(card[i].company), 16), 16);
  93.                 write(write_file, inputSpace(card[i].tel, strlen(card[i].tel), 16), 16);
  94.                 write(write_file, inputSpace(card[i].position, strlen(card[i].position), 16), 16);
  95.                 write(write_file, "\n", 1);
  96.             }
  97.         }
  98.         else if(strcmp(string, "종료") == 0)
  99.             break;
  100.        
  101.     }
  102. }
  103.  
  104.  
  105. void showMenu() {
  106.     printf("\n명함 관리 프로그램\n");
  107.     printf("---------------------\n");
  108.     printf("입력\n");
  109.     printf("출력\n");
  110.     printf("검색\n");
  111.     printf("수정\n");
  112.     printf("삭제\n");
  113.     printf("정렬\n");
  114.     printf("종료\n");
  115.     fputs(" : ", stdout);
  116. }
  117.  
  118. // 명함 배열 출력
  119. void showAllCards() {
  120.     int i;
  121.     puts("Name\tCompany\tTel\tPosition");
  122.     for (i = 0; i < card_index; i++)
  123.     {
  124.         printf("%s\t%s\t%s\t%s\n", card[i].name, card[i].company, card[i].tel, card[i].position);
  125.     }
  126. }
  127.  
  128. // 명함 입력
  129. void inputCard() {
  130.     fputs("저장할 이름 입력 : ", stdout);
  131.     scanf("%s", card[card_index].name);
  132.     fputs("저장할 회사 입력 : ", stdout);
  133.     scanf("%s", card[card_index].company);
  134.     fputs("저장할 전화번호 입력 : ", stdout);
  135.     scanf("%s", card[card_index].tel);
  136.     fputs("저장할 직급 입력 : ", stdout);
  137.     scanf("%s", card[card_index].position);
  138.  
  139.     //파일을 내용추가 쓰기모드로 열기
  140.     write_file = open(filename, O_WRONLY | O_APPEND, 0644);
  141.     //포인터를 파일의 끝에 맞춤
  142.     lseek(write_file, 0, SEEK_END);
  143.     //파일 쓰기 (16칸 중 비는 공간은 스페이스바 ' ' 로 대체)
  144.     write(write_file, inputSpace(card[card_index].name, strlen(card[card_index].name), 16), 16);
  145.     write(write_file, inputSpace(card[card_index].company, strlen(card[card_index].company), 16), 16);
  146.     write(write_file, inputSpace(card[card_index].tel, strlen(card[card_index].tel), 16), 16);
  147.     write(write_file, inputSpace(card[card_index].position, strlen(card[card_index].position), 16), 16);
  148.     write(write_file, "\n", 1);
  149.     close(write_file);
  150.     card_index++;
  151. }
  152.  
  153.  
  154. void searchCard() {
  155.     char name[30];
  156.     fputs("검색할 이름 입력 : ", stdout);
  157.     scanf("%s", name);
  158.     //이진검색 성공시
  159.     if(binSrch(card, card_index, name))
  160.         printf("이름:%s 회사:%s 전화번호:%s 직급:%s\n", result_card.name, result_card.company, result_card.tel, result_card.position);
  161.     else
  162.         printf("검색할 이름이 존재하지 않습니다.\n");
  163.  
  164. }
  165.  
  166.  
  167. void deleteCard() {
  168.     int i, j;
  169.     char name[30];
  170.     write_file = open(filename, O_WRONLY | O_TRUNC);
  171.     fputs("삭제할 이름 입력 : ", stdout);
  172.     scanf("%s", name);
  173.     for (i = 0; i < card_index; i++)
  174.     {
  175.         //삭제할 명함과 이름이 같으면
  176.         if (strcmp(name, card[i].name) == 0) {
  177.             //그 명함 다음부터 한 칸씩 앞으로 땡김
  178.             for (j = i; j < card_index; j++)
  179.             {
  180.                 card[j] = card[j + 1];
  181.             }
  182.             card_index--;
  183.             //파일 쓰기
  184.             for (i = 0; i<card_index; i++){
  185.                 write(write_file, inputSpace(card[i].name, strlen(card[i].name), 16), 16);
  186.                 write(write_file, inputSpace(card[i].company, strlen(card[i].company), 16), 16);
  187.                 write(write_file, inputSpace(card[i].tel, strlen(card[i].tel), 16), 16);
  188.                 write(write_file, inputSpace(card[i].position, strlen(card[i].position), 16), 16);
  189.                 write(write_file, "\n", 1);
  190.             }
  191.             return;
  192.         }
  193.     }
  194.     close(write_file);
  195.     printf("삭제할 이름이 존재하지 않습니다.\n");
  196. }
  197.  
  198.  
  199. void updateCard() {
  200.     int i;
  201.     char name[30];
  202.     write_file = open(filename, O_WRONLY);
  203.     fputs("검색할 이름 입력 : ", stdout);
  204.     scanf("%s", name);
  205.     for (i = 0; i < card_index; i++)
  206.     {
  207.         //명함 이름이 같으면
  208.         if (strcmp(name, card[i].name) == 0) {
  209.             //포인터는 해당 명함 줄 위치에 위치 '\n' 고려하여 i만큼 더함
  210.             lseek(write_file, i * 16 * 4 + i, SEEK_SET);
  211.             fputs("수정할 이름 : ", stdout);
  212.             scanf("%s", card[i].name);
  213.             fputs("수정할 회사 : ", stdout);
  214.             scanf("%s", card[i].company);
  215.             fputs("수정할 전화번호 : ", stdout);
  216.             scanf("%s", card[i].tel);
  217.             fputs("수정할 직급 : ", stdout);
  218.             scanf("%s", card[i].position);
  219.             write(write_file, inputSpace(card[i].name, strlen(card[i].name), 16), 16);
  220.             write(write_file, inputSpace(card[i].company, strlen(card[i].company), 16), 16);
  221.             write(write_file, inputSpace(card[i].tel, strlen(card[i].tel), 16), 16);
  222.             write(write_file, inputSpace(card[i].position, strlen(card[i].position), 16), 16);
  223.             write(write_file, "\n", 1);
  224.             return;
  225.         }
  226.     }
  227.     close(write_file);
  228.     printf("수정할 이름이 존재하지 않습니다.\n");
  229. }
  230.  
  231. char * inputSpace(char * string, int length, int size)
  232. {
  233.     //문자열 길이 이외에 남은 공간을 공백으로 채움
  234.     int i;
  235.     for(i = 0; i < size; i++)
  236.     {
  237.         if(i < length)
  238.         {
  239.             temp_string[i] = string[i];
  240.         }
  241.         else
  242.             temp_string[i] = ' ';
  243.     }
  244.     return temp_string;
  245. }
  246.  
  247. char * deleteSpace(char * string, int size)
  248. {
  249.     //문자열에 있는 공백을 널로 대체
  250.     int i;
  251.     for(i = 0; i < size; i++)
  252.         if(string[i] == ' ')
  253.         {
  254.             string[i] = '\0';
  255.         }
  256.     return string;
  257. }
  258.  
  259. int binSrch(Card* scope, const int len, char* find)
  260. {
  261.     int start = 0;
  262.     int end = len;
  263.     int i;
  264.     Card temp_Card[MAX];
  265.     //임시 명함 배열에 현재 명함 배열을 복사
  266.     for(i = 0; i < card_index; i++)
  267.     {
  268.         strcpy(temp_Card[i].name, scope[i].name);
  269.         strcpy(temp_Card[i].company, scope[i].company);
  270.         strcpy(temp_Card[i].tel, scope[i].tel);
  271.         strcpy(temp_Card[i].position, scope[i].position);
  272.     }
  273.     //임시 명함 배열 정렬
  274.     sortCard(temp_Card, 0, card_index-1);
  275.  
  276.     //이진 검색 ( 중간값을 기준으로 검색함)
  277.     while (start < end) {
  278.         int middle = (start + end) / 2;
  279.         int comp = strcmp(temp_Card[middle].name, find);
  280.         if (comp < 0 ) {
  281.             start = middle + 1;
  282.         }
  283.         else if (comp > 0) {
  284.             end = middle;
  285.         }
  286.         else {
  287.             //이름이 일치하면 결과 명함 구조체에 결과물 복사
  288.             strcpy(result_card.name, temp_Card[middle].name);
  289.             strcpy(result_card.company, temp_Card[middle].company);
  290.             strcpy(result_card.tel, temp_Card[middle].tel);
  291.             strcpy(result_card.position, temp_Card[middle].position);
  292.             return 1;
  293.         }
  294.     }
  295.       return 0;
  296. }
  297.  
  298. //퀵정렬 사용
  299. //기준값보다 작은 값은 앞으로 큰 값은 뒤로 보내는 방식 처리시간이 짧은게 특징.
  300. void sortCard(Card * data, int left, int right)
  301. {
  302.     int i, j;
  303.     Card x;
  304.     Card temp;
  305.  
  306.     i = left;
  307.     j = right;
  308.     x = data[(left+right)/2];
  309.  
  310.     do {
  311.         while((strcmp(data[i].name,x.name) < 0) && (i < right)) {
  312.             i++;
  313.         }
  314.         while((strcmp(data[j].name,x.name) > 0) && (j > left)) {
  315.             j--;
  316.             }
  317.         if(i <= j) {
  318.             temp = data[i];
  319.             data[i] = data[j];
  320.             data[j] = temp;
  321.             i++;
  322.             j--;
  323.         }
  324.     } while(i <= j);
  325.  
  326.     if(left < j) {
  327.         sortCard(data, left, j);
  328.     }
  329.     if(i < right) {
  330.         sortCard(data, i, right);
  331.     }
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement