From 015f38f7806be18aae1db545ec8c75f0d900efbb Mon Sep 17 00:00:00 2001 From: leca Date: Sat, 21 Dec 2024 17:58:39 +0300 Subject: [PATCH] garland generation and printing --- main.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 main.c diff --git a/main.c b/main.c new file mode 100644 index 0000000..47cbb87 --- /dev/null +++ b/main.c @@ -0,0 +1,60 @@ +#include +#include +#include + +#define YELLOW "\033[1;33m" +#define RED "\033[1;31m" +#define BLUE "\033[1;34m" +#define GREEN "\033[1;32m" +#define RESET "\033[0m" + +typedef struct Garland { + char** lightbulbs; + unsigned int length; +} Garland; + +Garland generate_garland(unsigned int length); +void print_garland(Garland* g); + +int main () { + Garland g = generate_garland(100); + print_garland(&g); +} + +Garland generate_garland(unsigned int length) { + Garland g; + 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] = (char*) malloc(9); + strncpy(g.lightbulbs[i], color, 9); + free(color); + color = NULL; + } + return g; +} + +void print_garland(Garland* g) { + for (unsigned int i = 0; i < g->length; i ++) { + printf("%s", g->lightbulbs[i]); + } + printf(RESET); + printf("\n"); +}