-
Notifications
You must be signed in to change notification settings - Fork 1
/
QuizBuddyMain.c
909 lines (841 loc) · 33.7 KB
/
QuizBuddyMain.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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
/*
@brief QuizBuddy 1.1
Programmed By
@author Sushant Gautam (544,2071)
( http://sushant.info.np
With Sishir Bhandari
@date 2016
For TU IOE BCT First Semester Project
(c) With Developers
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#include <time.h>
///Setting minimum value to 1; used in random number generation range.
#define minV 1
///Setting number of maximum words to 512; this is used for URLs.
#define MAX_LINE 512
///Integer array to store 100 unique random numbers in a range generated by RandomNumber function.
int RandArray[100];
///@brief Function to return a unique random number not present in array 'RandArray[100]' in a range value passed :
int RandomNumber(int, int);
///@brief Function to Start Quiz :
int QuizStart(int);
///@brief Function to scan data from question database with correct answer as return value :
int datascanner(int);
///@brief This function calls the About section of the program :
void about(void);
///@brief Calls the WIN API to download the file. It uses global string variables 'url' and 'destination'. So these values should be set first before function call :
int download(void);
///@brief This function shows all quiz results from database :
void printQuizData(void);
///@brief This function edits personal information record of user:
char UserRecord(void);
///@brief This funcction displays personal information record of user:
void viewprofile(void);
///@brief This function takes the user to preferences section to change the program preferences :
void settings(void);
///@brief This function allows user to select different quiz database. This function is followed by download() function that downloads the selected database from internet :
int QuizSelect(void);
///@brief This function prepares new files and database for first time users :
int firstrun();
///@brief This function finds the number of lines in a database for passed filename as string value :
int findnOfQsnInDatabase(char[]);
///@brief Display HomeScreen with diffrent options for user to choose :
int HomeScreen();
///@brief Let user review their answers after finishing quiz:
int QuizReview();
///@brief Prints Progress Graph for user performance:
int PrintProgressGraph();
///@brief Print Graph for user performance according to answering speed:
void PrintSpeedGraph();
///@brief Calculates the data for result analysis:
void CalculateResult();
///@brief Finds the number of times the user has taken quiz:
int NumberOfQuizTaken();
///@brief Find the name of database in use:
void findDataBasename(void);
/// @brief Reset Question number variable to zero:
int QsnNumbr=0;
/// @brief Reset marks variable to zero.
int Marks=0;
///@brief Download Url variable used by download() function.
char url[MAX_LINE];
///@brief Download destination variable used by download() function.
char destination[MAX_LINE];
///@brief String variable to store temporary data during file reading .
char str[512];
///@brief Structure to hold user informations. This structure is saved to record file and data is retrieved on next program start.
struct UserRecSt {
char name[20];
int age;
char address[20];
char collegename[20];
char preparingfor[20];
char mobilenum[20];
int nOfQsns;
int timeForeachQuestion;
char QuizType[30];
int lastQuizPerc;
int numberofQuizTaken; }
/// Structure variable for UserRecSt. This structure is saved to record file and data is retrieved on next program start.
data;
///@brief Structure to hold user performance informations.
struct UserResultAnalysis{
int averageprcnt;
int highestPrcntg;
float lowestTimeperQsn;
}
/// Structure variable for UserResultAnalysis. This structure holds user performance informations
analysis;
///@brief Structure to hold values of question and answers along with correct answer while using QuizStart();
struct resultData {
char qsn[200];
char ans1[100];
char ans2[100];
char ans3[100];
char ans4[100];
char corans;
char userans;
}
///Structure variable for resultData to hold values of question and answers along with correct answer while using QuizStart();
result[100];
///@brief Structure to hold the values of data retrieved from result database.
struct ResultFromdataStr {
int nOfQsns;
int Marks;
float times;
char date[20];
char time[20];
///Structure variable for ResultFromdataStr to hold the values of data retrieved from result database.
}
///Structure variable for ResultFromdataStr to hold the values of data retrieved from result database.
RsltFrmdtaStr[99999];
int main()
{
///Create QBcache folder and hide it. NO
system("@attrib +r +a +s +h QBcache /s /d");
system("@mkdir QBcache");
///If false Display Loading screen.
system("@title QuizBuddy- loading. . ."); system("@color cf"); system("@cls");
printf("\n\n\n\t\t\tQuizBuddy v 1.1\n\n\n\t\t Developed By : \n \n\t Sushant Gautam - [email protected] \n \n\t\t Sishir Bhandari \n \n\t\t TU IOE BCT 2016 (1st Sem. Project) \n \n\t\t (c) with Developers. \n \n\t\t www.sushant.info.np \n");
printf("\n\n\n\t\t\tLoading..."); sleep(1);
printf(".....");sleep(1);
printf("...");sleep(1);
sleep(1);
system("@cls");
///Check if record file for user information exists or not.
FILE *userrec = fopen("QBcache/tdrsu.QBcache", "r");
if (userrec == NULL)
{ ///If file doesn't exist, call UserRecord() to edit user profile.
data.nOfQsns=10;
UserRecord();
}
///If file exists, read the data to structure.
fread(&data, sizeof(data),1,userrec);
fclose(userrec);
///Call to HomeScreen() Function.
HomeScreen();
}
int HomeScreen()
{
///Hide QbCache folder.
system("@attrib +r +a +s +h QBcache /s /d");
system("@title QuizBuddy"); system("@color fc"); system("@cls");
printf("\n\t\t\tWelcome to QuizBuddy \n\t\t\t __________________\n");
printf("\n\t\t %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", 0Xc9, 0Xcd, 0Xcd,0Xcd, 0Xcd,0Xcd,0Xcd,0Xcd,0Xcd,0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0xbb);
///Find the name of database in use using findDataBasename().
findDataBasename();
///Set the time for each question to 30 second.
data.timeForeachQuestion=30;
///Display main menu along with user data.
printf("\n \t\t %c Start Quiz \t Press 1 %c\n\n \t\t %c Preferences \t Press 2 %c\n\n \t\t %c Profile / Results \t Press 3 %c\n\n \t\t %c About \t\t Press 4 %c\n\n \t\t %c Exit \t\t Press 5 %c\n",0Xba,0Xba ,0Xba ,0Xba ,0Xba,0Xba,0Xba,0Xba,0Xba,0Xba );
printf("\n\t\t %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c", 0xc8, 0Xcd, 0Xcd,0Xcd,0Xcd,0Xcd,0Xcd,0Xcd, 0Xcd,0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0Xcd, 0xbc);
printf("\n\n \t User: %s ", data.name);
printf("\t\t Preparing for : %s ", data.preparingfor);
printf("\n\n \t\t\t Quiz Selected : %s\n", data.QuizType);
///Set the number of quiz taken to the return value of function NumberOfQuizTaken().
data.numberofQuizTaken=NumberOfQuizTaken();
if(data.numberofQuizTaken>=99999)
{
system("cls");
printf("\n\n\tYou are exceeded the number of Quizzes ");
printf("\n\t\tthat QuizBuddy is programmed to handle.");
printf("\n\n\tPress Y To RESET your result database or any key to exit.\n");
char tmp=getch();
if ((tmp=='y')||(tmp=='Y'))
{
remove("QBcache/bdtsr.QBcache");
printf("\n\n\tResult Cleared!!\n");
data.lastQuizPerc=0;
sleep(2);
main();
}
sleep(2);
printf("\n\n\tBye Bye.. . .. \n");
exit(0);
}
///Call the CalculateResult() to calculate user data from database.
CalculateResult();
printf("\t %d Quiz(zes) Taken ", data.numberofQuizTaken);
if((analysis.averageprcnt>0)&&(analysis.averageprcnt<=100)) {
printf("\t\t Average Percentage: %d", analysis.averageprcnt);
}
/// Get User Input in HomeScreen
char WlcmScrnKy=getch();
///Check for User Input using Switch.
switch(WlcmScrnKy)
{case '1': /// If 1, reset Question number and marks & Call the QuizStart() Function
{fflush(stdin); system("@cls"); QsnNumbr=0 , Marks=0; QuizStart(data.nOfQsns); break; }
case '2' : /// If 2, call the settings() function.
settings();
FILE *userrec = fopen("QBcache/tdrsu.QBcache", "wb");
fwrite(&data, sizeof(data),1,userrec);
fclose(userrec);
printf("\n\n\t%d Questions selected",data.nOfQsns );
sleep(1); HomeScreen();
/// If 3, call the viewprofile().
case '3' : viewprofile();
/// If 4, print about section via about().
case '4' : about() ;
///If 5, exit QuizBuddy with goodbye message.
case '5' :
{
system("@cls");
system("@title QuizBuddy -- Exit");
printf("\n\n\t\tBye Bye. .. . .\n");
printf("\t\tSee You Back Soon :) . .. . .\n");
sleep(2);
exit(0);
}
///By Default, Call HomeScreen() function for any other keys pressed.
default : system("@cls"); HomeScreen(); ;
}
}
int QuizStart(int nOfQsns)
{
/// Find no of question in database by counting number of lines; calling nOfQsnInDatabase() function.
int nOfQsnInDatabase= findnOfQsnInDatabase("QBcache/btd.QBcache")-2;
///Start Timer
clock_t qTimerStart = clock();
int i; int timeup;
for (i=0; i<(nOfQsns-1); i++)
{result[i].userans='0';
result[i].corans=0;
}
QsnNumbr=0; char get;
while((get!='q')&&(get!='Q'))
{
system("cls");
///Call the datascanner() with the random value from return of RandomNumber(), which lies in the range specified.
RandArray[QsnNumbr]=(RandomNumber(nOfQsnInDatabase,QsnNumbr)+2);
if (result[QsnNumbr].corans==0) result[QsnNumbr].corans=datascanner(RandArray[QsnNumbr]);
///Print the Quiz Questions and answers.
printf("\n\t\t QuizBuddy -- Quiz Running \n");
printf("\n\t Question number: %d\t Number of Questions: %d\tTime: %.2fs\n\t\t___________\n", QsnNumbr+1, nOfQsns, (double)(clock() - qTimerStart) / CLOCKS_PER_SEC );
if (((double)(clock() - qTimerStart) / CLOCKS_PER_SEC )>= data.timeForeachQuestion*data.nOfQsns) { timeup=1; break;}
printf("\n\t %s\n", result[QsnNumbr].qsn);
printf("\n\t Option 1: %s\n", result[QsnNumbr].ans1);
printf("\n\t Option 2: %s\n", result[QsnNumbr].ans2);
printf("\n\t Option 3: %s\n", result[QsnNumbr].ans3);
printf("\n\t Option 4: %s\n", result[QsnNumbr].ans4);
printf("\n\t Your Answer: %c\n", result[QsnNumbr].userans);
printf("\n\t\t___________\n\n\t PRESS 1,2,3,4 to CHANGE answer.\n\t PRESS a and d to NAVIGATE and PRESS O to SUBMIT answers.");
///Ask user inpur for correct answer.
char get=getch();
///Continuously Check for system time change comparing the take at beginning and at present.
if (((double)(clock() - qTimerStart) / CLOCKS_PER_SEC )<0)
{
system("cls");
printf("\n\tFraud Detected!\n\t________________\n\n\tSystem Time Changed !!\n\tWe don't entertain that. :p \n\n\tBe fair next time %s .\n\tExiting QuizBuddy. . .. ", data.name);
sleep(3);
///If system time change detected, exit quiz showing fraud message.
exit(0);
}
if ((get=='d')||(get=='D')) QsnNumbr++;
if ((get=='a')||(get=='A')) QsnNumbr--;
if ((get=='q')||(get=='Q')) break;
if ((get=='0') ||(get=='o')||(get=='O')) break;
///Change the answer storing variable in result structure as per user answer.
if (get=='1') {result[QsnNumbr].userans='1'; QsnNumbr++;}
if (get=='2') {result[QsnNumbr].userans='2'; QsnNumbr++;}
if (get=='3') {result[QsnNumbr].userans='3'; QsnNumbr++;}
if (get=='4') {result[QsnNumbr].userans='4'; QsnNumbr++;}
if (QsnNumbr>nOfQsns-1) QsnNumbr--;
if (QsnNumbr<0) QsnNumbr++ ;
system("cls");
}
system("cls");
printf("\n\t\tQuiz Completed\n\t\t___________\n");
printf("\n\t\tSubmitting Answer. . \n");
sleep(2);
/// Stop Timer
clock_t qTimerEnd = clock();
if (timeup==1) { system("cls"); printf("\n\t\tYou ran out of time. Be quick on next quiz. %s.\n", data.name);printf("\n\t\tGood Luck!!\n", data.name); sleep(4); qTimerEnd= qTimerStart+data.timeForeachQuestion*data.nOfQsns ; }
for (i=0; i<(nOfQsns-1); i++)
/// Check for correct answers and write update marks.
{if (result[i].corans==result[i].userans) Marks++;
}
/// After finishing Asking Questions - Write Output to Data File
FILE *ResultData;
ResultData = fopen("QBcache/bdtsr.QBcache", "a");
if(ResultData == NULL) { ResultData= fopen("QBcache/bdtsr.QBcache", "wb"); } /// If file does not exist, create it
///Display the result overview to user.
system("@cls");
printf("\n\n \t RESULT\n\t__________________________\n\t__________________________\n\n");
printf("\tYou made %i out of %i questions correctly\n", Marks,nOfQsns) ;
printf("\tYou Took %.0f seconds to complete.\n", (double)(qTimerEnd - qTimerStart) / CLOCKS_PER_SEC);
printf("\tYour Accuracy Percentage: %d \n", (Marks*100/nOfQsns));
printf("\tYou answered one question in around %.0f second(s) on average. \n", (double)(qTimerEnd - qTimerStart) / (CLOCKS_PER_SEC*nOfQsns));
if ((Marks*100/nOfQsns)>analysis.highestPrcntg) printf("\n\tHIGHEST RECORD!!\n");
else if ((Marks*100/nOfQsns)>analysis.averageprcnt) printf("\n\tYou did better than your average records.\n");
printf("\n\t__________________________\n\t");
///Calculate and Write percentage to structure variable data.
data.lastQuizPerc=(Marks*100/nOfQsns);
/// Write result to Data File.
fprintf(ResultData,"%d\t%d\t%f\t%s\t%s\n",nOfQsns ,Marks,((double)(qTimerEnd - qTimerStart) / CLOCKS_PER_SEC),__TIME__, __DATE__);
fclose(ResultData);
printf("\n\t%s\t%s\n",__DATE__,__TIME__);
///Call the QuizReview() function to review the quiz after submission.
printf("\n\n\n\t Press any key for QuizReview . . . \n\t Press q to skip QuizReview.");
///Prompt for user character input.
char tmp = getch();
if ((tmp=='q')||(tmp=='Q')) {HomeScreen(); }
///Goto HomeScreen() if user selects the respective option.
//Call the QuizReview() function and then PrintProgressGraph();
data.numberofQuizTaken=NumberOfQuizTaken();
QuizReview();
PrintProgressGraph();
}
int QuizReview()
{
int i=0, nOfQsns= data.nOfQsns; char get;
while((get!='q')&&(get!='Q')&&(i>=0)&&(i<nOfQsns))
{
///Prints the Questions, Options, Correct answers and your inputs for respective question during the last quiz.
system("cls");
printf("\n\t\tQuiz Review\n\t\t___________\n");
printf("\n\t Question: %d of %d \t Percentage: %d\n", i+1,data.nOfQsns, data.lastQuizPerc);
printf("\n\t %s\n", result[i].qsn);
printf("\n\t Option 1: %s\n", result[i].ans1);
printf("\n\t Option 2: %s\n", result[i].ans2);
printf("\n\t Option 3: %s\n", result[i].ans3);
printf("\n\t Option 4: %s\n", result[i].ans4);
printf("\n\t Correct Answer: %c\t\t Your Answer: %c\n", result[i].corans, result[i].userans);
if (result[i].corans==result[i].userans) {printf("\n\tYou answered correctly.\n\t\t___________\n"); system("@color A9");}
else if (result[i].userans=='0') {printf("\n\tNo answer was chosen.\n\t\t___________\n"); system("@color F3");
}
else {printf("\n\tYou answred wrong.\n\t\t___________\n");
system("@color 0B");
}
printf("\n\tUse a and d to navigate and 'q' to quit review.");
///Prompt the user character input for navigating through questions or exit.
get=getch();
system("cls");
if ((get=='a')||(get=='A')) i--;
if ((get=='d')||(get=='D')) i++;
///Exit for respective user input.
if ((get=='q')||(get=='Q')) break;
if (i>nOfQsns-1) i--;
if (i<0) i++;
}
printf("\n\t\tQuiz Review\n\t\t___________\n");
printf("\n\t\tReview Completed. . \n");
return 0;
}
int datascanner(int line)
{
int end, loop;
char str[512];
FILE *database = fopen("QBcache/btd.QBcache", "r");
if (database == NULL)
{
///If file opening fails goto firstrun() function to reset files.
firstrun();
HomeScreen();
}
for(end = loop = 0;loop<line;++loop){
if(0==fgets(str, sizeof(str), database)){
end = 1;
break;
}
}
char CrectAnswer;
CrectAnswer=str[(strlen(str)-2)];
fclose(database);
int sort[100];
int i=0,j=0;
for (; i<1000; i++)
{
if (str[i] =='$')
{
sort[j]=i; j++;
}
}
///Sort questions and options from file.
char QuestionID[10], Question[1000], Op1[300], Op2[300], Op3[300], Op4[300];
strncpy(QuestionID, str + 0, sort[0] - 0); QuestionID[sort[0]]='\0';
strncpy(Question, str+ sort[0] , sort[1] - sort[0]); Question[sort[1] - sort[0]]='\0';
strncpy(Op1, str + sort[1], sort[2] - sort[1]); Op1[sort[2] - sort[1]]='\0';
strncpy(Op2, str + sort[2], sort[3] - sort[2]); Op2[sort[3] - sort[2]]='\0';
strncpy(Op3, str + sort[3], sort[4] - sort[3]); Op3[sort[4] - sort[3]]='\0';
strncpy(Op4, str + sort[4], sort[5] - sort[4]); Op4[sort[5] - sort[4]]='\0';
QuestionID[0]=' '; Question[0]=' '; Op1[0]=' '; Op2[0]=' '; Op3[0]=' '; Op4[0]=' ';
///Set string values for question and options in structure variable.
strcpy(result[QsnNumbr].qsn,Question);
strcpy(result[QsnNumbr].ans1,Op1);
strcpy(result[QsnNumbr].ans2,Op2);
strcpy(result[QsnNumbr].ans3,Op3);
strcpy(result[QsnNumbr].ans4,Op4);
result[QsnNumbr].corans=CrectAnswer;
///Return correct answer value.
if (CrectAnswer=='1') return '1';
if (CrectAnswer=='2') return '2';
if (CrectAnswer=='3') return '3';
if (CrectAnswer=='4') return '4';
}
void settings(void)
{ //Get and check User Input for displayed preferences.
system("@cls");
system("@color fd");
system("@title QuizBuddy -- Preferences ");
printf("\n\t\tNumber of Questions Selected : %d\n\t\tAverage Time for each question: %d seconds.", data.nOfQsns, data.timeForeachQuestion);
printf("\n\n\t\tPreferences\n\t\t______________\n");
printf("\n\t\tSelect Exam Type:\n\t\t1. 10 Questions\n\t\t2. 50 Questions\n\t\t3.100 Questions\n\n\t\t4. UpdateQuestionDatabase\n\t\t5. Quiz Selection\n\t\t6. Update QuizBuddy Program\n\t\t7. Reset Result Data");
char settingType=getch();
switch(settingType)
///If 1,2 or 3 Set no of question value acccording to option selected by user.
{case '1':
data.nOfQsns=10 ; break;
case '2' : data.nOfQsns=50; break;
case '3' : data.nOfQsns=100; break;
///If 4, goto firstrun() .
case '4' : firstrun(); break;
///If 5 goto QuizSelect() .
case '5' : QuizSelect(); break;
///If 6 set url and destination to update program and call the download() .
case '6' : {
strcpy(url,""); strcpy(destination,""); strcpy(url,"https://drive.google.com/uc?export=download&id=0B5iGbzCnc7P7WE1zNTVoUGV5SlE");
strcpy(destination,"QuizBuddyUpdated.exe");
if (download()==0) HomeScreen();
system("@ren QuizBuddy.exe QBold.exe");
system("@ren QuizBuddyUpdated.exe QuizBuddy.exe");
system("@move /y QBold.exe QBcache/");
system("@del /Q /F QBold.exe");
printf("\n\n\tQuizBuddy Updated\n\tPress any key . . .\n\t ");
getch();
printf("\n\n\tByeBye! This version of QuizBuddy will now exit.\n\tYou will meet the new and smarter QuizBuddy on next start.\n\t ");
sleep(5);
exit(0);
}
case '7' :
///If prompt for user confirmation and delete result data.
{
printf("\n\n\tYou are about to CLEAR all your result data.");
printf("\n\n\tPress Y To CONFIRM or any key to cancel.\n");
char tmp=getch();
if ((tmp=='y')||(tmp=='Y'))
{
remove("QBcache/bdtsr.QBcache");
printf("\n\n\tResult Cleared!!\n");
data.lastQuizPerc=0;
sleep(2);
main();
}
}
///By default, return to HomeScreen for other key pressed.
default : system("@cls"); HomeScreen();
}
}
int download()
{
char buffer[MAX_LINE];
HRESULT dl;
system("@color bd");
system("@title QuizBuddy -- Updating. . ");
printf("\n Updating. . . \n");
typedef HRESULT (WINAPI * URLDownloadToFileA_t)(LPUNKNOWN pCaller, LPCSTR szURL, LPCSTR szFileName, DWORD dwReserved, void * lpfnCB);
URLDownloadToFileA_t xURLDownloadToFileA;
xURLDownloadToFileA = (URLDownloadToFileA_t)GetProcAddress(LoadLibraryA("urlmon"), "URLDownloadToFileA");
///Start downloading files from internet.
dl = xURLDownloadToFileA(NULL, url, destination, 0, NULL);
sprintf( buffer, " Update in progress. . \n");
///If downloaded successfully, print success message and return 1.
if(dl == S_OK)
{
sprintf(buffer, " \n Successfully Updated !! ", destination);
printf(buffer);
sleep(2);
return 1;
}
else if(dl == E_OUTOFMEMORY)
{
sprintf(buffer, "\n Failed To Download due to Insufficient Memory");
printf(buffer);
sleep(2);
return 0;
}
else
///If not downloaded successfully, print unsuccess message, return 0.
{
sprintf( buffer, "\n Failed To Update!! \n Press any key to continue.. . .");
printf(buffer);
fflush(stdin);
getch();
return 0;
}
}
int firstrun(void)
{
system("@title QuizBuddy- First Run"); system("@color cf"); system("@cls");
printf("\n\n\n\t\t\tQuizBuddy\n\n");
sleep(1);
printf("\n\t\tNo Quiz database found.\n\n");
printf("\n\tIts time to update our database from internet.\n\n");
sleep(1);
printf("\n Make sure that you have an active internet connection \n\n");
printf("\n\t to update the QuizBuddy database. \n\n");
sleep(2);
strcpy(url,"https://drive.google.com/uc?export=download&id=0B5iGbzCnc7P7cm5ITzVvU1J6UHc" );
strcpy(destination,"QBcache/tslq.QBcache" );
/// Download the Quiz List from Internet using download().
if (download()==0)
{system("cls");
printf("\n\n Sorry! The action was unsuccessful.\n Update after internet connection or try obtaining standalone versions.\n Visit www.sushant.info.np for informations.\n ");
sleep(10);
exit(0);
}
printf("\n Done!! You are ready to continue.. . .. ");
sleep(2);
/// Call to QuizSelect() .
QuizSelect();
}
int QuizSelect(void)
{
system("@cls");
printf("\n Please Select your Quiz:\n\n");
int end, loop;
char str[600];
FILE *database = fopen("QBcache/tslq.QBcache", "r");
if (database == NULL)
{ ///If file Quiz List not found, call to firstrun() .
printf("\n database null");
firstrun();
HomeScreen();
}
int line;
///Print Quiz List data from file.
for (line=1;line<50;)
{
for(end = loop = 0;loop<line;++loop)
{
if(0==fgets(str, sizeof(str)+5, database)){
end = 1;
break;
}
}
printf("%s", str);
strcpy(str, "");
if (end==1) break;
line=2;
}
char usrQuizSelect[1];
fflush(stdin);
///Prompt for user input to select Quiz.
scanf("%s",&usrQuizSelect) ;
line=(atoi(usrQuizSelect)+2)*2;
rewind(database); strcpy(str,"");
for(end = loop = 0;loop<line;++loop){
if(0==fgets(str, sizeof(str), database)){
end = 1;
break;
}
}
///Set url and destination value as per user selection.
strcpy(url,""); strcpy(destination,""); strcpy(url,str);
strcpy(destination,"QBcache/btd.QBcache" );
fclose(database);
///Call to download() .
if (download()==0)
{system("cls");
printf("\n\n Unsuccesful! Quiz Database was not downloaded. \n Try to update after internet connection.\n ");
sleep(5);
}
else HomeScreen();
}
int RandomNumber(int max, int i)
{
clock();
int val=0;
int num;
///Loop until correct and unique value obtained.
while (val!=5)
{
///Get a number time for process started and perform modulo operation by maximum value on the time.
num = clock()%max;
///Check if the value is in corect range and exclude false data.
if ((!(num>=minV&&num<=max))&&(num==0)&&(num==1)) {
val=4;
}
else val=5;
int j;
///Check if the value has previously been in RandArray[] , exclude data if exists.
for (j=0;j<i;j++)
{
if (num==RandArray[j]) { val=4; break; }
else val=5;
}
}
///If value is unique store the value in RandArray[] and return the value.
RandArray[i]= num;
return num;
}
int findnOfQsnInDatabase(char dest[])
{
int end, loop, number;
char str[512];
FILE *database = fopen(dest, "r");
if (database == NULL)
{
///If file opening fails. Call firstrun() .
printf("\n No database found.");
firstrun();
HomeScreen();
fclose(database);
return 0;
}
while(1)
{
char tmp=fgetc(database);
///Count the number of new lines in the file.
if(tmp=='\n' ) number++;
if(tmp==EOF) break;
}
fclose(database);
///Return the number of new lines.
return number;
printf("hi");
}
void about()
{
///Display the about information.
system("@cls");
system("@color cf");
system("@title QuizBuddy -- About ");
printf("*\n**\n****\n******\n********\n**********\n****???**\n******?******\n****************\n******************\n********************\n**\t\t\t\t QuizBuddy v 1.1 (WIN32) ");
sleep(1);
printf("\n****\n******\n******** Developed By :\n**********\n************ Sushant Gautam - [email protected]\n**************\n**************** Sishir Bhandari\n********?********\n******???******** TU IOE BCT 2016 (1st Sem. Project)\n********?************\n************************\t (c) with Developers.\n**************************\n****************************\t www.sushant.info.np");
sleep(1);
printf("\n** \n**\n**\n** Made in Nepal :)\n**\n**\n");
getch();
system("@cls");
///Return to HomeScreen.
HomeScreen();
}
char UserRecord(void)
{
system("@title QuizBuddy -- Profile Edit ");
///Ask the user informations.
printf("\n\tPlease edit your profile:\n\t\t");
printf("\n\tInput your full name:\n\t\t");
fflush(stdin);
///Store it in data structure which is written to record file.
gets(data.name);
printf("\n\tInput your email address:\n\t\t");
gets(data.address);
printf("\n\tInput associated institution name:\n\t\t");
gets(data.collegename);
printf("\n\tInput the name of exam you are preparing for:\n\t\t");
gets(data.preparingfor);
printf("\n\tInput contact number:\n\t");
gets(data.mobilenum);
printf("\n\tInput your age in number:\n\t\t");
scanf("%d", &data.age);
fflush(stdin);
system("cls");
///Display the user information for verification.
printf("\n\tPROFILE REVIEW\n\t________________\n");
printf("\n\t Name: %s ",data.name );
printf("\n\t Age: %ld ",data.age );
printf("\n\t Email : %s ",data.address );
printf("\n\t Institution name: %s ",data.collegename );
printf("\n\t Exam preparing for: %s ",data.preparingfor );
printf("\n\t Contact Number: %s ",data.mobilenum );
FILE *userrec = fopen("QBcache/tdrsu.QBcache", "wb");
fwrite(&data, sizeof(data),1,userrec);
fclose(userrec);
printf("\n\n\t\tYour Profile has been updated.\n\t\tYou can always edit your profile..", data.name);
printf("\n\tReturning Back to Main Menu. . " );
///Return to main menu
sleep(5);
}
void viewprofile()
{system("@title QuizBuddy -- Profile review ");
system("cls");
///Display the user information for verification.
printf("\n\tPROFILE REVIEW\n\t________________\n");
printf("\n\n\t Name: %s ",data.name );
printf("\n\t Age: %ld ",data.age );
printf("\n\t Email : %s ",data.address );
printf("\n\t Institution name: %s ",data.collegename );
printf("\n\t Exam preparing for: %s ",data.preparingfor );
printf("\n\t Contact Number: %s \n\n\t________________\n",data.mobilenum );
printf("\n\n\tPress r key from keyboard to view your QUIZ RESULTS and GRAPHS");
printf("\n\n\tPress e key from keyboard to EDIT your PROFILE");
printf("\n\n\tPress l key from keyboard to LOG OUT");
char tmp= getch();
///Prompt user character input for result view or profile edit or clearing data.
system("cls");
///Perform the task as per user input : result display through printQuizData() or profile edit through UserRecord().
if ((tmp==('e'))||(tmp==('E'))) {UserRecord();}
if ((tmp==('r'))||(tmp==('R'))) {printQuizData();}
if ((tmp=='l')||(tmp=='L')) {
printf("\n\n\tYou are about to log out ");
printf("\n\n\t and CLEAR all your personal data including results.");
printf("\n\n\tPress Y To CONFIRM or any key to cancel.\n");
char tmp=getch();
if ((tmp=='y')||(tmp=='Y'))
{
remove("QBcache/tdrsu.QBcache");
remove("QBcache/bdtsr.QBcache");
sleep(2);
main();
}
}
system("cls");
///Return to HomeScreen().
HomeScreen();
}
void findDataBasename(void){
///Open database file.
FILE *odatabase = fopen("QBcache/btd.QBcache", "r");
int end, loop, line=1;
for(end = loop = 0;loop<line;++loop){
if(0==fgets(str, sizeof(str), odatabase))
{
end = 1;
break;
}
}
fclose(odatabase);
///Copy the database name from first line in the file.
strcpy(data.QuizType, "");
strcpy(data.QuizType, str);
FILE *userrec = fopen("QBcache/tdrsu.QBcache", "wb");
fwrite(&data, sizeof(data),1,userrec);
fclose(userrec);
}
int NumberOfQuizTaken()
{ ///Open result database.
FILE* ResultData = fopen("QBcache/bdtsr.QBcache", "r");
if (ResultData == NULL)
{ ///If file Quiz result database not found, call to QuizStart() function to start a new quiz .
system("cls");
printf("\n\n \n \t Hi %s! You have not taken a Quiz yet.\n\t Please try one.\n\t Loding the first quiz for you.........\n\n", data.name);
sleep(5);
data.lastQuizPerc=0;
fclose(ResultData);
QuizStart(data.nOfQsns);
}
int i=0;
///Count the number of lines in the database.
///Write the different data in the database to structure that holds record from result database.
while ( fscanf (ResultData, "%d\t%d\t%f\t%s\t%[^\n]s\n", &RsltFrmdtaStr[i].nOfQsns, &RsltFrmdtaStr[i].Marks, &RsltFrmdtaStr[i].times, &RsltFrmdtaStr[i].time, &RsltFrmdtaStr[i].date) != EOF )
{
i++;
}
fclose(ResultData);
///Return count value.
data.numberofQuizTaken=i;
return i;
}
void printQuizData()
{
int i;
int lines=15;
float factor= data.numberofQuizTaken/lines;
if ((float)factor<=1) factor=1;
if (lines>data.numberofQuizTaken) lines=data.numberofQuizTaken;
printf("\n\t\t\t QUIZ Results\n\t\t\t_________________\n\n");
for(i=0; i<lines ;i++)
{
int num=(int)(factor*i);
///Print the data using the structure that holds record from result database updated by NumberOfQuizTaken() function.
printf ("\t%d\t%d\t%.2f\t%s\t%s\t%d\n", RsltFrmdtaStr[num].nOfQsns, RsltFrmdtaStr[num].Marks, RsltFrmdtaStr[num].times, RsltFrmdtaStr[num].time, RsltFrmdtaStr[num].date, (RsltFrmdtaStr[num].Marks*100/RsltFrmdtaStr[num].nOfQsns)) ;
}
printf("\n\tTotal Obtained Seconds \t\tTime and Date \t Percentage \n");
printf("\n\t\t\t_________________\n\tYour Average Percentage: %d\t Highest Percentage: %d\n", analysis.averageprcnt, analysis.highestPrcntg );
printf("\tPress any key for Progress Graph.. . "); getch();
///Call the PrintProgressGraph() to print graph of user progress.
PrintProgressGraph();
}
void CalculateResult()
{
int i, sum=0;
///Calculate various results using structure that holds record from result database updated by NumberOfQuizTaken() function.
for(i=0; i<data.numberofQuizTaken;i++)
{ sum+=(RsltFrmdtaStr[i].Marks*100/RsltFrmdtaStr[i].nOfQsns);
if ((RsltFrmdtaStr[i].Marks*100/RsltFrmdtaStr[i].nOfQsns)> analysis.highestPrcntg) analysis.highestPrcntg=(RsltFrmdtaStr[i].Marks*100/RsltFrmdtaStr[i].nOfQsns);
}
analysis.averageprcnt= sum/i;
///Update the value to structure.
}
int PrintProgressGraph()
{
system("cls");
system("color bc");
system("title QuizBuddy-- Progress Graph");
int i,j;
int linesInGraph=15;
float factor= data.numberofQuizTaken/linesInGraph;
if ((float)factor<=1) factor=1;
if (linesInGraph>data.numberofQuizTaken) linesInGraph=data.numberofQuizTaken;
printf("\n\t\t\t PROGRESS GRAPH\n\t\t\t_________________\n\n");
///If number of quiz taken is less than two display message to take more quizzes.
///Print graphical characters for each result data in structure that holds record from result database updated by NumberOfQuizTaken() function according to its value.
for(i=0; i<linesInGraph ;i++)
{
int num=(int)(factor*i);
if (i==linesInGraph) num=data.numberofQuizTaken;
printf("%s %s ",RsltFrmdtaStr[num].time, RsltFrmdtaStr[num].date);
{ for (j=0; j<((RsltFrmdtaStr[num].Marks*100/RsltFrmdtaStr[num].nOfQsns))/2; j++)
printf("%c",0xDB); printf("\n"); j++;
}
}
printf(" Date and Time\t 0\t\t20\t\t50\t\t80\t100\n");
printf("\n The Higher Value indicate higher accuracy.\n Higher the value, better is the result. \n");
printf("\n\t Press any Key to continue. . .");
getch();
///Call the PrintSpeedGraph() to display graph for speed.
PrintSpeedGraph();
}
void PrintSpeedGraph()
{ system("cls");
system("color bc");
system("title QuizBuddy-- Speed Graph");
int i,j;
int linesInGraph=15;
float factor= data.numberofQuizTaken/linesInGraph;
if ((float)factor<=1) factor=1;
if (linesInGraph>data.numberofQuizTaken) linesInGraph=data.numberofQuizTaken;
printf("\n\t\t\t SPEED GRAPH\n\t\t\t_________________\n\n");
///Print graphical characters for each result data in structure that holds record from result database updated by NumberOfQuizTaken() function according to its value.
for(i=0; i<linesInGraph;i++)
{
int num=(int)(factor*i);
printf("%s%s",RsltFrmdtaStr[num].time, RsltFrmdtaStr[num].date);
{ for (j=0; j<(data.timeForeachQuestion-RsltFrmdtaStr[num].times/RsltFrmdtaStr[i].nOfQsns); j++)
if ((RsltFrmdtaStr[num].times/RsltFrmdtaStr[num].nOfQsns)<=data.timeForeachQuestion) printf("%c%c",0xDB,0xDB); printf("\n"); j++;
}
}
printf(" Date and Time\t 30 25 20 15 10 5 0\n");
printf(" The Lower Value indicate higher time spent on average.\n Higher the value, better is the result. \n");
printf("\n\t Press any Key to continue. . .");
///Prompt for user input for exiting the function.
getch();
HomeScreen();
}