make stuff work
This commit is contained in:
93
main.c
93
main.c
@@ -1,7 +1,94 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
#define MAX_TODO_TITLE_LENGTH 32
|
||||||
printf("Hello, world!\n");
|
#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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user