From ff80b3a4383fa32f7406c7ffed4f210843af3cc5 Mon Sep 17 00:00:00 2001 From: leca Date: Sat, 21 Dec 2024 18:20:33 +0300 Subject: [PATCH] added CMakeLists.txt and simplified code --- CMakeLists.txt | 5 +++++ main.c | 36 +++++++++--------------------------- 2 files changed, 14 insertions(+), 27 deletions(-) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..1d10ed7 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.10) + +project(garland VERSION 1.0 LANGUAGES C) + +add_executable(garland main.c) diff --git a/main.c b/main.c index 234b5f2..a5a8251 100644 --- a/main.c +++ b/main.c @@ -2,14 +2,16 @@ #include #include -#define YELLOW "\033[1;33m" -#define RED "\033[1;31m" -#define BLUE "\033[1;34m" -#define GREEN "\033[1;32m" +#define YELLOW "\033[1;33m#" +#define RED "\033[1;31m#" +#define BLUE "\033[1;34m#" +#define GREEN "\033[1;32m#" #define RESET "\033[0m" +static char* lightbulbs[] = {RED, YELLOW, BLUE, GREEN}; + typedef struct Garland { - char** lightbulbs; + char* lightbulbs; unsigned int length; } Garland; @@ -26,34 +28,14 @@ Garland generate_garland(unsigned int length) { g.lightbulbs = malloc(length * sizeof(char*)); g.length = length; for (unsigned int i = 0; i < length; i ++) { - char* color = (char*) malloc(9); - switch (rand() % 4) { - case 0: - strncpy(color, RED, 7); - break; - case 1: - strncpy(color, YELLOW, 7); - break; - case 2: - strncpy(color, GREEN, 7); - break; - case 3: - strncpy(color, BLUE, 7); - break; - } - strncpy(color+7, "#\0", 2); - - g.lightbulbs[i] = malloc(9); - strncpy(g.lightbulbs[i], color, 9); - free(color); - color = NULL; + g.lightbulbs[i] = rand() % 4; } return g; } void print_garland(Garland* g) { for (unsigned int i = 0; i < g->length; i ++) { - printf("%s", g->lightbulbs[i]); + printf("%s", lightbulbs[g->lightbulbs[i]]); } printf(RESET); printf("\n");