-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeycode.c
More file actions
322 lines (305 loc) · 9.31 KB
/
keycode.c
File metadata and controls
322 lines (305 loc) · 9.31 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct userdb
{
int score;
int no_of_ques;
char uname[15];
}getuser={0,0,"NULL"},f_user;
void practice(FILE *); //done
void showstats(FILE *); //done
void leaderboard(void); // done
int match(int); //done
int scoreupdate(FILE *); //done
void showques(void); //done
void showans(void); //done
void welcome(void); //done
FILE* login(void); //done
FILE *getpos(void); //done
char username[15];
int qnum=0;
int main()
{
int choice;
char flag='y';
FILE *pos;
system("rm sol.c;rm sol;rm output.txt;touch userdata.dat");
system("clear");
welcome();
pos=login();
showstats(pos);
do{
printf("\n\n 1.) Practice\n "
"2.) Show Leaderboard\n "
"3.) Logout\n "
"0.) Exit\n\n "
"Your Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: practice(pos);
break;
case 2: leaderboard();
break;
case 3: main();
break;
case 4: return 0;
default: printf(" Incorrect Input!!!\n");
}
/* printf("\n Want to continue?(y/n): ");
scanf("%c",&flag);*/
}while(1);
return 0;
}
void practice(FILE *pos)
{
char ch='y';
int choice,val,comp=0,exec=0;
printf("\n File ptr in practice beg: %ld\n",ftell(pos));
showques();
printf("\n !!!---Press enter to open nano text editor---!!!\n");
getchar();
getchar();
system("nano sol.c");
do{
printf("%s","\n 1.) View question again\n "
"2.) Open text editor again\n "
"3.) Compile\n "
"4.) Run with custom input\n "
"5.) Submit your code\n "
"6.) Show answer\n "
"0.) Go Back\n\n "
"Your Choice: ");
scanf("%d",&choice);
pos=getpos();
printf("\n File ptr in practice do while: %ld\n",ftell(pos));
switch(choice)
{
case 1: showques();
break;
case 2: system("nano sol.c");
break;
case 3: val=system("gcc sol.c -o sol");
if(!val)
{
printf("\n Compilation Successful.\n");
comp=1;
}
else
{
printf("\n GCC returned error %d.\n Compilation unsuccessful.\n",val);
comp=0;
}
break;
case 4:
if(!comp)
{
printf("\n You need to successfully compile your program first!!!");
exec=0;
}
else
{
printf("\n Provide your Input here(if any).\n\n");
val=system("./sol");
if(val)
{
printf("\n Execution unsuccessful.\n Error %d.\n",val);
exec=0;
}
else
{
printf("\n Executed successfull!!!");
exec=1;
}
}
break;
case 5: if(exec==1 || comp==1)
{
system("./sol > output.txt");
val=match(qnum);
if(!val)
{
printf("\n Answer matched!!!");
if(!scoreupdate(pos))
printf("\n Score Updated!");
qnum++;
}
}
else
{
printf("\n You should at least try to compile your program first!!!");
}
break;
case 6: showans();
break;
case 0: return ;
default: printf("\n Please provide a valid input.");
}
}while(1);
}
FILE* login(void)
{
int flag=0,i=0;
FILE *fptr;
char readname[15];
/* printf(" Enter username: ");
getchar();
scanf("%[^\n]s",readname); */
printf("\n\n Enter Username: ");
scanf("%s",readname);
// printf("\n read string : %s",readname);
strcpy(getuser.uname,readname);
// printf("\ncopied to getuser.uname");
fptr=fopen("userdata.dat","r+b");
while(!feof(fptr))
{
fread(&f_user,sizeof(f_user),1,fptr);
if(!strcmp(f_user.uname,getuser.uname))
{
printf("\n Welcome Back %s\n",getuser.uname);
flag=1;
break;
}
}
if(!flag)
{
// fwrite(&getuser,sizeof(getuser),1,fptr);
fwrite(&getuser,sizeof(getuser),1,fptr);
printf("\n New Profile created!!\n");
}
fseek(fptr,-sizeof(getuser),SEEK_CUR);
return fptr;
}
void leaderboard(void)
{
FILE *readptr;
char exp_level[13];
readptr=fopen("userdata.dat","rb");
printf(" Name\t\tScore\tExp Level\tQuestions\n");
while(fread(&f_user,sizeof(f_user),1,readptr))
{
switch(f_user.no_of_ques)
{
case 0: strcpy(exp_level,"Unranked"); break;
case 1: strcpy(exp_level,"Beginner"); break;
case 2: strcpy(exp_level,"Intermediate"); break;
case 3: strcpy(exp_level,"Advanced"); break;
case 4: strcpy(exp_level,"Ace"); break;
default: strcpy(exp_level,"Legendary"); break;
}
printf("\n %s\t\t%d\t%s\t\t%d",f_user.uname,f_user.score,exp_level,f_user.no_of_ques);
}
fclose(readptr);
}
int scoreupdate(FILE *pos)
{
FILE *fptr;
fptr=fopen("userdata.dat","r+b");
fptr=pos;
printf("\n File ptr in scoreupd: %ld\n",ftell(fptr));
// fseek(fptr,-sizeof(f_user),SEEK_CUR);
fread(&f_user,sizeof(f_user),1,fptr);
f_user.score+=10;
f_user.no_of_ques+=1;
fseek(fptr,-sizeof(f_user),SEEK_CUR);
fwrite(&f_user,sizeof(f_user),1,fptr);
fclose(fptr);
return 0;
}
FILE *getpos()
{
FILE *fptr;
fptr=fopen("userdata.dat","r+b");
while(!feof(fptr))
{
fread(&f_user,sizeof(f_user),1,fptr);
if(!strcmp(f_user.uname,getuser.uname))
{
break;
}
}
fseek(fptr,-sizeof(getuser),SEEK_CUR);
return fptr;
}
int match(int solnum)
{
int ret;
switch(solnum)
{
case 0: ret=system("diff -Z output.txt s0.txt");
break;
case 1: ret=system("diff -Z output.txt s1.txt");
break;
case 2: ret=system("diff -Z output.txt s2.txt");
break;
case 3: ret=system("diff -Z output.txt s3.txt");
break;
case 4: ret=system("diff -Z output.txt s4.txt");
break;
default: printf("\n Limit Exceeded!!\n");
}
return ret;
}
void showques()
{
switch(qnum)
{
case 0: system("cat q0.txt");
break;
case 1: system("cat q1.txt");
break;
case 2: system("cat q2.txt");
break;
case 3: system("cat q3.txt");
break;
case 4: system("cat q4.txt");
break;
default: printf("\n Sorry we do not have anymore questions for you!!\n");
}
}
void showans(void)
{
switch(qnum)
{
case 0: system("firefox https://www.programiz.com/c-programming/examples/print-sentence");
break;
case 1: system("firefox https://www.programiz.com/c-programming/examples/print-integer");
break;
case 2: system("firefox https://www.programiz.com/c-programming/examples/factorial");
break;
case 3: system("cat q3.txt");
break;
case 4: system("cat q4.txt");
break;
default: printf("\n Sorry we do not have anymore questions for you!!\n");
}
}
void showstats(FILE *pos)
{
char exp_level[13];
FILE *fptr;
fptr=fopen("userdata.dat","rb");
fptr=pos;
fread(&f_user,sizeof(f_user),1,fptr);
switch(f_user.no_of_ques)
{
case 0: strcpy(exp_level,"Unranked"); break;
case 1: strcpy(exp_level,"Beginner"); break;
case 2: strcpy(exp_level,"Intermediate"); break;
case 3: strcpy(exp_level,"Advanced"); break;
case 4: strcpy(exp_level,"Ace"); break;
default: strcpy(exp_level,"Legendary"); break;
}
qnum=f_user.no_of_ques;
printf("\n Username: %s\n Questions solved: %d\n Score: %d\n Programmer Level: %s\n\n",f_user.uname,f_user.no_of_ques,f_user.score,exp_level);
fclose(fptr);
}
void welcome(void)
{
printf("\t## ## ###### ## ## ##### ##### ##### ######\n"
"\t## ## ## ## ## ## ## # ## # ## \n"
"\t### #### ## ## ## # ## # #### \n"
"\t## ## ## ## ## ## # ## # ## \n"
"\t## ## ###### ## ##### ##### ##### ######\n");
}