-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskFlow.c
More file actions
215 lines (183 loc) · 5.4 KB
/
Copy pathTaskFlow.c
File metadata and controls
215 lines (183 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TASKS 100
#define MAX_TITLE 100
// ---------------- STRUCTURES ----------------
typedef struct {
int id;
char title[MAX_TITLE];
int priority; // 1 = High, 2 = Medium, 3 = Low
int completed; // 0 = pending, 1 = done
} Task;
Task heap[MAX_TASKS];
int heapSize = 0;
// ---------------- FUNCTION DECLARATIONS ----------------
void addTask();
void viewTasks();
void markTaskComplete();
void deleteTask();
void saveTasks();
void loadTasks();
void swap(Task *a, Task *b);
void heapifyUp(int index);
void heapifyDown(int index);
Task extractMin();
// ---------------- MAIN MENU ----------------
int main() {
int choice;
loadTasks(); // Load saved tasks on start
do {
printf("\n====== TaskFlow - To-Do List Manager ======\n");
printf("1. Add Task\n");
printf("2. View Tasks (by Priority)\n");
printf("3. Mark Task as Completed\n");
printf("4. Delete Top Priority Task\n");
printf("5. Save & Exit\n");
printf("===========================================\n");
printf("Enter choice: ");
scanf("%d", &choice);
getchar(); // clear buffer
switch (choice) {
case 1: addTask(); break;
case 2: viewTasks(); break;
case 3: markTaskComplete(); break;
case 4: deleteTask(); break;
case 5:
saveTasks();
printf("Tasks saved successfully. Exiting...\n");
break;
default: printf("Invalid choice! Try again.\n");
}
} while (choice != 5);
return 0;
}
// ---------------- HEAP FUNCTIONS ----------------
// Add new task to heap
void addTask() {
if (heapSize >= MAX_TASKS) {
printf("Task limit reached!\n");
return;
}
Task newTask;
newTask.id = heapSize + 1;
newTask.completed = 0;
printf("Enter task title: ");
fgets(newTask.title, MAX_TITLE, stdin);
newTask.title[strcspn(newTask.title, "\n")] = '\0'; // remove newline
printf("Enter priority (1 = High, 2 = Medium, 3 = Low): ");
scanf("%d", &newTask.priority);
heap[heapSize] = newTask;
heapifyUp(heapSize);
heapSize++;
printf("Task added successfully!\n");
}
// Heapify upwards
void heapifyUp(int index) {
if (index == 0) return;
int parent = (index - 1) / 2;
if (heap[index].priority < heap[parent].priority) {
swap(&heap[index], &heap[parent]);
heapifyUp(parent);
}
}
// Heapify downwards
void heapifyDown(int index) {
int smallest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;
if (left < heapSize && heap[left].priority < heap[smallest].priority)
smallest = left;
if (right < heapSize && heap[right].priority < heap[smallest].priority)
smallest = right;
if (smallest != index) {
swap(&heap[index], &heap[smallest]);
heapifyDown(smallest);
}
}
// Swap two tasks
void swap(Task *a, Task *b) {
Task temp = *a;
*a = *b;
*b = temp;
}
// Extract top-priority task
Task extractMin() {
Task minTask = heap[0];
heap[0] = heap[heapSize - 1];
heapSize--;
heapifyDown(0);
return minTask;
}
// ---------------- CORE FEATURES ----------------
// View all tasks
void viewTasks() {
if (heapSize == 0) {
printf("No tasks available.\n");
return;
}
printf("\n---- Current Tasks (by priority) ----\n");
for (int i = 0; i < heapSize; i++) {
printf("[%d] %s | Priority: %d | Status: %s\n",
heap[i].id,
heap[i].title,
heap[i].priority,
heap[i].completed ? "Done" : "Pending");
}
}
// Mark task as completed
void markTaskComplete() {
if (heapSize == 0) {
printf("No tasks to mark.\n");
return;
}
char title[MAX_TITLE];
printf("Enter task title to mark completed: ");
fgets(title, MAX_TITLE, stdin);
title[strcspn(title, "\n")] = '\0';
for (int i = 0; i < heapSize; i++) {
if (strcmp(heap[i].title, title) == 0) {
heap[i].completed = 1;
printf("Task marked as completed!\n");
return;
}
}
printf("Task not found.\n");
}
// Delete top priority task
void deleteTask() {
if (heapSize == 0) {
printf("No tasks to delete.\n");
return;
}
Task top = extractMin();
printf("Deleted Task: %s (Priority %d)\n", top.title, top.priority);
}
// ---------------- FILE HANDLING ----------------
void saveTasks() {
FILE *fp = fopen("tasks.txt", "w");
if (!fp) {
printf("Error saving file!\n");
return;
}
for (int i = 0; i < heapSize; i++) {
fprintf(fp, "%d,%s,%d,%d\n",
heap[i].id,
heap[i].title,
heap[i].priority,
heap[i].completed);
}
fclose(fp);
}
void loadTasks() {
FILE *fp = fopen("tasks.txt", "r");
if (!fp) return; // no saved file
while (fscanf(fp, "%d,%[^,],%d,%d\n",
&heap[heapSize].id,
heap[heapSize].title,
&heap[heapSize].priority,
&heap[heapSize].completed) != EOF) {
heapSize++;
}
fclose(fp);
}