๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
C์ธ์ด๋ก ์ ํ๋ฒํธ๋ถ ๋ง๋ค๊ธฐ ๋ณธ๋ฌธ
๐ฉ๐ป IoT (Embedded)/C์ธ์ด
C์ธ์ด๋ก ์ ํ๋ฒํธ๋ถ ๋ง๋ค๊ธฐ
์ง์ง์ํ์นด 2023. 9. 17. 23:07728x90
๋ฐ์ํ
โ๏ธ ContactBook โ๏ธ
- ์ ํ๋ฒํธ๋ถ ํ๋ก๊ทธ๋จ์ ๊ฐ์๋ฅผ ๋ง๋ค๊ณ ํ๋ฉด๊ณผ ๋ฉ๋ด์ ๊ตฌ์ฑํ๋ฉฐ ๊ธฐ๋ฅ์ ํ์ฅํด๋ณด์!!!
๐๏ธ 2์กฐ์ ๊ท์น
1๏ธโฃ commit message
โ[Feat] : ๊ธฐ๋ฅ ์ถ๊ฐ ๐[Chore] : ์คํ ํ์ผ ๋ฐ ์์ ์ ๐ง[Refactor] : ์ฝ๋ ์์ (๋ฆฌํฉํ ๋ง) ๐[Memo] : ์ฃผ์ ์์ ๐จ[Alert] : ๊ธฐ๋ฅ ์ค๋ฅ ์โ
2๏ธโฃ ๊นํ๋ธ push ์ ๐จ ์๋ฆฌ๊ธฐ (์ถฉ๋ ๋ฐ์!! Pull ํ๊ณ ์์ ํ์ธ์!!)
3๏ธโฃ 1์ผ 1 issue ์์ฑ
4๏ธโฃ dev branch ์ 1์ฐจ Merge, main branch ์ ์ต์ข Merge
โ๏ธ ํ๋ก์ ํธ โ๏ธ
๐จ๐ป 1. ํ๋ก์ ํธ ๊ฐํธ ์ฐจํธ
- ContactBook ๊ฐ์์ง๊ธฐ (September 7, 2023)
- ContactBook ๊ฐ์ ๊ตฌ์ฒดํํ๊ธฐ (September 8, 2023)
- ํ๋ก๊ทธ๋๋ฐ ์์ํ๊ธฐ + ํ์๋ก ์์ฑํ๋ฉด์ ์งํ (September 9, 2023 ~ September 17, 2023)
- ContactBook ๋ฐํ ํ๊ธฐ (September 18, 2023)
๐จ๐ป 2. ์์ ์์
- function ๋ง๋ค๊ธฐ
#pragma once
// ๊ตฌ์กฐ์ฒด (์ด๋ฆ, ์ ํ๋ฒํธ, ์นดํ
๊ณ ๋ฆฌ, ์ฐ๊ฒฐ๋ฆฌ์คํธ ์ฃผ์)
typedef struct USERDATA
{
char szName[32];
char szPhone[32];
int category;
struct USERDATA* pNext;
} USERDATA;
// ํฐ ๋ฉ๋ด UI
void MainMenu(char* menu);
// ์ธ๋ถ ๋ฉ๋ด UI
void PrintAllUI();
void MainMenuUI();
void RemoveUI();
void CategoryUI();
void CreateUI();
void SearchUI();
// 1) ์ ํ๋ฒํธ๋ถ ์ถ๊ฐ
// - ์ค๋ณต ์ด๋ฆ ์ ํจ์ฑ ๊ฒ์ฌ
char* Find(USERDATA* Head, char* name);
// - ์๋ก์ด ์ ํ๋ฒํธ๋ถ ๊ตฌ์กฐ์ฒด ์์ฑ
USERDATA* Create(USERDATA** Head);
// - ์ฐ๊ฒฐ๋ฆฌ์คํธ์ ์ถ๊ฐ
USERDATA* Add(USERDATA** Head, USERDATA* NewNode);
// 2) ์ ํ๋ฒํธ๋ถ ๊ฒ์
void Search(USERDATA* Head);
// 3) ์ ํ๋ฒํธ๋ถ ์ ์ฒด ์ถ๋ ฅ
USERDATA* PrintAll(USERDATA* Head);
// 4) ์ ํ๋ฒํธ๋ถ ์ญ์
// - ์ญ์ ํ ์ด๋ฆ์ ์ฃผ์ ํ์ธ
USERDATA* SLL_GetNodeAt(USERDATA* Head);
// - ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ก ํด๋น ์ฃผ์ ์ญ์
void Remove(USERDATA** Head, USERDATA* Remove);
// - ์ญ์ ํ ๋
ธ๋ ๋ฉ๋ชจ๋ฆฌ ์๋ฉธ
void SLL_DestroyNode(USERDATA* Remove);
// 5) ์ ํ๋ฒํธ๋ถ ์์
void Update(USERDATA* Head);
// 6) ์ ํ๋ฒํธ๋ถ ์นดํ
๊ณ ๋ฆฌ๋ณ ์ถ๋ ฅ
void Category(USERDATA* Head);
// 7) ์ ํ๋ฒํธ๋ถ ์ฒ์ ์์์ ์ฃผ์ ์ฌํ ๋น
USERDATA* Reallocation();
// 8) ์ ํ๋ฒํธ๋ถ ์ข
๋ฃ์ ๋์ ํ ๋น ํด์ง
void Exit(USERDATA* Head);
- ํ์๋ก
- Tasks Board ์นธ๋ฐ๋ณด๋
๐จ๐ป 3. ๋ค์ด์ด๊ทธ๋จ

๐จ๐ป 4. ์์ค์ฝ๋ ๋ถ์
- โ๏ธ ์ ํ๋ฒํธ๋ถ ์ถ๊ฐ
- ์ค๋ณต ์ด๋ฆ ์ ํจ์ฑ ๊ฒ์ฌ
- char* Find(USERDATA* Head, char* name);
- ์ฒ์ ์ถ๊ฐํ ๋๋ ๋ฐ๋ก ์ด๋ฆ์ ๋ฆฌํดํ๋ค
- ์ด๋ฆ์ด ์ค๋ณต๋์์ผ๋ฉด ์ฌ๊ทํจ์๋ฅผ ํตํด ๋ค์ ์ด๋ฆ ๋ฐ๋๋ค
- while(1) ์ ํตํด null ๊น์ง ๊ฒ์ฌํ๊ณ , ์์ผ๋ฉด ๊ธฐ์กด ์ด๋ฆ์ ๋ฆฌํดํ๋ค
if ((Head) == NULL) { printf("์ถ๊ฐํ ์ด๋ฆ์ ์ ๋ ฅํ์ธ์:"); scanf("%s", &NameTemp); strcpy(name, NameTemp); return name; } else { printf("์ถ๊ฐํ ์ด๋ฆ์ ์ ๋ ฅํ์ธ์:"); scanf("%s", &NameTemp); strcpy(FindTemp, Current->szName); while (1) { if (*FindTemp == *NameTemp) { printf("์ค๋ณต๋ ์ด๋ฆ์ ๋๋ค. ๋ค์ ์ ๋ ฅํด์ฃผ์ธ์!\\n"); Find(Current, name); } if (Current->pNext != NULL) { break; } Current = Current->pNext; } strcpy(name, NameTemp); return name; }
- ์๋ก์ด ์ ํ๋ฒํธ๋ถ ๊ตฌ์กฐ์ฒด ์์ฑ
- USERDATA* Create(USERDATA** Head);
- ContactBook ์ด๋ผ๋ ๊ตฌ์กฐ์ฒด์ ์ด๋ฆ, ์ ํ๋ฒํธ, ์นดํ ๊ณ ๋ฆฌ, ๊ทธ๋ค์ ๋ ธ๋์ ํฌ์ธํฐ๋ null ๋ก ๋ฐ๋๋ค
USERDATA* ContactBook = (USERDATA*)malloc(sizeof(USERDATA)); char* name = (char*)malloc(sizeof(char)); char phone[32]; int num = 0; printf("%p\\n", name); Find(*Head, name); printf("%p", name); printf("์ด๋ฆ์ ๋ง๋ ์ ํ๋ฒํธ๋ฅผ ์ ๋ ฅํ์ธ์(010-0000-0000):"); scanf("%s", phone); printf("์นดํ ๊ณ ๋ฆฌ์ ๋ง๋ ์ซ์๋ฅผ ์ ๋ ฅํ์ธ์. ์น๊ตฌ[0], ๊ฐ์กฑ[1], ์ง์ฅ[2], ๊ธฐํ[3]:"); scanf("%d", &num); getchar(); strcpy(ContactBook->szName, name); strcpy(ContactBook->szPhone, phone); ContactBook->category = num; // ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ค. ContactBook->pNext = NULL; // ๋ค์ ๋ ธ๋์ ๋ํ ํฌ์ธํฐ๋ NULL๋ก ์ด๊ธฐํ ํ๋ค.
- ์ฐ๊ฒฐ๋ฆฌ์คํธ์ ์ถ๊ฐ
- USERDATA* Add(USERDATA** Head, USERDATA* NewNode);
- ์ฒ์ ์ฃผ์๋ฉด ๋ฐ๋ก head ์ ๋ถ์ธ๋ค
- tail ์ฃผ์๊ฐ null ์ผ ๋๊น์ง tail์ ์ด์ด์ ์๋ก์ด ๋ ธ๋๋ฅผ ๋ถ์ธ๋ค
if ((*Head) == NULL) { *Head = NewNode; } else { // ํ ์ผ์ ์ฐพ์ NewNode๋ฅผ ์ฐ๊ฒฐํ๋ค. USERDATA* Tail = (*Head); while (Tail->pNext != NULL) { Tail = Tail->pNext; } // ์ด์ ์ฃผ์ Temp = Tail; // ํ์ฌ ์ฃผ์ Tail->pNext = NewNode; }
- ์ค๋ณต ์ด๋ฆ ์ ํจ์ฑ ๊ฒ์ฌ
- โ๏ธ ์ ํ๋ฒํธ๋ถ ๊ฒ์
- void Search(USERDATA* Head);
- ๊ตฌ์กฐ์ฒด์ด๋ฆ๊ณผ ๊ฒ์ํ ์ด๋ฆ์ด ๊ฐ์ง ์์ผ๋ฉด, Current์ฃผ์๋ ๋ค์ ๊ตฌ์กฐ์ฒด๋ก ๋์ด๊ฐ๋ค
- ๊ตฌ์กฐ์ฒด ์ด๋ฆ๊ณผ ๊ฒ์ํ ์ด๋ฆ์ด ๊ฐ์ผ๋ฉด ์ถ๋ ฅํ๋ค.
USERDATA* Current = Head; char compareName[32]; scanf("%s", compareName); //Current->pNext๊ฐ ๋ณผ ํฌ์ธํฐ๊ฐ ์๊ณ ๊ตฌ์กฐ์ฒด์ด๋ฆ๊ณผ ๊ฒ์ํ ์ด๋ฆ์ด ๊ฐ์ง ์์ผ๋ฉด, Current์ฃผ์๋ ๋ค์ ๊ตฌ์กฐ์ฒด๋ก ๋์ด๊ฐ๋ค while (strcmp(Current->szName, compareName) != 0 && Current->pNext != NULL) { Current = Current->pNext; } //๊ตฌ์กฐ์ฒด ์ด๋ฆ๊ณผ ๊ฒ์ํ ์ด๋ฆ์ด ๊ฐ์ผ๋ฉด ์ถ๋ ฅํ๋ค. if (strcmp(Current->szName, compareName) == 0) { printf("%s\\n", Current->szName); printf("%s\\n", Current->szPhone); printf("%d\\n", Current->category); }
- โ๏ธ ์ ํ๋ฒํธ๋ถ ์ ์ฒด ์ถ๋ ฅ
- USERDATA* PrintAll(USERDATA* Head);
- ํ์ฌ ํฌ์ธํฐ pNext ์ฃผ์๊ฐ null ์ผ ๋๊น์ง ์ถ๋ ฅํ๋ค
USERDATA* Current = Head; while (Current != NULL) { printf("[%p] ", Current); printf("์ด๋ฆ: %s ", Current->szName); printf("์ ํ๋ฒํธ: %s ", Current->szPhone); printf("์นดํ ๊ณ ๋ฆฌ: %d ", Current->category); printf("์ด์ ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ : %p\\n", Current->pNext); Current = Current->pNext; } return Current;
- โ๏ธ ์ ํ๋ฒํธ๋ถ ์ญ์
- ์ญ์ ํ ์ด๋ฆ์ ์ฃผ์ ํ์ธ
- USERDATA* SLL_GetNodeAt(USERDATA* Head);
- ์ญ์ ํ๊ณ ์ถ์ ์ด๋ฆ์ด๋ ๊ฐ์ ๋๊น์ง ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ก ์ด์ด๊ฐ ํ, ์ฐพ์ ํ์ฌ ์ฃผ์๋ฅผ ๋ฆฌํดํ๋ค
char removeName[15]; char removeTemp[15]; scanf("%s", removeName); getchar(); USERDATA* Current = Head; while (Current != NULL) { strcpy(removeTemp, Current->szName); if (*removeName == *removeTemp) { break; } Current = Current->pNext; } return Current;
- ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ก ํด๋น ์ฃผ์ ์ญ์
- void Remove(USERDATA** Head, USERDATA* Remove);
- ์ญ์ ํ ์ฃผ์๋ฅผ ๋ง๋๋ฉด ์ญ์ ํ pNext์ ํ์ฌ pNext๋ฅผ ์ฐ๊ฒฐ์ํจ๋ค
if (*Head == Remove) { *Head = Remove->pNext; } else { USERDATA* Current = *Head; while (Current != NULL) { if (Current->pNext == Remove) { break; } Current = Current->pNext; } if (Current != NULL) Current->pNext = Remove->pNext; }
- ์ญ์ ํ ๋
ธ๋ ๋ฉ๋ชจ๋ฆฌ ์๋ฉธ
- void SLL_DestroyNode(USERDATA* Remove);
free(Remove);
- ์ญ์ ํ ์ด๋ฆ์ ์ฃผ์ ํ์ธ
- โ๏ธ ์ ํ๋ฒํธ๋ถ ์์
- void Update(USERDATA* Head);
- ์์ ํ๊ณ ์ถ์ ์ด๋ฆ์ ์ฐพ๊ณ , ์ ํ๋ฒํธ, ์นดํ ๊ณ ๋ฆฌ๋ฅผ ์ ๋ ฅํ๋ค
char UpdateName[32]; char UpdateNameTemp[32]; char UpdatePhone[32]; int UpdateCategory = 0; scanf("%s", UpdateName); while (Current->pNext != NULL) { //๊ตฌ์กฐ์ฒด ์ด๋ฆ๊ณผ ์์ ํ ์ด๋ฆ์ด ๊ฐ์ผ๋ฉด ์ถ๋ ฅํ๋ค. strcpy(UpdateNameTemp, Current->szName); if (*UpdateName == *UpdateNameTemp) { printf("์์ ํ๊ณ ์ถ์ ์ ํ๋ฒํธ๋ฅผ ์ ๋ ฅํ์ธ์ : "); scanf("%s", &UpdatePhone); strcpy(Current->szPhone, UpdatePhone); printf("์์ ํ๊ณ ์ถ์ ์นดํ ๊ณ ๋ฆฌ๋ฅผ ์ ๋ ฅํ์ธ์ ์น๊ตฌ[0], ๊ฐ์กฑ[1], ์ง์ฅ[2], ๊ธฐํ[3] : "); scanf("%d", &UpdateCategory); getchar(); Current->category = UpdateCategory; break; } Current = Current->pNext; }
- โ๏ธ ์ ํ๋ฒํธ๋ถ ์นดํ
๊ณ ๋ฆฌ๋ณ ์ถ๋ ฅ
- void Category(USERDATA* Head);
- ์ญ์ ํ๊ณ ์ ํ๋ ์นดํ ๊ณ ๋ฆฌ์ ๋ฒํธ๋ฅผ ๋ฐ์์ ๋ชจ๋ ์ถ๋ ฅํ๋ค
int CategoryNum = 0; scanf("%d", &CategoryNum); while (1) { //๊ตฌ์กฐ์ฒด ์ด๋ฆ๊ณผ ๊ฒ์ํ ์ด๋ฆ์ด ๊ฐ์ผ๋ฉด ์ถ๋ ฅํ๋ค. if (Current->category == CategoryNum) { printf("์ด๋ฆ: %s ", Current->szName); printf("์ ํ๋ฒํธ: %s ", Current->szPhone); printf("์นดํ ๊ณ ๋ฆฌ: %d ", Current->category); printf("\\n"); } if (Current->pNext == NULL) { break; } Current = Current->pNext; }
- โ๏ธ ์ ํ๋ฒํธ๋ถ ์ฒ์ ์คํ ์ ์ฃผ์ ์ฌํ ๋น
- โ๏ธ ์ ํ๋ฒํธ๋ถ UI
- ํจ์๋ณ๋ก ์ถ๋ ฅํ UI ๋ฅผ ๋ชจ์๋ค
void PrintAllUI() { printf("\\n"); printf("--------------------------------์ ์ฒด ์ถ๋ ฅ[P]--------------------------------\\n"); } void MainMenuUI() { printf("\\n"); printf("--------------------------------์ ํ๋ฒํธ๋ถ ์ธํฐํ์ด์ค--------------------------------\\n"); printf("์ถ๊ฐ[A] ๊ฒ์[S] ์ ์ฒด ์ถ๋ ฅ[P] ์ญ์ [R] ์์ [U] ์นดํ ๊ณ ๋ฆฌ ์ถ๋ ฅ[C] ํ๋ก๊ทธ๋จ ์ข ๋ฃ[X]\\n"); printf("์ํ์๋ ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํ์ธ์ : "); } void RemoveUI() { printf("\\n"); printf("--------------------------------์ญ์ [R]--------------------------------\\n"); printf("์ญ์ ํ๊ณ ์ถ์ ์ด๋ฆ์ ์ ๋ ฅํ์ธ์ : "); } void CategoryUI() { printf("\\n"); printf("--------------------------------์นดํ ๊ณ ๋ฆฌ ์ถ๋ ฅ[C]--------------------------------\\n"); printf("๋ณด๊ณ ์ถ์ ์นดํ ๊ณ ๋ฆฌ์ ๋ง๋ ์ซ์๋ฅผ ์ ๋ ฅํ์ธ์. ์น๊ตฌ[0], ๊ฐ์กฑ[1], ์ง์ฅ[2], ๊ธฐํ[3]: "); } void CreateUI() { printf("\\n"); printf("--------------------------------์ถ๊ฐ[A]--------------------------------\\n"); } void SearchUI() { printf("\\n"); printf("--------------------------------๊ฒ์[S]--------------------------------\\n"); printf("๊ฒ์ํ๊ณ ์ถ์ ์ด๋ฆ์ ์ ๋ ฅํ์ธ์ : "); } void UpdateUI() { printf("\\n"); printf("--------------------------------์์ [U]--------------------------------\\n"); printf("์์ ํ๊ณ ์ถ์ ์ด๋ฆ์ ์ ๋ ฅํ์ธ์ : "); }
- โ๏ธ ์ ํ๋ฒํธ๋ถ ๋
- void Exit(USERDATA* Head);
- ์ ํ๋ฒํธ๋ถ ์ข ๋ฃ์ ๋์ ํ ๋น ์๋ฉธ์ํค๊ธฐ
while (Current->pNext != NULL) { free(Current); }
๐จ๐ป 5. ํ๋ก์ ํธ ์ฑ๊ณผ ๊ฒฐ๊ณผ
โ๏ธ ์ ํ๋ฒํธ๋ถ ์ถ๊ฐ
โ๏ธ ์ ํ๋ฒํธ๋ถ ๊ฒ์
โ๏ธ ์ ํ๋ฒํธ๋ถ ์ ์ฒด ์ถ๋ ฅ
โ๏ธ ์ ํ๋ฒํธ๋ถ ์ญ์
โ๏ธ ์ ํ๋ฒํธ๋ถ ์์
โ๏ธ ์ ํ๋ฒํธ๋ถ ์นดํ ๊ณ ๋ฆฌ๋ณ ์ถ๋ ฅ
โ๏ธ ์ ํ๋ฒํธ๋ถ ์ฒ์ ์คํ ์ ์ฃผ์ ์ฌํ ๋น
โ๏ธ ์ ํ๋ฒํธ๋ถ UI
โ๏ธ ์ ํ๋ฒํธ๋ถ ๋
728x90
๋ฐ์ํ
'๐ฉโ๐ป IoT (Embedded) > C์ธ์ด' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C programming] ํ์ ๋ฌธ์์ด์ ์ด์ฉํ ์ ์ถ๋ ฅ (printf, scanf) (0) | 2023.08.24 |
---|---|
[C programming] ๋ฌธ์ & ๋ฌธ์์ด ์ ์ถ๋ ฅ ํจ์ (0) | 2023.08.22 |
[C programming] ๋ฉ๋ชจ๋ฆฌ์ ๋ํ ๊ธฐ๋ณธ ์ด๋ก (0) | 2023.08.22 |
[C programming] ๋ด๊ฐ ๊ถ๊ธํด์ ์ ๋ฆฌํ ํฌ์ธํธ ๋ฐฐ์ด char ์ฐจ์ด & ํฌ์ธํธ ๋ฐฐ์ด ํฌ๊ธฐ (1) | 2023.08.22 |
[C programming] ํฌ์ธํฐ & ๋ณ์ & ์ฃผ์ (0) | 2023.08.21 |
Comments