Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
7.5 kB
4
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Task {
    char task[100];
    int duration;
    int completed;
    struct Task* next;
} Task;

Task* createTask(const char* task, int duration) {
    Task* newTask = (Task*)malloc(sizeof(Task));
    strcpy(newTask->task, task);
    newTask->duration = duration;
    newTask->completed = 0;
    newTask->next = NULL;
    return newTask;
}

Task* addTask(Task* head, const char* task, int duration) {
    if (head == NULL) {
        return createTask(task, duration);
    } else {
        Task* current = head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = createTask(task, duration);
    }
    return head;
}

void removeTask(Task** head, const char* task) {
    if (*head == NULL) {
        printf("Todo list is empty.\n");
        return;
    }

    Task* current = *head;
    Task* prev = NULL;
    int found = 0;

    while (current != NULL) {
        if (strcmp(current->task, task) == 0) {
            found = 1;
            break;
        }

        prev = current;
        current = current->next;
    }

    if (found) {
        if (prev == NULL) {
            *head = current->next;
        } else {
            prev->next = current->next;
        }
        free(current);
        printf("Task removed.\n");
    } else {
        printf("Task not found.\n");
    }
}

void displayTasks(Task* head, const char* day) {
    printf("Tasks for %s:\n", day);

    if (head == NULL) {
        printf("Todo list is empty.\n");
    } else {
        Task* current = head;
        while (current != NULL) {
            printf("%s (%d minutes) - %s\n", current->task, current->duration,
                   current->completed ? "Completed" : "Not Completed");
            current = current->next;
        }
    }
}

void markTaskCompleted(Task* head, const char* task) {
    Task* current = head;
    int found = 0;

    while (current != NULL) {
        if (strcmp(current->task, task) == 0) {
            found = 1;
            current->completed = 1;
            break;
        }
        current = current->next;
    }

    if (found) {
        printf("Task marked as completed.\n");
    } else {
        printf("Task not found.\n");
    }
}

void removeCompletedTasks(Task** head) {
    Task* current = *head;
    Task* prev = NULL;

    while (current != NULL) {
        if (current->completed) {
            if (prev == NULL) {
                *head = current->next;
                free(current);
                current = *head;
            } else {
                prev->next = current->next;
                free(current);
                current = prev->next;
            }
        } else {
            prev = current;
            current = current->next;
        }
    }

    printf("Completed tasks removed.\n");
}

void pushLeftoverTasks(Task** currentDay, Task** nextDay, const char* currentDayName, const char* nextDayName) {
    Task* current = *currentDay;
    Task* prev = NULL;

    while (current != NULL) {
        if (!current->completed) {
            if (*nextDay == NULL) {
                *nextDay = createTask(current->task, current->duration);
            } else {
                Task* nextCurrent = *nextDay;
                while (nextCurrent->next != NULL) {
                    nextCurrent = nextCurrent->next;
                }
                nextCurrent->next = createTask(current->task, current->duration);
            }

            if (prev == NULL) {
                *currentDay = current->next;
                free(current);
                current = *currentDay;
            } else {
                prev->next = current->next;
                free(current);
                current = prev->next;
            }
        } else {
            prev = current;
            current = current->next;
        }
    }

    printf("Leftover tasks pushed to %s.\n", nextDayName);
}

void freeTasks(Task* head) {
    Task* current = head;
    while (current != NULL) {
        Task* temp = current;
        current = current->next;
        free(temp);
    }
}

int main() {
    Task* days[7] = { NULL };
    char daysOfWeek[7][20] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

    int currentDayIndex;
     printf("What is the day today:\n");
    printf("1. Monday\n");
    printf("2. Tuesday\n");
    printf("3. Wednesday\n");
    printf("4. Thursday\n");
    printf("5. Friday\n");
    printf("6. Saturday\n");
    printf("7. Sunday\n");
    printf("Enter current day:");
    scanf("%d", &currentDayIndex);
    getchar();

    int choice;
    char task[100];
    int duration;

    do {
        printf("\nTodo List Menu\n");
        printf("1. Add Task\n");
        printf("2. Remove Task\n");
        printf("3. Display Tasks\n");
        printf("4. Mark Task as Completed\n");
        printf("5. Remove Completed Tasks\n");
        printf("6. Push Leftover Tasks to Next Day\n");
        printf("7. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        getchar();  // Consume the newline character

        switch (choice) {
            case 1:
                printf("Enter the task: ");
                fgets(task, sizeof(task), stdin);
                task[strcspn(task, "\n")] = '\0';  // Remove trailing newline character
                printf("Enter the duration (in minutes): ");
                scanf("%d", &duration);
                getchar();  // Consume the newline character
                days[currentDayIndex - 1] = addTask(days[currentDayIndex - 1], task, duration);
                break;

            case 2:
                printf("Enter the task to remove: ");
                fgets(task, sizeof(task), stdin);
                task[strcspn(task, "\n")] = '\0';  // Remove trailing newline character
                removeTask(&days[currentDayIndex - 1], task);
                break;

            case 3:
                displayTasks(days[currentDayIndex - 1], daysOfWeek[currentDayIndex - 1]);
                break;

            case 4:
                printf("Enter the task to mark as completed: ");
                fgets(task, sizeof(task), stdin);
                task[strcspn(task, "\n")] = '\0';  // Remove trailing newline character
                markTaskCompleted(days[currentDayIndex - 1], task);
                break;

            case 5:
                removeCompletedTasks(&days[currentDayIndex - 1]);
                break;

            case 6:
                if (currentDayIndex == 7) {
                    printf("Cannot push leftover tasks to the next day. It is already Sunday.\n");
                } else {
                    pushLeftoverTasks(&days[currentDayIndex - 1], &days[currentDayIndex], daysOfWeek[currentDayIndex - 1], daysOfWeek[currentDayIndex]);
                    currentDayIndex++;
                }
                break;

            case 7:
                printf("Exiting the program.\n");
                break;

            default:
                printf("Invalid choice. Please try again.\n");
        }

    } while (choice != 7);

    // Free the memory for all days
    for (int i = 0; i < 7; i++) {
        freeTasks(days[i]);
    }

    return 0;
}