-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask Application.c
More file actions
531 lines (430 loc) · 13 KB
/
Task Application.c
File metadata and controls
531 lines (430 loc) · 13 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
Beykoz University - Computer Enginerring
Name: Mert Altuntas
ID : 1804010005
It's not the final version. There are more things to do.
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//element of priority queue
struct node
{
FILE* fp;
struct node* next;
struct rec* data;
};
//node data strcture
struct rec
{
char name[30];
char time[6];
};
void addfirstentry(struct node **p);
void addentry(struct node **p);
int comparetime(char t1[],char t2[]);
void viewlist(struct node *p);
void delete_entry(struct node **p);
void toptasks(struct node *p,int n);
void viewtask(struct node *p);
void display_structure(struct rec *r, int i);
//***************************************************** INTRO FUNCTION ********************************************************
void intro_ekrani(){
printf("\n----------------------------------\n");
printf("| Software Engineering Project |");
printf("\n----------------------------------\n");
printf("| Made By: Mert Altuntas |");
printf("\n----------------------------------\n");
printf("|\t ID: 1804010005 |");
printf("\n----------------------------------\n\n\n");
printf("Loading ");
int j, a;
for (a = 0; a <= 5; a++) {
printf(".");
for (j = 0; j <= 120000000; j++);
}
system("cls");
}
//***************************************************** DISPLAY STRUCTURE *******************************************************
void display_structure(struct rec *r, int i)
{
//displays structure elements
printf("%d. %s\t %s\n",i,r->name,r->time);
}
//***************************************************** TOP N TASKS *************************************************************
void toptasks(struct node *p,int n) //prints top n tasks from file
{
int i=1;
printf("\n\n TASK NAME\tDEADLINE\n\n");
while(p!=NULL&&i<=n)
{
display_structure(p->data,i);
i++;
p=p->next;
}
}
//***************************************************** TASK VIEWER ********************************************************
void viewtask(struct node *p) //view task by name
{
int flag=0;
printf("\nEnter the task you want to view: ");
char t[20];
scanf("%s", t);
char *task=t;
strcat(task, ".txt"); //task file name
printf("\n\n TASK NAME\tDEADLINE\n\n");
while(p!=NULL)
{
if(strcmp(p->data->name, task )==0)
{
display_structure(p->data,1);
flag=1;
break;
}
else
{
p=p->next;
}
}
if(flag==0)
{
printf("\nEntered task not found.");
}
}
void viewlist(struct node *q) //prints all tasks
{
printf("\n\n TASK NAME\tDEADLINE\n\n");
int i=1;
while(q!=NULL)
{
display_structure(q->data,i);
i++;
q=q->next;
}
}
//***************************************************** TIME COMPARING ********************************************************
int comparetime(char t1[],char t2[]) //returns 1 if t1 is before t2 time
{
int tvalue1=0;
int tvalue2=0;
char hours1[3];
hours1[0]=t1[0];
hours1[1]=t1[1];
hours1[2]='\0';
char minutes1[3];
minutes1[0]=t1[3];
minutes1[1]=t1[4];
minutes1[2]='\0';
tvalue1= atoi(hours1)*60+atoi(minutes1); //calculates total value in minutes (from 00:00 being 0, 1:00 being 60 and so on)
char hours2[3];
hours2[0]=t2[0];
hours2[1]=t2[1];
hours2[2]='\0';
char minutes2[3];
minutes2[0]=t2[3];
minutes2[1]=t2[4];
minutes2[2]='\0';
tvalue2= atoi(hours2)*60+atoi(minutes2); //calculates total value in minutes (from 00:00 being 0, 1:00 being 60 and so on)
if(tvalue1<tvalue2)
return 1;
else
return 0;
}
//***************************************************** ADDING NEW TASK ********************************************************
void addentry(struct node **p)
{
if(*p==NULL)
{
//adding first entry
struct node *record;
struct rec *newrec= (struct rec*)malloc(sizeof(struct rec));
record=(struct node*)malloc(sizeof(struct node));
record->next=NULL;
printf("\nPlease enter the details:\nEntry name: ");
scanf("%s",newrec->name);
printf("\nDeadline time[hh:mm]: ");
scanf("%s",newrec->time);
record->data=newrec;
strcat(record->data->name,".txt");
record->fp=fopen(record->data->name,"a+");
//ADDING TO MOTHER file
FILE *f= fopen("mother.txt","a");
char filename[20]="";
strcat(filename,(record->data->name));
strcat(filename,"\n");
fputs(filename, f);
fclose(f);
FILE *file= fopen(record->data->name,"wb+");
fwrite(newrec,sizeof(struct rec), 1,file);
rewind(file);
struct rec getrec; //to get structure from file and print
fread(&getrec,sizeof(struct rec), 1,file);
printf("\nRecord name: %s\tRecorded time: %s",getrec.name, getrec.time);
if(record->fp==NULL)
{
printf("\nSYSTEM ERROR.");
printf("\nPRESS ANY KEY TO EXIT");
return ;
}
else{
printf("\nRECORD ADDED SUCCESSFULLY\n\n");
}
*p=record; //head of the queue is created
fclose(file);
fclose(record->fp);
}
else
{
struct node *record;
struct rec *newrec= (struct rec*)malloc(sizeof(struct rec));
record=(struct node*)malloc(sizeof(struct node));
record->next=NULL;
record->fp=NULL;
record->data=NULL;
printf("\nPlease enter the details:\nEntry name: ");
scanf("%s",newrec->name);
strcat(newrec->name,".txt");
//ADDING TO MOTHER file
FILE *f= fopen("mother.txt", "a");
char filename[20]="";
strcat(filename, newrec->name);
strcat(filename, "\n");
fputs(filename, f);
fclose(f);
printf("\nDeadline time[hh:mm]: ");
scanf("%s",newrec->time);
record->data=newrec;
record->fp=fopen(record->data->name,"w+");
printf("\nFile created");
FILE *file= fopen(record->data->name,"wb+");
fwrite(newrec,sizeof(struct rec), 1,file);
rewind(file);
fclose(file);
struct node *q;
q=*p;
struct node *prev;
prev=NULL;
while(q!=NULL && comparetime(q->data->time,newrec->time)) //priority wise insertion (based on time)
{
prev=q;
q=q->next;
}
if(prev==NULL)
{
record->next=q;
*p=record;
}
else
{
record->next=prev->next;
prev->next=record;
}
if(record->fp==NULL)
{
printf("\nSYSTEM ERROR.");
printf("\nPRESS ANY KEY TO EXIT");
return ;
}
else
{
printf("\nRECORD ADDED SUCCESSFULLY\n\n");
}
fclose(record->fp);
}
}
//***************************************************** DELETING TASK ********************************************************
void delete_entry(struct node **p) //deletes based on task name
{
char del[20];
struct node *q,*prev;
int flag=0;
if(*p==NULL)
{
printf("\nNo files exist.");
return;
}
printf("\nPlease enter the name of the entry to be deleted: ");
scanf("%s",del);
char *delname=del;
strcat(delname, ".txt");
prev=NULL;
q=*p;
while((q!=NULL)&&(strcmp(q->data->name,delname)!=0))
{
prev=q;
q=q->next;
}
if(strcmp(q->data->name,delname)==0)
{
flag=1;
if(remove(q->data->name)==0)
{
char curname[20];
FILE *f=fopen("mother.txt", "r");
rewind(f);
FILE *f2=fopen("temp.txt","a");
while((!feof(f))&&fscanf(f,"%s",curname))
{
char *c= curname;
if(strcmp(c,delname)!=0)
{
if(!feof(f))
fprintf(f2,"%s\n",c);
}
else{
fprintf(f2,"");
}
}
fclose(f);
remove("mother.txt");
fclose(f2);
rename("temp.txt","mother.txt");
if(prev!=NULL)
{
prev->next=q->next; //deleting from our list
}
else
{
*p=q->next;
}
free(q);
printf("\nDELETED SUCCESFULLY.");
return;
}
else{
printf("\nDeletion unsuccessful");
return;
}
}
else
{
printf("\nFile not found.\n");
}
if(flag==0)
{
printf("\nFile not found.\n");
}
}
//***************************************************** MAIN FUNCTION ********************************************************
int main()
{
intro_ekrani();
struct node *p=NULL;
//p=initialize_list(&p);
FILE *fp= fopen("mother.txt","r"); //stores all names of files (tasks)
if(fp==NULL)
{
printf("\n\nFirst run, mother file not present.\n");
//First entry into the file and *p is NULL
}
else
{ rewind(fp);
//Inserting first file name into linked list from mother file
struct node *record=(struct node*)malloc(sizeof(struct node));
struct rec newrec;
//gets file NAME from mother.txt
char curname[20];
fscanf(fp,"%s",curname);
char *c= curname;
FILE *file= fopen(c,"r");
if(!file)
{
printf("\nError\n");
exit(0);
}
fread(&newrec,sizeof(struct rec), 1,file); //stores all data from curname file in newrec structure.
record->fp= file;
record->data=&newrec;
record->next=NULL;
p=record; //first insertion
fclose(file);
struct node *q;
struct node *prev;
while(fscanf(fp,"%s",curname)&&!feof(fp))
{
struct node *r=(struct node*)malloc(sizeof(struct node));
struct rec newr;
//gets file NAME from mother.txt
char *c= curname;
FILE *file= fopen(c, "r");
fread(&newr,sizeof(struct rec), 1,file); //stores all data from curname file in newrec structure.
r->fp= file;
r->data=&newr;
r->next=NULL;
q=p;
prev=NULL;
fclose(file);
while(q!=NULL && comparetime(q->data->time,r->data->time))
{
prev=q;
q=q->next;
}
if(prev==NULL)
{
r->next=p; //insert before First
p=r;
}
else
{
r->next=q;
prev->next=r;
}
strcpy(curname,"");
}
fclose(fp);
}
int ch=0;
int n;
while(1)
{
x:
printf("\n-------------------------------------------------\n");
printf("\n\n\t\tMAIN MENU:");
printf("\n\n\t1. ADD TASK");
printf("\n\t2. VIEW ALL TASKS");
printf("\n\t3. VIEW TOP TASK");
printf("\n\t4. DELETE TASK BY NAME");
printf("\n\t5. VIEW TASK BY NAME");
printf("\n\t6. SEE TOP n TASKS");
printf("\n\t7. EXIT");
printf("\n\n-------------------------------------------------\n\n");
printf("\n\n\tENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
addentry(&p);
break;
case 2:
viewlist(p);
break;
case 3:
printf("\n\nTOP TASK:");
toptasks(p, 1);
break;
case 4:
delete_entry(&p);
break;
case 5:
viewtask(p);
break;
case 6:
printf("\nEnter the number of tasks you want to see:");
scanf("%d",&n);
printf("\n\nThe top %d tasks are: \n", n);
toptasks(p,n);
break;
case 7:
printf("\n\n\tTHANK YOU\n");
exit(0);
default:
printf("\nWRONG ENTRY");
printf("\nPRESS ANY KEY TO TRY AGAIN");
getch();
goto x;
break;
}
}
exit(0);
return 0;
}