-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAIN.H
449 lines (410 loc) · 12.1 KB
/
MAIN.H
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
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct currentValidID{
int year;
char branch[6];
int totalVoters;
};
typedef struct candidate{
int cid;
char cname[20];
int votes;
}CANDIDATE;
//GLOBALS --------------------------------------------------------
struct currentValidID currentValidID; //stores current Valid user ID parameters
CANDIDATE candidateArray[20]; //to store information all candidates
int numberOfCandidates; //Total number of candidates standing for election
char studentVotes[200]; //to store information of votes given by each student
//----------------------------------------------------------------
//To extract year from userID -- For example, userID:2018btecs00064 year:2018
int extractYear(char userID[15])
{
int year=0;
char tmp;
for(int i=0;i<4;i++){
tmp=userID[i];
year=(year*10)+(tmp-48);
}
return year;
}
int extractRollNo(char userID[15])
{
int rollno=0;
char tmp;
for(int i=9;i<14;i++){
tmp=userID[i];
rollno=(rollno*10)+(tmp-48);
}
return rollno;
}
//Will check whether the global branch code and inputed branch code is matching or not
int checkBranchCode(char userID[15])
{
char branchCode[6];
for(int i=4;i<9;i++){
branchCode[i-4]=userID[i];
}
branchCode[5]='\0';
if(strcmp(branchCode,currentValidID.branch)==0)
return 1;
else
return 0;
}
int authenticateAdmin(){
char username[15], password[6];
printf("\nEnter username: ");
scanf("%s",username);
if((strcmp(username,"Admin"))!=0)
return 0;
else
{
printf("Enter Password: ");
int i=0;
for(i=0;i<5;i++)
{
password[i]=getch();
printf("%c",'*');
}
password[i]='\0';
if((strcmp(password,"admiN"))!=0)
return 0;
}
return 1;
}
void banID(){
printf("\nCreating Banned.txt...\n");
FILE *fp=fopen("Banned.txt", "w");
if(fp==NULL){
printf("Error: Banned.txt not created.\n");
fclose(fp);
return;
}
printf("Just Enter last roll no to ban\nPress 0 to exit... ");
int input;
while(1){
printf("\nEnter Number: ");
scanf("%d",&input);
if(input==0)
break;
studentVotes[input-1]='$';
fprintf(fp,"%d\n",input);
}
fclose(fp);
printf("Created Successfully\n");
}
void createCandidateFiles(){
printf("\nCreating candidate files...\n");
FILE *fp;
char filename[20];
for(int i = 1;i <= numberOfCandidates; i++){
sprintf(filename,"candidate%d.txt",i);
fp=fopen(filename,"w");
fprintf(
fp,"0\n%s",
candidateArray[i-1].cname
);
fclose(fp);
}
printf("Created Files successfully\n");
}
void deleteIllegalVote(char userID[15])
{
FILE *fp,*fcp;
char filename[20];
char line[20];
int location = extractRollNo(userID);
sprintf(filename,"candidate%d.txt",candidateArray[studentVotes[location-1]-49].cid);
candidateArray[studentVotes[location-1]-49].votes--;
studentVotes[location-1]='0';
if ((fp = fopen(filename,"r")) == NULL)
{
printf("\nFile cannot be opened...Operation Failed");
return;
}
printf("\nDeleting in process...\n ");
if ((fcp = fopen("tmp.txt","w")) == NULL)
{
printf("\nFile cannot be opened...Operation Failed");
return;
}
while (!feof(fp))
{
fscanf(fp,"%s",line);
fprintf(fcp,"%s\n",line);
}
fclose(fp);
fclose(fcp);
if ((fp = fopen(filename,"w")) == NULL)
{
printf("\nFile cannot be opened...Operation Failed");
return;
}
int numFromFile;
char cnameFromFile[20];
fcp = fopen("tmp.txt","r");
fscanf(fcp,"%d",&numFromFile);
fprintf(fp,"%d",numFromFile-1);
fscanf(fcp,"%s",cnameFromFile);
fprintf(fp,"\n%s",cnameFromFile);
while(!feof(fcp)){
fscanf(fcp,"%d",&numFromFile);
if(numFromFile!=location)
fprintf(fp,"\n%d",numFromFile);
}
fclose(fp);
fclose(fcp);
remove("tmp.txt");
printf("\nVote deleted successfully\nPress any key to continue...");
getch();
}
int getWinner(){
int maxV = -1;
int winnerCid;
for(int i = 0;i < numberOfCandidates; i++){
if(candidateArray[i].votes > maxV) {
winnerCid = candidateArray[i].cid;
maxV = candidateArray[i].votes;
}
else if(candidateArray[i].votes == maxV) {
return -1;
}
}
return winnerCid;
}
void initiateNewElection()
{
printf("\nNew Election Initiation:\n");
printf("\nElections for which Year: ");
scanf("%d",¤tValidID.year);
printf("Enter branch code:");
scanf("%s",currentValidID.branch);
printf("Enter max roll no.:");
scanf("%d",¤tValidID.totalVoters);
printf("Enter the no. of candidates:");
scanf("%d",&numberOfCandidates);
for (int i = 0; i < currentValidID.totalVoters; i++)
{
studentVotes[i] = '0';
}
for (int i = 0;i < numberOfCandidates; i++)
{
candidateArray[i].cid=i+1;
printf("Enter name of candidate %d: ",i+1);
scanf(" %s",candidateArray[i].cname);
candidateArray[i].votes=0;
}
return;
}
void saveElectionInfoInFile(){
printf("Saving Election Info in File...\n");
FILE *fp = fopen("ElectionInfo.txt", "w");
if(fp==NULL)
{
printf("\nError in file creation\n");
fclose(fp);
return;
}
fprintf(
fp,"%d\n%s\n%d\n%d",
currentValidID.year,
currentValidID.branch,
currentValidID.totalVoters,
numberOfCandidates
);
fclose(fp);
printf("Saved Successfully : )");
}
void loadElectionInfoFromFile()
{
FILE *f1,*f2,*f3;
f1=fopen("ElectionInfo.txt","r");
if(f1==NULL)
printf("Not Exist");
fscanf(f1,"%d",¤tValidID.year);
fseek(f1,2,SEEK_CUR);
fscanf(f1,"%s",currentValidID.branch);
fseek(f1,2,SEEK_CUR);
fscanf(f1,"%d",¤tValidID.totalVoters);
fseek(f1,2,SEEK_CUR);
fscanf(f1,"%d",&numberOfCandidates);
fclose(f1);
//load candidates info and student votes
for (int i = 0; i < currentValidID.totalVoters; i++)
{
studentVotes[i] = '0';
}
for(int i=1;i<=numberOfCandidates;i++)
{
int location;
char filename[20];
sprintf(filename,"candidate%d.txt",i);
f2=fopen(filename,"r+");
candidateArray[i-1].cid=i;
fscanf(f2,"%d",&candidateArray[i-1].votes);
fscanf(f2,"%s",candidateArray[i-1].cname);
while(!feof(f2)){
fscanf(f2,"%d",&location);
studentVotes[location-1] = i+48;
}
fclose(f2);
}
//load banned votes
int location;
f3=fopen("banned.txt","r+");
while(!feof(f3)){
fscanf(f3,"%d",&location);
studentVotes[location-1] = '$';
}
fclose(f3);
}
void adminPanel()
{
while(1){
if(authenticateAdmin()!=1){
printf("\n Wrong Username or Password \n");
break;
}
printf("\n\nLOGGED IN SUCCESSFULLY (Press Enter)");
getch();
while(1)
{
char inputID[15];
char input;char banInp;
int WinnerCid, totalVotedNow=0;
printf("\n1.New Election\n2.Continue Previous Election\n3.Delete Illegal Vote\n4.Ban User IDs\n5.Result\n6.Logout\nOption:");
scanf(" %c",&input);
switch(input)
{
case '1':
initiateNewElection();
saveElectionInfoInFile();
createCandidateFiles();
break;
case '2':
loadElectionInfoFromFile();
break;
case '3':
printf("\nEnter user ID to delete its vote: ");
scanf("%s",inputID);
deleteIllegalVote(inputID);
break;
case '4':
printf("Do you want to ban particular ID's?\nPress 1 if yes or any other key to continue...");
scanf(" %c",&banInp);
if(banInp=='1'){
banID();
}
break;
case '5':
WinnerCid = getWinner();
if(WinnerCid != -1){
printf("\nWinner is %s with %d votes\n",candidateArray[WinnerCid-1].cname,candidateArray[WinnerCid-1].votes);
}
else{
printf("\nIts A TIE");
}
printf("\nFull Result\n");
for(int i=0;i<numberOfCandidates;i++){
totalVotedNow+=candidateArray[i].votes;
printf("%d. %s -> %d votes\n",candidateArray[i].cid,candidateArray[i].cname,candidateArray[i].votes);
}
printf("\nVoting Percentage: %d %%\n\n",(totalVotedNow*100)/currentValidID.totalVoters);
break;
case '6':
return;
default:
printf("Invalid Option");
getch();
}
}
}
};
int isValid(char userID[15])
{
if(strlen(userID)!=14)
return 0;
int inputedYear=extractYear(userID);
int inputedRollNo = extractRollNo(userID);
if(inputedYear!=currentValidID.year || checkBranchCode(userID)!=1 || inputedRollNo>currentValidID.totalVoters)
return 0;
return 1;
}
int isVoted(char userID[15])
{
int location=extractRollNo(userID);
if(studentVotes[location-1]=='0')
return 0;
else
return 1;
}
int isBanned(char userID[15]){
int location=extractRollNo(userID);
if(studentVotes[location-1]=='$')
return 1;
else
return 0;
}
void saveVote(char userID[15],char voteInput)
{
char filename[20];
sprintf(filename,"candidate%d.txt",voteInput-48);
FILE *fp = fopen(filename,"r+");
int location=extractRollNo(userID);
studentVotes[location-1]=voteInput;
candidateArray[voteInput-49].votes++;
fseek(fp, 0, SEEK_SET);
fprintf(fp,"%d\n",candidateArray[voteInput-49].votes);
fseek(fp, 0, SEEK_END);
fprintf(fp,"\n%d",location);
fclose(fp);
}
void studentPanel()
{
char userID[15];
char voteInput;
while(1)
{
printf("\n\n To exit press 0");
printf("\n Enter user ID:");
scanf("%s",userID);
if(strcmp(userID, "0")==0)
return;
if(isValid(userID)!=1)
{
printf("\n Invalid User ID(Press Enter)");
getch();
continue;
}
if(isBanned(userID)!=0)
{
printf("\nThis User ID is currently banned...\nContact Admin for the reason...(Press Enter to continue)");
getch();
continue;
}
if(isVoted(userID)!=0)
{
printf("\n Your PRN entered is already voted\n Contact Admin for furthur query");
getch();
continue;
}
printf("\n\n Candidates for election:");
for (int i = 0; i < numberOfCandidates; i++)
{
printf("\n %d. %s",i+1,candidateArray[i].cname);
}
printf("\n\n Your Vote(Enter Number):");
voteInput=getch();
printf("*");
if(voteInput-48 < 1 || voteInput-48 > numberOfCandidates)
{
printf("\nInvalid Vote\nTry Again...");
getch();
continue;
}
saveVote(userID,voteInput);
printf("\n\nThanks for your precious vote(Press Enter)");
getch();
}
};