[C++] 전화번호부

chxxrin·2024년 7월 25일
0

C++

목록 보기
18/22

PhoneBook.h

/*
    홍정모 연구소 https://honglab.co.kr/
*/

#ifndef PHONE_BOOK_H_
#define PHONE_BOOK_H_

const int kMaxStr = 20; // 문자열의 최대 글자 수

struct Contact
{
    char name[kMaxStr];
    char phone[kMaxStr];
};

class PhoneBook
{
public:
    PhoneBook();
    ~PhoneBook();

    bool IsEmpty();
    bool IsFull();
    void PrintAll();
    void PrintContact(int i);
    void AddContact();
    void AddContact(const char name[], const char phone[]);
    int FindByName();
    bool IsEqual(const char str1[], const char str2[]);
    void DeleteByName();

private:
    int capacity_ = 3; // 연락처 최대 개수 (변경 가능)
    int num_contacts_ = 0;
    Contact *contacts_ = nullptr;
};

#endif

PhoneBook.cpp

/*
    홍정모 연구소 https://honglab.co.kr/
*/

#include "PhoneBook.h"

#include <cassert>
#include <iostream>
#include <cstring> // memcpy(.)

using namespace std;

PhoneBook::PhoneBook()
{
    contacts_ = new Contact[capacity_];
}

PhoneBook::~PhoneBook()
{
    if (contacts_)
        delete[] contacts_;
}

bool PhoneBook::IsEmpty()
{
    assert(num_contacts_ >= 0);

    if (num_contacts_ == 0)
        return true;
    else
        return false;
}

bool PhoneBook::IsFull()
{
    if (num_contacts_ == capacity_)
        return true;
    else
        return false;
}

void PhoneBook::PrintAll()
{
    for (int i = 0; i < num_contacts_; i++)
    {
        PrintContact(i);
    }
}

void PhoneBook::PrintContact(int index)
{
    cout << index << " ";
    cout << contacts_[index].name;
    cout << ", " << contacts_[index].phone << endl;
}

void PhoneBook::AddContact(const char name[], const char phone[])
{
    assert(!IsFull()); // 디버깅할 때 assert 괄호 안쪽 조건이 false가 되면 오류 처리

    memcpy(contacts_[num_contacts_].name, name, sizeof(contacts_[num_contacts_].name));
    memcpy(contacts_[num_contacts_].phone, phone, sizeof(contacts_[num_contacts_].phone));

    num_contacts_ += 1;
}

void PhoneBook::AddContact()
{
    // capacity_가 고정된 경우
    if (IsFull())
    {
        cout << "더 이상 추가할 수 없습니다." << endl;
        return;
    }

    // 더 해볼 것: 메모리를 재할당 받아서 연락처 개수 제한 없애기

    char new_name[kMaxStr];
    char new_phone[kMaxStr];

    cout << "이름을 입력해주세요 : ";
    cin.getline(new_name, sizeof(new_name));

    cout << "전화번호를 입력해주세요 : ";
    cin.getline(new_phone, sizeof(new_phone));

    AddContact(new_name, new_phone);
}

int PhoneBook::FindByName()
{
    char search_name[kMaxStr];

    cout << "검색할 이름을 입력해주세요 : ";
    cin.getline(search_name, sizeof(search_name));

    for (int i=0; i< num_contacts_; i++)
    {
        if(IsEqual(search_name, contacts_[i].name))
        {
            PrintContact(i);
            return i;
        }
    }

    cout << search_name << " 님을 찾지 못했습니다." << endl;

    return -1;
}

bool PhoneBook::IsEqual(const char str1[], const char str2[])
{
    for (int i = 0; i < kMaxStr; i++)
    {
        if (str1[i] != str2[i])
            return false;

        if (str1[i] == '\0')
            return true;
    }

    return true;
}

void PhoneBook::DeleteByName()
{
    // 삭제할 때 메모리를 줄이지는 않는 것으로 할께요.

    int index = FindByName();

    if (index >= 0)
    {
        // TODO: 중간에서 삭제했을 경우 데이터 정리

        if(index >= 0)
        {
            for(int i=index+1; i< num_contacts_; i++)
            {
                memcpy(&contacts_[i-1], &contacts_[i], sizeof(Contact));
            }
        }
        // TODO: num_contacts_ 하나 감소

        num_contacts_ -= 1;
    }
}

삭제

  • 데이터를 삭제한다기보다, 그 뒤의 데이터를 지우고자하는 데이터가 있는 자리로 복사를 하고, num_contacts_를 하나 감소시킴으로써 원래 numcontacts가 4개에서 3개로 줄어듦에의해 삭제된 것처럼 보이게 하는것이다.

출처 : 홍정모 유튜브

0개의 댓글

관련 채용 정보