-
Notifications
You must be signed in to change notification settings - Fork 0
/
modquiz.cpp
167 lines (157 loc) · 3.92 KB
/
modquiz.cpp
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
#include "modquiz.h"
Quiz::Quiz(int level,int topic,int total,int score,int duration) {
this->level = level;
this->topic = topic;
this->totalQuestions = total;
this->score = score;
this->duration = duration;
this->remainder = 0;
}
void Quiz::setDuration(int dtime){
this->duration = dtime;
}
int Quiz::getDuration(void){
return this->duration;
}
void Quiz::setLevel(int level){
this->level = level;
}
int Quiz::getLevel(){
return this->level;
}
void Quiz::settopic(int topic){
this->topic = topic;
}
int Quiz::gettopic(void){
return this->topic;
}
String Quiz::getTopicName(int index){
String str = this->topics[index];
return str;
}
String Quiz::getTopicSymbol(void){
return this->topic_symbol[topic];
}
void Quiz::setTotalQuestions(int total){
this->totalQuestions = total;
}
int Quiz::getTotalQuestions(){
return this->totalQuestions;
}
void Quiz::genNumbers(){
this->fnumber = random(1,levels[this->level]);
this->lnumber = random(1,levels[this->level]);
}
void Quiz::setAnswer(int answer){
this->answer = answer;
}
int Quiz::getAnswer(){
return this->answer;
}
boolean Quiz::searchIndex(int number ,int* options,int arraysize){
boolean flag = false; //not found
int Array_size = arraysize;
for(int i = 0; i < Array_size; i++){
if(number == options[i]){
flag = true; //found
break;
}
}
return flag;
}
int* Quiz::genOptions(){
this->options_array[0]= 0;
this->options_array[1]= 0;
this->options_array[2]= this->answer+1;
this->options_array[3]= this->answer;
int arraysize = sizeof(this->options_array)/sizeof(this->options_array[0]);
boolean stillGen = true;
int no_options = 0;
while(stillGen){
int gen_number = random(1,(levels[this->level]));
boolean elementFound = this->searchIndex(gen_number,this->options_array,arraysize);
if(!elementFound){
this->options_array[no_options] = gen_number;
no_options = no_options + 1;
if(no_options > 1){
break;
}
}
}
return this->options_array;
}
int Quiz::findNumberFactors(int number){
// Serial<<"Natural number:"<<number<<endl;
int sizeofFactors = 0;
for(int i= 1; i < number; i++){
if(number % i == 0){
sizeofFactors = sizeofFactors + 1;
}
}
int factors[sizeofFactors];
memset(factors,0,sizeofFactors);
int index = 0;
for(int j= 1; j < number; j++){
if((number % j) == 0){
factors[index] = j;
//Serial<< factors[index]<<endl;
++index;
}
}
int lastfactor = index ;//- 1;
int factor = random(0,lastfactor);
return factors[factor];
}
int Quiz::getfnumber(){
return this->fnumber;
}
int Quiz::getlnumber(){
return this->lnumber;
}
void Quiz::setlnumber(int a){
this->lnumber = a;
}
int Quiz::addition(int a, int b){
int result = a + b;
return result;
}
int Quiz::subtraction(int a, int b){
int result = a - b;
return result;
}
int Quiz::multiplication(int a,int b){
int result = a * b;
return result;
}
int Quiz::division(int a, int b){
if(a > b && a > 0){
int result = a / b;
int rem = a % b;
this->remainder = rem;
return result;
}
return 0;
}
void Quiz::setScore(int score){
this->score = score;
}
int Quiz::getScore(void){
return this->score;
}
int Quiz::operationHandler(int topic, int a, int b){
int result = 0;
if(topic == 0 ){
result = this->addition(a , b);
}
if(topic == 1){
result = this->subtraction(a,b);
}
if(topic == 2){
result = this->multiplication(a,b);
}
if(topic == 3){
result = this->division(a, b);
}
this->answer = result;
return result;
}