[C 프로그래밍] 주소록 프로그램 만들기

2019. 6. 13. 01:19coding

[헤더파일 main.h]

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

typedef struct _address{
    char name[30];
    char phone[30];
    struct _address *next; //linked list 로 만들기 위해서 자기참조구조체 만들기
} Address;

extern Address*head;
extern Address*tail; //main.c 에 있는 전역변수 extern으로 가져오기

void add_address(void);
void pw_check(void);
void all_print(void);
void personal_print(void);
void save_data(void);
void init(void);
[main.c]

#include "main.h"

//head -> data -> data -> tail 이런식으로 linked
Address *head = NULL;
Address *tail = NULL;

void pw_check(){
    static int pw_count = 0;
    char pw[] = "qwer1234";
    char pw_input[10];
    printf("비밀번호(%d회 실패): ", pw_count);
    if(pw_count < 3){
        scanf("%s", pw_input);
        if(strcmp(pw, pw_input) == 0)
            return;
        else {
            pw_count++;
            pw_check();
        }
    } else {
        printf("등록할 수 없음!\n 프로그램을 종료합니다.");
        exit(0);
    }
}

void menu(){
    int choice;
    do {
        printf("전화번호 관리\n");
        printf("1.등록 2.전체검색 3.특정인검색 4.종료\n");
        printf("메뉴 선택: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                add_address();
                break;
            case 2:
                all_print();
                break;
            case 3:
                personal_print();
                break;
            case 4:
                printf("프로그램을 종료합니다.");
                exit(0);
            default:
                break;
        }
    } while(choice != 4);
}

int main(){
    init();
    menu();
    return 0;
}
[register.c]

#include "main.h"

void add_address(){
    pw_check();
    char name[20];
    char phone[20];
    printf("등록할 이름: ");
    scanf("%s", name);
    printf("전화번호: ");
    scanf("%s", phone);
    Address *new = (Address *)malloc(sizeof(Address));
    strcpy(new->name, name);
    strcpy(new->phone, phone);
    printf("%s 정보 등록 완료!\n", name);
    
    new->next = NULL;
    if(head == NULL){
        head = new;
        tail = new;
    } else {
        tail->next = new;
        tail = new;
    }
    save_data();
}
[allprint.c]

#include "main.h"

void all_print(){
    Address *curr = head;
    printf("<<전화번호 목록>>\n");
    while(curr != NULL){
        printf("%s ", curr->name);
        printf("%s\n", curr->phone);
        curr = curr->next;
    }
}
[personalprint.c]

#include "main.h"

void personal_print(){
    Address *curr = head;
    char name_search[30];
    printf("검색할 이름: ");
    scanf("%s", name_search);
    while(curr != NULL){
        if(strcmp(curr->name, name_search) == 0){
            printf("%s %s\n", curr->name, curr->phone);
            break;
        }
        else
            curr = curr->next;
    }
}
[init.c]

#include "main.h"

void init_link(Address new){
    Address * data = (Address *)malloc(sizeof(Address));
    strcpy(data->name, new.name);
    strcpy(data->phone, new.phone);
    if(head == NULL){
        head = data;
        tail = data;
    } else {
        tail->next = data;
        tail = data;
    }
    printf("%s %s\n", new.name, new.phone);
}

void init(){
    Address new;
    FILE * fp;
    fp = fopen("/Users/yenny/Desktop/phonebook.bin", "rb");
    while(fread(&new, sizeof(Address), 1, fp) > 0){
        init_link(new);
    }
}
[savedata.c]

#include "main.h"

void save_data(){
    FILE * fp;
    fp = fopen("/Users/yenny/Desktop/phonebook.bin", "ab");
    fwrite(tail, sizeof(Address), 1, fp);
    fclose(fp);
}