From 06a0b782cfc4ad4375c9c88ce832ff4c24a473e3 Mon Sep 17 00:00:00 2001 From: Zvonimir Rudinski Date: Sat, 16 Dec 2023 17:03:00 +0100 Subject: [PATCH] make stuff work --- main.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index dbda043..5465cf2 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,94 @@ #include +#include +#include -int main(int argc, char **argv) { - printf("Hello, world!\n"); +#define MAX_TODO_TITLE_LENGTH 32 +#define MAX_TODOS 10 - return 0; +typedef struct { + char title[MAX_TODO_TITLE_LENGTH]; + bool completed; +} TodoItem; + +enum Command { + ADD = 'a', + MARK = 'm', + PRINT = 'p', + QUIT = 'q' +}; + +TodoItem create_todo(const char *title) { + TodoItem item; + strncpy(item.title, title, MAX_TODO_TITLE_LENGTH); + item.completed = false; + return item; +} + +void mark_todo(TodoItem *item, bool completed) { + (*item).completed = completed; +} + +void print_todo(TodoItem item) { + printf("%s - ", item.title); + if (item.completed) { + printf("Completed\n"); + } else { + printf("Not Completed\n"); + } +} + +TodoItem todos[MAX_TODOS]; +int todos_count = 0; + +void add_todo() { + char title[MAX_TODO_TITLE_LENGTH]; + printf("Enter todo title: "); + fgets(title, MAX_TODO_TITLE_LENGTH - 1, stdin); + + TodoItem item = create_todo(title); + todos[todos_count] = item; + todos_count++; +} + +void print_all_todos() { + for (int i = 0; i < todos_count; i++) { + print_todo(todos[i]); + } +} + +int main(int argc, char **argv) { + char cmd; + int op_index = -1; + do { + printf("Enter command: "); + cmd = getc(stdin); + getc(stdin); // remove the newline character from the buffer + + switch (cmd) { + case ADD: + if (todos_count + 1 > MAX_TODOS) { + printf("No more space for new todos\n"); + break; + } + add_todo(); + break; + case MARK: + do { + printf("Enter todo index: "); + op_index = getc(stdin) - '0'; + getc(stdin); // remove the newline character from the buffer + } while (op_index < 0 || op_index >= todos_count); + mark_todo(&todos[op_index], !todos[op_index].completed); + break; + case PRINT: + print_all_todos(); + break; + case QUIT: + break; + default: + printf("Invalid command\n"); + break; + } + + } while (cmd != QUIT); }