본문 바로가기
카테고리 없음

친구 관리 프로그램

by 휘성티비 2024. 6. 24.

오늘은 친구관리 프로그램은 만들어 업로드 해보겠습니다.

 

C언어에서 동적할당, 구조체, Windows.h 헤더파일을 이용하여 프로그램을 만들고 꾸미기 까지 해보았습니다.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>

enum ColorType {
	BLUE = 9,
	GREEN,		//10
	SkyBlue,	//11
	RED,		//12
	PURPLE,		//13
	YELLOW,		//14
	WHITE		//15
} COLOR;

void textcolor(int colorNum) {
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colorNum);
}

typedef struct {
	char* name;
	char* message;
	int status;
} Friend;

Friend* friends = NULL;
int friendCount = 0;

int menu() {
	textcolor(PURPLE);
	printf("======================== Friend Menu ========================\n");
	printf("=                                                           =\n");
	printf("=                                                           =\n");
	printf("=                                                           =\n");
	printf("=");
	textcolor(SkyBlue);
	printf("                       1. 친구 추가                        ");
	textcolor(PURPLE);
	printf("=\n");

	printf("=");
	textcolor(RED);
	printf("                       2. 친구 검색                        ");
	textcolor(PURPLE);
	printf("=\n");

	printf("=");
	textcolor(YELLOW);
	printf("                       3. 친구 삭제                        ");
	textcolor(PURPLE);
	printf("=\n");

	printf("=");
	textcolor(GREEN);
	printf("                       4. 전체 친구 목록 보기              ");
	textcolor(PURPLE);
	printf("=\n");

	printf("=");
	textcolor(BLUE);
	printf("                       5. 종료                             ");
	textcolor(PURPLE);
	printf("=\n");
	printf("=                                                           =\n");
	printf("=                                                           =\n");
	printf("=                                                           =\n");
	printf("=============================================================\n");
	printf("\n");
	textcolor(WHITE);
	int a;
	scanf("%d", &a);
	return a;
}

void add_friend() {
	int nameLength, messageLength;

	printf("이름의 길이: ");
	scanf("%d", &nameLength);

	char* tempName = (char*)malloc(nameLength + 1);

	printf("이름: ");
	scanf("%s", tempName);
	for (int i = 0; i < friendCount; i++) {
		if (strcmp(friends[i].name, tempName) == 0) {
			printf("이미 존재하는 친구 이름\n");
			free(tempName);
			return;
		}
	}
	printf("메시지의 길이: ");
	scanf("%d", &messageLength);

	char* tempMessage = (char*)malloc(messageLength + 1);


	printf("메시지: ");
	scanf("%s", tempMessage);

	friends = (Friend*)realloc(friends, sizeof(Friend) * (friendCount + 1));
	friends[friendCount].name = tempName;
	friends[friendCount].message = tempMessage;

	printf("상태 (1:온라인, 2:오프라인, 3:자리비움): ");
	scanf("%d", &friends[friendCount].status);

	friendCount++;
}


void search_friend() {
	char name[10];
	printf("검색할 친구의 이름: ");
	scanf("%s", name);
	for (int i = 0; i < friendCount; i++) {
		if (strcmp(friends[i].name, name) == 0) {
			textcolor(SkyBlue);
			printf("|      이름 |   상태 |   메시지 |\n");
			printf("|      %s  |", friends[i].name);
			printf("     %d  |", friends[i].status);
			printf("    %s |\n", friends[i].message);
			textcolor(WHITE);
			return;
		}
	}
	printf("해당 이름의 친구를 찾을 수 없습니다.\n");
}

void delete_friend() {
	char name[10];
	printf("삭제할 친구의 이름: ");
	scanf("%s", name);
	for (int i = 0; i < friendCount; i++) {
		if (strcmp(friends[i].name, name) == 0) {
			for (int j = i; j < friendCount - 1; j++) {
				friends[j] = friends[j + 1];
			}
			friendCount--;
			friends = (Friend*)realloc(friends, sizeof(Friend) * friendCount);
			printf("친구가 삭제되었습니다.\n");
			return;
		}
	}
	printf("해당 이름의 친구를 찾을 수 없습니다.\n");
}

void display_friends() {
	textcolor(SkyBlue);
	printf("|      이름 |   상태 |   메시지 |\n");
	for (int i = 0; i < friendCount; i++) {
		printf("|      %s  |", friends[i].name);
		printf("     %d  |", friends[i].status);
		printf("    %s |\n", friends[i].message);
	}
	textcolor(WHITE);

}

int main() {
	int m;
	while (1) {
		m = menu();
		if (m == 1) {
			add_friend();
		}
		else if (m == 2) {
			search_friend();
		}
		else if (m == 3) {
			delete_friend();
		}
		else if (m == 4) {
			display_friends();
		}
		else if (m == 5) {
			for (int i = 0; i < friendCount; i++) {
				free(friends[i].name);
				free(friends[i].message);
			}
			free(friends);
			break;
		}
		else {
			printf("메뉴에 없는 번호입니다.\n");
		}
	}
	return 0;
}

동적할당에서 realloc을 활용해 구조체 포인터의 길이를 다루어 목록을 추가하고 줄이고를 할 수 있었고, Windows.h 헤더파일과 textcolor 함수를 통해 C언어 커맨드 창을 꾸미는 것까지 가능했습니다. 어느 부분에서 할당을 해제하고 어떤식으로 동적인 동작이 일어나는지 이해할 수 있었습니다.

결과는 위 사진과 같고 앞으로 친구 목록을 텍스트 파일로 만드는 작업도 해보겠습니다.