Todo list basic app

This commit is contained in:
sosokker 2023-08-12 21:26:01 +07:00
parent fa4934f439
commit becd7e789c
5 changed files with 154 additions and 0 deletions

40
todo-list/main.c Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include "tasks.h"
int main() {
int numTasks = 0;
struct Task tasks[MAX_TASKS];
loadTasksFromFile(tasks, &numTasks);
int choice;
do {
printf("To-Do List Application\n");
printf("1. Add Task\n");
printf("2. View Tasks\n");
printf("3. Mark Completed\n");
printf("4. Save and Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addTask(tasks, &numTasks);
break;
case 2:
viewTasks(tasks, numTasks);
break;
case 3:
markCompleted(tasks, numTasks);
break;
case 4:
saveTasksToFile(tasks, numTasks);
printf("Tasks saved. Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);
return 0;
}

92
todo-list/tasks.c Normal file
View File

@ -0,0 +1,92 @@
#include <stdio.h>
#include <stdlib.h>
#include "tasks.h"
void loadTasksFromFile(struct Task tasks[], int *numTasks) {
FILE *file = fopen("tasks.txt", "r");
if (file == NULL) {
printf("No tasks file found.\n");
return;
}
int i = 0;
while (fscanf(file, "%49s %99s %19s %d", tasks[i].title, tasks[i].description, tasks[i].dueDate, &tasks[i].completed) == 4) {
i++;
if (i >= MAX_TASKS) {
break;
}
}
*numTasks = i;
fclose(file);
}
void saveTasksToFile(struct Task tasks[], int numTasks) {
FILE *file = fopen("tasks.txt", "w");
if (file == NULL) {
printf("Error saving tasks.\n");
return;
}
for (int i = 0; i < numTasks; i++) {
fprintf(file, "%s %s %s %d\n", tasks[i].title, tasks[i].description, tasks[i].dueDate, tasks[i].completed);
}
fclose(file);
}
void addTask(struct Task tasks[], int *numTasks) {
if (*numTasks >= MAX_TASKS) {
printf("Maximum number of tasks reached.\n");
return;
}
printf("Enter task title: ");
scanf("%s", tasks[*numTasks].title);
printf("Enter task description: ");
scanf("%s", tasks[*numTasks].description);
printf("Enter due date: ");
scanf("%s", tasks[*numTasks].dueDate);
tasks[*numTasks].completed = 0;
(*numTasks)++;
}
void viewTasks(struct Task tasks[], int numTasks) {
if (numTasks == 0) {
printf("No tasks available.\n");
return;
}
printf("Tasks:\n");
for (int i = 0; i < numTasks; i++) {
printf("Task %d:\n", i + 1);
printf("Title: %s\n", tasks[i].title);
printf("Description: %s\n", tasks[i].description);
printf("Due Date: %s\n", tasks[i].dueDate);
printf("Status: %s\n", tasks[i].completed ? "Completed" : "Incomplete");
printf("\n");
}
}
void markCompleted(struct Task tasks[], int numTasks) {
if (numTasks == 0) {
printf("No tasks available.\n");
return;
}
printf("Enter task number to mark as completed: ");
int taskNum;
scanf("%d", &taskNum);
if (taskNum < 1 || taskNum > numTasks) {
printf("Invalid task number.\n");
return;
}
tasks[taskNum - 1].completed = 1;
}

21
todo-list/tasks.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef TASKS_H
#define TASKS_H
#define MAX_TITLE_LENGTH 50
#define MAX_DESC_LENGTH 100
#define MAX_TASKS 100
struct Task {
char title[MAX_TITLE_LENGTH];
char description[MAX_DESC_LENGTH];
char dueDate[20];
int completed;
};
void loadTasksFromFile(struct Task tasks[], int *numTasks);
void saveTasksToFile(struct Task tasks[], int numTasks);
void addTask(struct Task tasks[], int *numTasks);
void viewTasks(struct Task tasks[], int numTasks);
void markCompleted(struct Task tasks[], int numTasks);
#endif

1
todo-list/tasks.txt Normal file
View File

@ -0,0 +1 @@
Exercise Running Brooo 1

BIN
todo-list/todo.exe Normal file

Binary file not shown.