-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
776 lines (749 loc) · 21.4 KB
/
main.c
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
struct Candidate
{
int id;
int age;
char name[100];
int numberOfVotes;
char party[100];
};
struct Voter
{
int id;
int age;
char name[100];
int voteCasted;
};
int isAdmin = 0;
char candidateFileName[] = "candidates.csv";
char votersFileName[] = "voters.csv";
char metaFileName[] = "election.meta";
char *delimiter = ",";
//BACKEND_BEGIN
struct Voter *ReadCSVVoters(char filename[], char *delimiter, int lines)
{
FILE *file = fopen(filename, "r");
const unsigned int skip_header = 1;
int count = lines;
if (ferror(file))
{
perror("Error : ");
return NULL;
}
else
{
fseek(file, 0, SEEK_SET);
struct Voter *voter = (struct Voter *)malloc((count + 1) * sizeof(struct Voter));
if (voter == NULL)
{
perror("Error : ");
return NULL;
}
unsigned int row = 0;
unsigned int i = 0;
while (!feof(file))
{
int buffer_size = 4096;
char buffer[buffer_size];
fgets(buffer, buffer_size, file);
if (row >= skip_header && strlen(buffer) > 0)
{
char *token = strtok(buffer, delimiter);
int pos = -1;
while (token != NULL)
{
switch (pos)
{
case -1:
voter[i].id = atoi(token);
break;
case 0:
voter[i].age = atoi(token);
break;
case 1:
strcpy(voter[i].name, token);
break;
case 2:
token[strcspn(token,"\n")] = 0;
voter[i].voteCasted = atoi(token);
break;
default:
break;
}
token = strtok(NULL, delimiter);
pos++;
}
i++;
}
row += 1;
}
fclose(file);
return voter;
}
}
struct Candidate *ReadCSVCandidates(char filename[], char *delimiter, int lines)
{
FILE *file = fopen(filename, "r");
const unsigned int skip_header = 1;
int count = lines;
if (ferror(file))
{
perror("Error : ");
return NULL;
}
else
{
fseek(file, 0, SEEK_SET);
struct Candidate *voter = (struct Candidate *)malloc(
(count + 1) * sizeof(struct Candidate));
if (voter == NULL)
{
perror("Error : ");
return NULL;
}
unsigned int row = 0;
unsigned int i = 0;
while (!feof(file))
{
int buffer_size = 4096;
char buffer[buffer_size];
fgets(buffer, buffer_size, file);
if (row >= skip_header && strlen(buffer) > 0)
{
char *token = strtok(buffer, delimiter);
int pos = -1;
while (token != NULL)
{
switch (pos)
{
case -1:
voter[i].id = atoi(token);
break;
case 0:
voter[i].age = atoi(token);
break;
case 1:
strcpy(voter[i].name, token);
break;
case 2:
voter[i].numberOfVotes = atoi(token);
break;
case 3:
token[strcspn(token,"\n")] = 0;
strcpy(voter[i].party, token);
break;
default:
break;
}
token = strtok(NULL, delimiter);
pos++;
}
i++;
}
row += 1;
}
fclose(file);
return voter;
}
}
int GetNumberOfLines(char filename[])
{
FILE *f = fopen(filename, "r");
int count = 0;
while (!feof(f))
{
for (int c = getc(f); c != EOF; c = getc(f))
{
if (c == '\n')
count++;
}
}
fclose(f);
return count;
}
void DisplayDataVoters(struct Voter *v, int lines)
{
printf("=============Voters==============\n");
printf("%s \t%s \t%-2s\n", "ID", "Age", "Name");
printf("================================\n");
for (int i = 0; i < lines; i++)
{
printf("%2d \t%2d \t%-2s\n", v[i].id, v[i].age,
v[i].name);
}
printf("=================================\n");
}
void DisplayDataCandidates(struct Candidate *v, int lines)
{
printf("====================Candidates====================\n");
printf("%s \t%s \t%-2s \t%-2s\n", "ID", "Age", "Name", "Party");
printf("==================================================\n");
for (int i = 0; i < lines; i++)
{
printf("%2d \t%2d \t%-2s \t%-2s\n", v[i].id, v[i].age,
v[i].name, v[i].party);
}
printf("==================================================\n");
}
struct Voter *AddNewValueVoter(struct Voter *voters, struct Voter v, int *lines)
{
int l = *lines;
int i = 0;
struct Voter *new = realloc(voters, (l + 2) * sizeof(struct Voter));
if (new == NULL)
{
free(voters);
perror("Error : ");
}
else
{
new[l].age = v.age;
new[l].id = v.id;
new[l].voteCasted = v.voteCasted;
strcpy(new[l].name, v.name);
l += 1;
*lines = l;
return new;
}
}
struct Candidate *AddNewValueCandidate(struct Candidate *c,
struct Candidate v, int *lines)
{
int l = *lines;
int i = 0;
struct Candidate *new = realloc(c, (l + 2) * sizeof(struct Candidate));
if (new == NULL)
{
free(c);
perror("Error : ");
}
else
{
new[l].age = v.age;
new[l].id = v.id;
new[l].numberOfVotes = v.numberOfVotes;
strcpy(new[l].name, v.name);
strcpy(new[l].party, v.party);
l += 1;
*lines = l;
return new;
}
}
void CastVote(int idVoter, int idCandidate, struct Voter *v, struct Candidate *c, int linesVoters, int linesCandidates)
{
for (int j = 0; j < linesVoters; j++)
{
if (v[j].id == idVoter)
{
for (int i = 0; i < linesCandidates; i++)
{
printf("ID : %d\n", c[i].id);
if (c[i].id == idCandidate)
{
c[i].numberOfVotes++;
v[j].voteCasted = 1;
printf("Vote casted by %s to %s of %s\n",
v[j].name, c[i].name, c[i].party);
return;
}
}
}
}
printf("Error : No such candidate exists!!\n");
}
void ShowResults(struct Candidate *c, int lines)
{
printf("============Results============\n");
printf("%s \t%-2s \t%-2s\n", "Name", "Party", "Votes");
for (int i = 0; i < lines; i++)
{
printf("%s \t%-2s \t%-2d\n", c[i].name,
c[i].party, c[i].numberOfVotes);
}
printf("===============================\n");
}
void SaveValuesVoters(char filename[], struct Voter *data, int lines)
{
FILE *f = fopen(filename, "w");
if (!f)
{
perror("Error ");
printf("Cannot save file %s\n", filename);
return;
}
fprintf(f, "id,age,name,voted\n");
for (int i = 0; i < lines; i++)
{
//printf("%s\n",data[i].uname);
if (i == lines - 1)
{
fprintf(f, "%d,%d,%s,%d", data[i].id, data[i].age, data[i].name,data[i].voteCasted);
continue;
}
fprintf(f, "%d,%d,%s,%d\n", data[i].id, data[i].age, data[i].name,data[i].voteCasted);
}
printf("File %s saved successfully\n", filename);
fclose(f);
}
void SaveValuesCandidates(char filename[], struct Candidate *data, int lines)
{
FILE *f = fopen(filename, "w");
if (!f)
{
perror("Error ");
printf("Cannot save file %s\n", filename);
return;
}
fprintf(f, "id,age,name,votes,party\n");
for (int i = 0; i < lines; i++)
{
//printf("%s\n",data[i].uname);
if (i == lines - 1)
{
fprintf(f, "%d,%d,%s,%d,%s", data[i].id, data[i].age, data[i].name,
data[i].numberOfVotes, data[i].party);
continue;
}
fprintf(f, "%d,%d,%s,%d,%s\n", data[i].id, data[i].age, data[i].name,
data[i].numberOfVotes, data[i].party);
}
printf("File %s saved successfully\n", filename);
fclose(f);
}
struct Voter *DeleteDataVoter(struct Voter *v, int *lines, int id)
{
int l = *lines;
struct Voter *new = v;
for (int i = 0; i < l; i++)
{
if (v[i].id == id)
{
for (int j = i; j < l - 1; j++)
{
new[j] = new[j + 1];
}
l -= 1;
new = realloc(new, (l + 1) * sizeof(struct Voter));
*lines = l;
return new;
}
}
printf("No such entry exists!!\n");
return v;
}
struct Candidate *DeleteDataCandidate(struct Candidate *v, int *lines, int id)
{
int l = *lines;
struct Candidate *new = v;
for (int i = 0; i < l; i++)
{
if (v[i].id == id)
{
for (int j = i; j < l - 1; j++)
{
new[j] = new[j + 1];
}
l -= 1;
new = realloc(new, (l + 1) * sizeof(struct Voter));
*lines = l;
return new;
}
}
printf("No such entry exists!!\n");
return v;
}
int fileExists(char fname[])
{
FILE *f = fopen(fname, "r");
if (f != NULL)
{
fclose(f);
return 1;
}
return 0;
}
struct Voter ReturnLoggedInVoter(int id,
struct Voter *v, int lines)
{
for (int i = 0; i < lines; i++)
{
if (v[i].id == id)
{
return v[i];
}
}
struct Voter temp;
temp.id = -1;
return temp;
}
struct Candidate ReturnLoggedInCandidate(int id,
struct Candidate *v, int lines)
{
for (int i = 0; i < lines; i++)
{
if (v[i].id == id)
{
return v[i];
}
}
struct Candidate temp;
temp.id = -1;
return temp;
}
void SaveMetaFile(char fname[], int status)
{
FILE *f = fopen(fname, "w");
if (!f)
{
perror("Error ");
printf("Cannot save file %s\n", fname);
return;
}
fprintf(f, "%d", status);
printf("File %s saved successfully\n", fname);
fclose(f);
}
//BACKEND_END
void AdminUI(struct Voter *v, struct Candidate *c,
int *lv, int *lc, int *estat)
{
char uname[50], passwd[50];
int trialsLeft = 3;
printf("Welcome admin!!\n");
printf("Please enter Admin Username and Password to authenticate\n");
do
{
fflush(stdin);
printf("Username : ");
fgets(uname, 50, stdin);
uname[strcspn(uname, "\n")] = '\0';
printf("Password : ");
fgets(passwd, 50, stdin);
passwd[strcspn(passwd, "\n")] = '\0';
if (strcmp(uname, "admin") == 0 && strcmp(passwd, "admin") == 0)
{
printf("Access granted(admin)\n");
isAdmin = 1;
}
else
{
trialsLeft--;
printf("Access denied(admin), You have %d chances left\n", trialsLeft);
}
} while (trialsLeft > 0 && isAdmin == 0);
if (trialsLeft <= 0)
{
return;
}
printf("WARNING : You have admin access now, you can create and delete accounts\nUSE YOUR POWERS WISELY\n");
trialsLeft = 3;
int choice = 0;
char temp[100];
do
{
if (*lv == 0)
{
printf("WARNING : Voter list is empty, add new voters!!");
}
if (*lc == 0)
{
printf("WARNING : Candidate list is empty, add new candidates!!");
}
printf("Options :-\n");
printf("1> Display Voters\n2> Display Candidates\n3> Add Voter\n4> Add Candidate\n");
printf("5> Delete Candidate\n6> Delete Voter\n7> Save Changes\n8> Start Election\n9> Exit");
printf("\nEnter your choice here(%d chances left after wrong input) : ", trialsLeft);
scanf("%d", &choice);
int id;
switch (choice)
{
case 1:
DisplayDataVoters(v, *lv);
break;
case 2:
DisplayDataCandidates(c, *lc);
break;
case 3:
if ((*estat) == 1)
{
printf("Error : New data cannot be entered while elections are going on\n");
continue;
}
printf("Enter details of new voter\n");
struct Voter newVoter;
newVoter.id = *lv + 1;
newVoter.voteCasted = 0;
printf("Age : ");
scanf("%d", &newVoter.age);
if (newVoter.age < 18)
{
printf("Error : Ages below 18 are not allowed!!\n");
continue;
}
printf("Name : ");
fflush(stdin);
fgets(temp, 100, stdin);
temp[strcspn(temp, "\n")] = '\0';
strcpy(newVoter.name, temp);
v = AddNewValueVoter(v, newVoter, lv);
break;
case 4:
if (*estat == 1)
{
printf("Error : New data cannot be entered while elections are going on\n");
continue;
}
printf("Enter details of new candidate\n");
struct Candidate newc;
newc.id = *lc + 1;
newc.numberOfVotes = 0;
printf("Age : ");
scanf("%d", &newc.age);
fflush(stdin);
printf("Name : ");
fgets(temp, 100, stdin);
temp[strcspn(temp, "\n")] = '\0';
strcpy(newc.name, temp);
printf("Username : ");
fgets(temp, 100, stdin);
temp[strcspn(temp, "\n")] = '\0';
strcpy(newc.party, temp);
printf("Password : ");
c = AddNewValueCandidate(c, newc, lc);
break;
case 5:
if (*estat == 1)
{
printf("Error : New data cannot be entered while elections are going on\n");
continue;
}
printf("Enter id of candidate to delete : ");
scanf("%d", &id);
fflush(stdin);
c = DeleteDataCandidate(c, lc, id);
break;
case 6:
if (*estat == 1)
{
printf("Error : New data cannot be entered while elections are going on\n");
continue;
}
printf("Enter id of d to delete : ");
scanf("%d", &id);
fflush(stdin);
v = DeleteDataVoter(v, lv, id);
break;
case 7:
printf("Which data do you want to save?\n");
printf("1> Voter's\n");
printf("2> Candidate's\n");
printf("3> Both\n");
int chc;
scanf("%d", &chc);
fflush(stdin);
switch (chc)
{
case 1:
SaveValuesVoters(votersFileName, v, *lv);
break;
case 2:
SaveValuesCandidates(candidateFileName, c, *lc);
break;
case 3:
SaveValuesVoters(votersFileName, v, *lv);
SaveValuesCandidates(candidateFileName, c, *lc);
default:
printf("Wrong input!!");
break;
}
break;
case 8:
printf("Elections started...\nNo change to data is possible now!!\n");
printf("Select this option to stop elections\n");
*estat = *estat == 1 ? 0 : 1;
break;
case 9:
printf("Exiting to main menu...\n");
break;
default:
trialsLeft--;
printf("Wrong Input, %d chances left", trialsLeft);
break;
}
} while (choice != 9);
}
void VoterUI(struct Voter *v, struct Candidate *c,
int *lv, int *lc, int *estat)
{
char uname[50], passwd[50];
int trialsLeft = 3;
int currId = -1;
int loggedIn = 0;
int voted = 0;
struct Voter cv;
printf("Welcome voter!!\n");
printf("Please enter your ID number : ");
int inputId;
scanf("%d", &inputId);
fflush(stdin);
cv = ReturnLoggedInVoter(inputId, v, *lv);
if (cv.id == -1)
{
printf("Access denied(voter), returning to main menu...\n");
return;
}
else
{
printf("Scanning Adhar Card....\n");
//sleep(2);
printf("Access granted(voter)\n");
loggedIn = 1;
currId = cv.id;
voted = cv.voteCasted;
}
printf("Welcome %s!!\n", cv.name);
printf("Election Status : %s", *estat == 1 ? "Ongoing\n" : "Stopped\n");
trialsLeft = 3;
int choice = -1;
char temp[100];
do
{
printf("Options:-\n");
printf("1> Vote\n2> Display Candidates\n");
printf("3> Exit\n");
printf("Enter your choice here(%d chances left after wrong input) : ", trialsLeft);
scanf("%d", &choice);
//fflush(stdin);
switch (choice)
{
case 1:
if (*estat == 0)
{
printf("Elections are not open yet...\n");
continue;
}
if (voted == 1)
{
printf("You've already voted...\n");
continue;
}
DisplayDataCandidates(c, *lc);
printf("Enter Id of candidate you want to vote for : ");
int id;
scanf("%d", &id);
fflush(stdin);
CastVote(currId, id, v, c, *lv, *lc);
voted = 1;
break;
case 2:
DisplayDataCandidates(c, *lc);
break;
case 3:
printf("Exiting...\n");
break;
default:
trialsLeft--;
printf("Wrong Input, %d chances left\n", trialsLeft);
break;
}
} while (choice < 3 && trialsLeft > 0);
}
int main()
{
int role = 0;
int electionBegin = 0;
struct Voter *voter;
struct Candidate *candidate;
printf("Loading data...\n");
if (!fileExists(metaFileName))
{
printf("WARNING : Reqired file %s not found, creating...\n", metaFileName);
FILE *f = fopen(metaFileName, "w");
if (f == NULL)
{
printf("ERROR : Unable to create file, exiting...\n");
return 1;
}
fprintf(f, "%d", electionBegin);
fclose(f);
printf("Done...\n");
}
else
{
FILE *f = fopen(metaFileName, "r");
char temp[2];
fgets(temp, 2, f);
electionBegin = atoi(temp);
fclose(f);
}
printf("%d\n", electionBegin);
if (!fileExists(votersFileName))
{
printf("WARNING : Reqired file %s not found, creating...\n", votersFileName);
FILE *f = fopen(votersFileName, "w");
if (f == NULL)
{
printf("ERROR : Unable to create file, exiting...\n");
return 1;
}
fprintf(f, "id,age,name,voted,username,password");
fclose(f);
printf("Done...\n");
}
if (!fileExists(candidateFileName))
{
printf("WARNING : Reqired file %s not found, creating...\n", candidateFileName);
FILE *f = fopen(candidateFileName, "w");
if (f == NULL)
{
printf("ERROR : Unable to create file, exiting...\n");
return 1;
}
fprintf(f, "id,age,name,votes,party,username,password");
fclose(f);
printf("Done...\n");
}
int linesVoters = GetNumberOfLines(votersFileName);
int linesCandidates = GetNumberOfLines(candidateFileName);
voter = ReadCSVVoters(votersFileName, delimiter, linesVoters);
candidate = ReadCSVCandidates(candidateFileName, delimiter, linesCandidates);
printf("Welcome to Election Management System!!\n");
DisplayDataVoters(voter,linesVoters);
printf("There are a lot of features and each type (admin,voter) have different menus\n");
do
{
printf("Choose your role :- \n");
printf("1 > Admin\n2> Voter\n3> Show Results\n4> Exit\n");
printf("Select number before role(wrong input will terminate program) : ");
scanf("%d", &role);
switch (role)
{
case 1:
AdminUI(voter, candidate, &linesVoters, &linesCandidates, &electionBegin);
break;
case 2:
VoterUI(voter, candidate, &linesVoters, &linesCandidates, &electionBegin);
break;
case 3:
ShowResults(candidate, linesCandidates);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Wrong input, exiting...\n");
break;
}
} while (role < 4 && role > 0);
fflush(stdin);
printf("Press any key to exit...\n");
getch();
SaveMetaFile(metaFileName, electionBegin);
SaveValuesVoters(votersFileName, voter, linesVoters);
SaveValuesCandidates(candidateFileName, candidate, linesCandidates);
return 0;
}