Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 헤더파일
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- //배열 크기
- #define MAX 100
- //구조체 생성
- typedef struct card_struct{
- char name[16];
- char company[16];
- char tel[16];
- char position[16];
- }Card;
- //명함 구초체 배열
- Card card[MAX];
- //명함 배열 갯수
- int card_index = 0;
- //이진검색 결과
- Card result_card;
- //파일 쓰기, 불러오기
- int write_file, read_file;
- //임시 문자열 저장공간
- char temp_string[16];
- //파일 이름
- char filename[]= "card.txt";
- void showMenu(); // 메뉴 보여주기
- void showAllCards(); // 모든 명함 출력
- void inputCard(); // 명함 추가
- void searchCard(); // 명함 검색
- void deleteCard(); // 명함 삭제
- void updateCard(); //명함 수정
- void sortCard(Card * data, int left, int right); // 명함 정렬
- int binSrch(Card* scope, const int len, char* find); / / 명함 이진 탐색
- char * deleteSpace(char * string, int size); // 공백문자 제거
- char * inputSpace(char * string, int length, int size); // 공백문자 삽입
- main()
- {
- char string[20];
- //파일이 없으면 생성
- write_file = open(filename, O_WRONLY | O_CREAT, 0644);
- close(write_file);
- //파일 읽기
- read_file = open(filename, O_RDONLY);
- while (1) {
- //16칸씩 읽음
- if (read(read_file, card[card_index].name, 16) == 0) {
- break;
- }
- read(read_file, card[card_index].company, 16);
- read(read_file, card[card_index].tel, 16);
- read(read_file, card[card_index].position, 16);
- lseek(read_file, (off_t) 1, SEEK_CUR);
- //공백 문자 제거
- deleteSpace(card[card_index].name, 16);
- deleteSpace(card[card_index].company, 16);
- deleteSpace(card[card_index].tel, 16);
- deleteSpace(card[card_index].position, 16);
- card_index++;
- }
- close(read_file);
- //파일 닫기
- while (1) {
- //메뉴 화면
- showMenu();
- scanf("%s", string);
- if(strcmp(string, "입력") == 0)
- inputCard();
- else if(strcmp(string, "출력") == 0)
- showAllCards();
- else if(strcmp(string, "검색") == 0)
- searchCard();
- else if(strcmp(string, "수정") == 0)
- updateCard();
- else if(strcmp(string, "삭제") == 0)
- deleteCard();
- else if(strcmp(string, "정렬") == 0)
- {
- int i;
- sortCard(card, 0, card_index-1);
- //정렬 후 저장
- write_file = open(filename, O_WRONLY | O_TRUNC);
- for (i = 0; i<card_index; i++){
- write(write_file, inputSpace(card[i].name, strlen(card[i].name), 16), 16);
- write(write_file, inputSpace(card[i].company, strlen(card[i].company), 16), 16);
- write(write_file, inputSpace(card[i].tel, strlen(card[i].tel), 16), 16);
- write(write_file, inputSpace(card[i].position, strlen(card[i].position), 16), 16);
- write(write_file, "\n", 1);
- }
- }
- else if(strcmp(string, "종료") == 0)
- break;
- }
- }
- void showMenu() {
- printf("\n명함 관리 프로그램\n");
- printf("---------------------\n");
- printf("입력\n");
- printf("출력\n");
- printf("검색\n");
- printf("수정\n");
- printf("삭제\n");
- printf("정렬\n");
- printf("종료\n");
- fputs(" : ", stdout);
- }
- // 명함 배열 출력
- void showAllCards() {
- int i;
- puts("Name\tCompany\tTel\tPosition");
- for (i = 0; i < card_index; i++)
- {
- printf("%s\t%s\t%s\t%s\n", card[i].name, card[i].company, card[i].tel, card[i].position);
- }
- }
- // 명함 입력
- void inputCard() {
- fputs("저장할 이름 입력 : ", stdout);
- scanf("%s", card[card_index].name);
- fputs("저장할 회사 입력 : ", stdout);
- scanf("%s", card[card_index].company);
- fputs("저장할 전화번호 입력 : ", stdout);
- scanf("%s", card[card_index].tel);
- fputs("저장할 직급 입력 : ", stdout);
- scanf("%s", card[card_index].position);
- //파일을 내용추가 쓰기모드로 열기
- write_file = open(filename, O_WRONLY | O_APPEND, 0644);
- //포인터를 파일의 끝에 맞춤
- lseek(write_file, 0, SEEK_END);
- //파일 쓰기 (16칸 중 비는 공간은 스페이스바 ' ' 로 대체)
- write(write_file, inputSpace(card[card_index].name, strlen(card[card_index].name), 16), 16);
- write(write_file, inputSpace(card[card_index].company, strlen(card[card_index].company), 16), 16);
- write(write_file, inputSpace(card[card_index].tel, strlen(card[card_index].tel), 16), 16);
- write(write_file, inputSpace(card[card_index].position, strlen(card[card_index].position), 16), 16);
- write(write_file, "\n", 1);
- close(write_file);
- card_index++;
- }
- void searchCard() {
- char name[30];
- fputs("검색할 이름 입력 : ", stdout);
- scanf("%s", name);
- //이진검색 성공시
- if(binSrch(card, card_index, name))
- printf("이름:%s 회사:%s 전화번호:%s 직급:%s\n", result_card.name, result_card.company, result_card.tel, result_card.position);
- else
- printf("검색할 이름이 존재하지 않습니다.\n");
- }
- void deleteCard() {
- int i, j;
- char name[30];
- write_file = open(filename, O_WRONLY | O_TRUNC);
- fputs("삭제할 이름 입력 : ", stdout);
- scanf("%s", name);
- for (i = 0; i < card_index; i++)
- {
- //삭제할 명함과 이름이 같으면
- if (strcmp(name, card[i].name) == 0) {
- //그 명함 다음부터 한 칸씩 앞으로 땡김
- for (j = i; j < card_index; j++)
- {
- card[j] = card[j + 1];
- }
- card_index--;
- //파일 쓰기
- for (i = 0; i<card_index; i++){
- write(write_file, inputSpace(card[i].name, strlen(card[i].name), 16), 16);
- write(write_file, inputSpace(card[i].company, strlen(card[i].company), 16), 16);
- write(write_file, inputSpace(card[i].tel, strlen(card[i].tel), 16), 16);
- write(write_file, inputSpace(card[i].position, strlen(card[i].position), 16), 16);
- write(write_file, "\n", 1);
- }
- return;
- }
- }
- close(write_file);
- printf("삭제할 이름이 존재하지 않습니다.\n");
- }
- void updateCard() {
- int i;
- char name[30];
- write_file = open(filename, O_WRONLY);
- fputs("검색할 이름 입력 : ", stdout);
- scanf("%s", name);
- for (i = 0; i < card_index; i++)
- {
- //명함 이름이 같으면
- if (strcmp(name, card[i].name) == 0) {
- //포인터는 해당 명함 줄 위치에 위치 '\n' 고려하여 i만큼 더함
- lseek(write_file, i * 16 * 4 + i, SEEK_SET);
- fputs("수정할 이름 : ", stdout);
- scanf("%s", card[i].name);
- fputs("수정할 회사 : ", stdout);
- scanf("%s", card[i].company);
- fputs("수정할 전화번호 : ", stdout);
- scanf("%s", card[i].tel);
- fputs("수정할 직급 : ", stdout);
- scanf("%s", card[i].position);
- write(write_file, inputSpace(card[i].name, strlen(card[i].name), 16), 16);
- write(write_file, inputSpace(card[i].company, strlen(card[i].company), 16), 16);
- write(write_file, inputSpace(card[i].tel, strlen(card[i].tel), 16), 16);
- write(write_file, inputSpace(card[i].position, strlen(card[i].position), 16), 16);
- write(write_file, "\n", 1);
- return;
- }
- }
- close(write_file);
- printf("수정할 이름이 존재하지 않습니다.\n");
- }
- char * inputSpace(char * string, int length, int size)
- {
- //문자열 길이 이외에 남은 공간을 공백으로 채움
- int i;
- for(i = 0; i < size; i++)
- {
- if(i < length)
- {
- temp_string[i] = string[i];
- }
- else
- temp_string[i] = ' ';
- }
- return temp_string;
- }
- char * deleteSpace(char * string, int size)
- {
- //문자열에 있는 공백을 널로 대체
- int i;
- for(i = 0; i < size; i++)
- if(string[i] == ' ')
- {
- string[i] = '\0';
- }
- return string;
- }
- int binSrch(Card* scope, const int len, char* find)
- {
- int start = 0;
- int end = len;
- int i;
- Card temp_Card[MAX];
- //임시 명함 배열에 현재 명함 배열을 복사
- for(i = 0; i < card_index; i++)
- {
- strcpy(temp_Card[i].name, scope[i].name);
- strcpy(temp_Card[i].company, scope[i].company);
- strcpy(temp_Card[i].tel, scope[i].tel);
- strcpy(temp_Card[i].position, scope[i].position);
- }
- //임시 명함 배열 정렬
- sortCard(temp_Card, 0, card_index-1);
- //이진 검색 ( 중간값을 기준으로 검색함)
- while (start < end) {
- int middle = (start + end) / 2;
- int comp = strcmp(temp_Card[middle].name, find);
- if (comp < 0 ) {
- start = middle + 1;
- }
- else if (comp > 0) {
- end = middle;
- }
- else {
- //이름이 일치하면 결과 명함 구조체에 결과물 복사
- strcpy(result_card.name, temp_Card[middle].name);
- strcpy(result_card.company, temp_Card[middle].company);
- strcpy(result_card.tel, temp_Card[middle].tel);
- strcpy(result_card.position, temp_Card[middle].position);
- return 1;
- }
- }
- return 0;
- }
- //퀵정렬 사용
- //기준값보다 작은 값은 앞으로 큰 값은 뒤로 보내는 방식 처리시간이 짧은게 특징.
- void sortCard(Card * data, int left, int right)
- {
- int i, j;
- Card x;
- Card temp;
- i = left;
- j = right;
- x = data[(left+right)/2];
- do {
- while((strcmp(data[i].name,x.name) < 0) && (i < right)) {
- i++;
- }
- while((strcmp(data[j].name,x.name) > 0) && (j > left)) {
- j--;
- }
- if(i <= j) {
- temp = data[i];
- data[i] = data[j];
- data[j] = temp;
- i++;
- j--;
- }
- } while(i <= j);
- if(left < j) {
- sortCard(data, left, j);
- }
- if(i < right) {
- sortCard(data, i, right);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement