-
Notifications
You must be signed in to change notification settings - Fork 1
/
rootsmodule.cpp
293 lines (250 loc) · 7.53 KB
/
rootsmodule.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
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
/* Copyright (c) 2013 Mike Dusseault
*
* This file is part of QMentat.
*
* QMentat is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QMentat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QMentat. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rootsmodule.h"
#include "mainwindow.h"
#include <iostream>
#include <QtGui>
#include <string>
#include <QString>
#include "rootsconfigframe.h"
#include "mainwindow.h"
#include "practicemodule.h"
//#include "questiondisplayform.h"
#ifdef USE_MATH_DISPLAY
# include "mathdisplayform.h"
#else
# include "questiondisplayform.h"
#endif
#include "random.h"
#include "bigfixedpoint.h"
RootsModule::RootsModule(MainWindow *mw)
{
// Keep a copy for callbacks
mainWindow = mw;
// Init sane defaults
root = 0;
genRoot = 0;
// Read config
QSettings settings;
settings.beginGroup("rootsmodule");
roundingMode = settings.value("roundingmode", false).toBool();
BigFixedPoint::setRounding(roundingMode == 1);
min = BigFixedPoint(settings.value("min", 2).toString());
max = BigFixedPoint(settings.value("max", 100).toString());
rootMin = settings.value("rootmin", 2).toInt();
rootMax = settings.value("rootmax", 2).toInt();
decimalPlaces = settings.value("decimalplaces", 0).toInt();
integersOnly = settings.value("integersonly", true).toBool();
settings.endGroup();
// Make config frame
configFrame = new RootsConfigFrame();
configFrame->setModule(this);
configFrame->setMinimum(min.toString());
configFrame->setMaximum(max.toString());
configFrame->setRootMinimum(rootMin);
configFrame->setRootMaximum(rootMax);
configFrame->setDecimalPlaces(decimalPlaces);
configFrame->setIntegersOnly(integersOnly);
configFrame->setRoundingMode((roundingMode == true) ? 1 : 0);
// Make display frame
#ifdef USE_MATH_DISPLAY
displayFrame = (QuestionDisplay*)(new MathDisplayForm());
#else
displayFrame = (QuestionDisplay*)(new QuestionDisplayForm());
#endif
rootRangeUpdated();
}
RootsModule::~RootsModule()
{
assert(configFrame != 0);
assert(displayFrame != 0);
delete genRoot;
genRoot = 0;
this->mainWindow->layout()->removeWidget(configFrame);
configFrame->close();
delete configFrame;
configFrame = 0;
delete displayFrame;
displayFrame = 0;
}
QFrame* RootsModule::getConfigFrame()
{
return configFrame;
}
QuestionDisplay* RootsModule::getDisplayFrame()
{
return displayFrame;
}
bool RootsModule::isRangeOk(const BigFixedPoint& newMin, const BigFixedPoint& newMax,
int newRootMin, int newRootMax)
{
bool isOk = false;
for (int i = newRootMin; i <= newRootMax; ++i)
{
BigFixedPoint maxNum = newMax.root(i);
BigFixedPoint minNum = BigFixedPoint::max(newMin.root(i), newMin);
if ((maxNum - minNum) >= 0)
{
isOk = true;
break;
}
}
return isOk;
}
QString RootsModule::question()
{
assert(genRoot != 0);
if (integersOnly) {
BigFixedPoint maxNum;
BigFixedPoint minNum;
// Iterate until we get a root that leads to a usable range.
do {
root = (*genRoot)();
maxNum = max.root(root);
minNum = BigFixedPoint::max(min.root(root), min);
} while ((maxNum - minNum) < 0);
BigFixedPoint desiredResult = BigFixedPoint::random(minNum, maxNum);
firstNumber = desiredResult.pow(root);
answer = desiredResult;
answer.scale(0);
} else {
root = (*genRoot)();
firstNumber = BigFixedPoint::random(min, max);
BigFixedPoint firstNumberScaled = firstNumber;
// Add one for rounding purposes
firstNumberScaled.scale(((decimalPlaces + firstNumber.getDecimalPlaces() + 1) * root));
answer = firstNumberScaled.root(root);
answer.scale(decimalPlaces);
}
#ifdef USE_MATH_DISPLAY
QString q = QString("<math><mroot><mi>%1</mi><mn>%2</mn></mroot></math>\n")
.arg(firstNumber.toString())
.arg(root);
#else
QString q = QString("%1\n| %2")
.arg(firstNumber.toString())
.arg(root);
#endif
return q;
}
bool RootsModule::isCorrect(const QString& answerGiven)
{
BigFixedPoint answerNum(answerGiven);
answerNum.scale(answer.getDecimalPlaces());
if (answerNum == answer)
{
return true;
} else {
return false;
}
}
QString RootsModule::getAnswerString()
{
return QString("%1^(1/%2) = %3")
.arg(firstNumber.toString())
.arg(root)
.arg(answer.toString());
}
/*! Range of root updated, so make a new PRG for it.
*/
void RootsModule::rootRangeUpdated()
{
assert(rootMin > 0);
assert(rootMax > 0);
// Get rid of previous generator
delete genRoot;
genRoot = 0;
genRoot = new RandomInt<int>(rootMin, rootMax);
}
void RootsModule::setSettings(const BigFixedPoint& newFirstMin,
const BigFixedPoint& newFirstMax,
int newRootMin,
int newRootMax,
bool newIntsOnly,
int newDecimals,
bool newRoundingMode)
{
bool settingsChanged = false;
// RANGE
if ((max != newFirstMax)
|| (max.getDecimalPlaces() != newFirstMax.getDecimalPlaces()))
{
max = newFirstMax;
QSettings settings;
settings.setValue("rootsmodule/max", max.toString());
settingsChanged = true;
}
if ((min != newFirstMin)
|| (min.getDecimalPlaces() != newFirstMin.getDecimalPlaces()))
{
min = newFirstMin;
QSettings settings;
settings.setValue("rootsmodule/min", min.toString());
settingsChanged = true;
}
if (rootMax != newRootMax)
{
rootMax = newRootMax;
QSettings settings;
settings.setValue("rootsmodule/rootmax", rootMax);
settingsChanged = true;
}
if (rootMin != newRootMin)
{
rootMin = newRootMin;
QSettings settings;
settings.setValue("rootsmodule/rootmin", rootMin);
settingsChanged = true;
}
// RESULTS
if (decimalPlaces != newDecimals)
{
decimalPlaces = newDecimals;
QSettings settings;
settings.setValue("rootsmodule/decimalplaces", decimalPlaces);
settingsChanged = true;
}
if (roundingMode != newRoundingMode)
{
roundingMode = newRoundingMode;
BigFixedPoint::setRounding(roundingMode);
QSettings settings;
settings.setValue("rootsmodule/roundingmode", roundingMode);
settingsChanged = true;
}
if (integersOnly != newIntsOnly)
{
integersOnly = newIntsOnly;
QSettings settings;
settings.setValue("rootsmodule/integersonly", integersOnly);
settingsChanged = true;
}
if (settingsChanged)
{
rootRangeUpdated();
mainWindow->newQuestion();
}
}
bool RootsModule::applyConfig()
{
if (configFrame != 0)
{
return configFrame->applyConfig();
}
return false;
}