|
1 | 1 | #include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#define MAX_TASKS 50 |
| 6 | +#define MAX_DESC 200 |
| 7 | + |
| 8 | +typedef struct { |
| 9 | + char description[MAX_DESC]; |
| 10 | + char priority[10]; |
| 11 | + char category[30]; |
| 12 | + int completed; |
| 13 | +} Task; |
| 14 | + |
| 15 | +Task tasks[MAX_TASKS]; |
| 16 | +int count = 0; |
| 17 | + |
| 18 | +void loadTasks() { |
| 19 | + FILE *file = fopen("tasks.dat", "rb"); |
| 20 | + if (file) { |
| 21 | + fread(&count, sizeof(int), 1, file); |
| 22 | + fread(tasks, sizeof(Task), count, file); |
| 23 | + fclose(file); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +void saveTasks() { |
| 28 | + FILE *file = fopen("tasks.dat", "wb"); |
| 29 | + if (file) { |
| 30 | + fwrite(&count, sizeof(int), 1, file); |
| 31 | + fwrite(tasks, sizeof(Task), count, file); |
| 32 | + fclose(file); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +void addTask() { |
| 37 | + if (count >= MAX_TASKS) { |
| 38 | + printf("Task limit reached!\n"); |
| 39 | + return; |
| 40 | + } |
| 41 | + printf("Enter task description: "); |
| 42 | + getchar(); |
| 43 | + fgets(tasks[count].description, MAX_DESC, stdin); |
| 44 | + tasks[count].description[strcspn(tasks[count].description, "\n")] = 0; |
| 45 | + |
| 46 | + printf("Enter priority (High/Medium/Low): "); |
| 47 | + fgets(tasks[count].priority, 10, stdin); |
| 48 | + tasks[count].priority[strcspn(tasks[count].priority, "\n")] = 0; |
| 49 | + |
| 50 | + printf("Enter category: "); |
| 51 | + fgets(tasks[count].category, 30, stdin); |
| 52 | + tasks[count].category[strcspn(tasks[count].category, "\n")] = 0; |
| 53 | + |
| 54 | + tasks[count].completed = 0; |
| 55 | + count++; |
| 56 | + printf("Task added successfully!\n"); |
| 57 | +} |
| 58 | + |
| 59 | +void viewTasks() { |
| 60 | + if (count == 0) { |
| 61 | + printf("No tasks available.\n"); |
| 62 | + return; |
| 63 | + } |
| 64 | + printf("\n%-3s %-30s %-10s %-15s %-10s\n", "#", "Description", "Priority", "Category", "Status"); |
| 65 | + printf("----------------------------------------------------------------\n"); |
| 66 | + for (int i = 0; i < count; i++) { |
| 67 | + printf("%-3d %-30s %-10s %-15s %-10s\n", i + 1, tasks[i].description, |
| 68 | + tasks[i].priority, tasks[i].category, |
| 69 | + tasks[i].completed ? "Done" : "Pending"); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +void editTask() { |
| 74 | + int id; |
| 75 | + printf("Enter task number to edit: "); |
| 76 | + if (scanf("%d", &id) != 1 || id < 1 || id > count) { |
| 77 | + printf("Invalid task number!\n"); |
| 78 | + return; |
| 79 | + } |
| 80 | + id--; |
| 81 | + printf("Current: %s\n", tasks[id].description); |
| 82 | + printf("New description: "); |
| 83 | + getchar(); |
| 84 | + fgets(tasks[id].description, MAX_DESC, stdin); |
| 85 | + tasks[id].description[strcspn(tasks[id].description, "\n")] = 0; |
| 86 | + printf("Task updated!\n"); |
| 87 | +} |
| 88 | + |
| 89 | +void removeTask() { |
| 90 | + int id; |
| 91 | + printf("Enter task number to remove: "); |
| 92 | + if (scanf("%d", &id) != 1 || id < 1 || id > count) { |
| 93 | + printf("Invalid task number!\n"); |
| 94 | + return; |
| 95 | + } |
| 96 | + id--; |
| 97 | + for (int i = id; i < count - 1; i++) { |
| 98 | + tasks[i] = tasks[i + 1]; |
| 99 | + } |
| 100 | + count--; |
| 101 | + printf("Task removed!\n"); |
| 102 | +} |
| 103 | + |
| 104 | +void markComplete() { |
| 105 | + int id; |
| 106 | + printf("Enter task number to mark complete: "); |
| 107 | + if (scanf("%d", &id) != 1 || id < 1 || id > count) { |
| 108 | + printf("Invalid task number!\n"); |
| 109 | + return; |
| 110 | + } |
| 111 | + tasks[id - 1].completed = 1; |
| 112 | + printf("Task marked as complete!\n"); |
| 113 | +} |
| 114 | + |
| 115 | +void sortTasks() { |
| 116 | + for (int i = 0; i < count - 1; i++) { |
| 117 | + for (int j = 0; j < count - i - 1; j++) { |
| 118 | + if (strcmp(tasks[j].priority, tasks[j + 1].priority) > 0) { |
| 119 | + Task temp = tasks[j]; |
| 120 | + tasks[j] = tasks[j + 1]; |
| 121 | + tasks[j + 1] = temp; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + printf("Tasks sorted by priority!\n"); |
| 126 | +} |
| 127 | + |
| 128 | +void searchTasks() { |
| 129 | + char keyword[50]; |
| 130 | + printf("Enter search keyword: "); |
| 131 | + getchar(); |
| 132 | + fgets(keyword, 50, stdin); |
| 133 | + keyword[strcspn(keyword, "\n")] = 0; |
| 134 | + |
| 135 | + printf("\nSearch Results:\n"); |
| 136 | + int found = 0; |
| 137 | + for (int i = 0; i < count; i++) { |
| 138 | + if (strstr(tasks[i].description, keyword) || strstr(tasks[i].category, keyword)) { |
| 139 | + printf("%d. %s [%s] (%s)\n", i + 1, tasks[i].description, |
| 140 | + tasks[i].priority, tasks[i].category); |
| 141 | + found = 1; |
| 142 | + } |
| 143 | + } |
| 144 | + if (!found) printf("No matching tasks found.\n"); |
| 145 | +} |
2 | 146 |
|
3 | 147 | int main() { |
4 | | - char tasks[20][100]; // Stores up to 20 tasks of max 100 characters |
5 | | - int count = 0; // Number of tasks added |
6 | | - int choice; // Menu choice variable |
| 148 | + loadTasks(); |
| 149 | + int choice; |
7 | 150 |
|
8 | 151 | while (1) { |
9 | | - // Simple menu |
10 | | - printf("\n--- TO-DO LIST ---\n"); |
| 152 | + printf("\n=== ENHANCED TO-DO LIST ===\n"); |
11 | 153 | printf("1. Add Task\n"); |
12 | | - printf("2. View Tasks\n"); |
13 | | - printf("3. Exit\n"); |
14 | | - printf("Enter choice: "); |
15 | | - scanf("%d", &choice); |
16 | | - |
17 | | - if (choice == 1) { |
18 | | - // Add a new task |
19 | | - printf("Enter task: "); |
20 | | - getchar(); // clears leftover newline |
21 | | - fgets(tasks[count], 100, stdin); |
22 | | - count++; |
| 154 | + printf("2. View All Tasks\n"); |
| 155 | + printf("3. Edit Task\n"); |
| 156 | + printf("4. Remove Task\n"); |
| 157 | + printf("5. Mark Complete\n"); |
| 158 | + printf("6. Sort by Priority\n"); |
| 159 | + printf("7. Search Tasks\n"); |
| 160 | + printf("8. Save & Exit\n"); |
| 161 | + printf("Enter choice (1-8): "); |
| 162 | + |
| 163 | + if (scanf("%d", &choice) != 1) { |
| 164 | + printf("Please enter a valid number!\n"); |
| 165 | + while (getchar() != '\n'); |
| 166 | + continue; |
23 | 167 | } |
24 | | - else if (choice == 2) { |
25 | | - // Display all tasks |
26 | | - printf("\nYour Tasks:\n"); |
27 | | - for (int i = 0; i < count; i++) { |
28 | | - printf("%d. %s", i + 1, tasks[i]); |
29 | | - } |
30 | | - } |
31 | | - else if (choice == 3) { |
32 | | - // Exit program |
33 | | - break; |
34 | | - } |
35 | | - else { |
36 | | - printf("Invalid choice!\n"); |
| 168 | + |
| 169 | + switch (choice) { |
| 170 | + case 1: addTask(); break; |
| 171 | + case 2: viewTasks(); break; |
| 172 | + case 3: editTask(); break; |
| 173 | + case 4: removeTask(); break; |
| 174 | + case 5: markComplete(); break; |
| 175 | + case 6: sortTasks(); break; |
| 176 | + case 7: searchTasks(); break; |
| 177 | + case 8: saveTasks(); printf("Tasks saved. Goodbye!\n"); return 0; |
| 178 | + default: printf("Invalid choice! Please enter 1-8.\n"); |
37 | 179 | } |
38 | 180 | } |
39 | | - return 0; |
40 | 181 | } |
0 commit comments