-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdivisionmodule.cpp
More file actions
568 lines (498 loc) · 16.9 KB
/
divisionmodule.cpp
File metadata and controls
568 lines (498 loc) · 16.9 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
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
/* 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 "divisionmodule.h"
#include "mainwindow.h"
#include <cmath>
#include <QtCore/qmath.h>
#include <iostream>
#include <vector>
#include <QtGui>
#include <string>
#include <QString>
#include "divisionconfigframe.h"
#include "mainwindow.h"
#include "practicemodule.h"
#ifdef USE_MATH_DISPLAY
# include "mathdisplayform.h"
#else
# include "questiondisplayform.h"
#endif
#include "random.h"
#include "bigfixedpoint.h"
/*! \class DivisionModule
* \brief Handles logic of questions and answers for division practice.
* \author Mike Dusseault <mike.dusseault@gmail.com>
*/
DivisionModule::DivisionModule(MainWindow *mw)
{
// Keep a copy for callbacks
mainWindow = mw;
genFirst = 0;
// Read config
QSettings settings;
settings.beginGroup("divisionmodule");
roundingMode = settings.value("roundingmode", false).toBool();
BigFixedPoint::setRounding(roundingMode == DivisionConfigFrame::ROUNDING_ROUND);
integersOnly = settings.value("integersonly", false).toBool();
if (integersOnly)
{
QString str = settings.value("firstmin", 2).toString();
firstMinIR = str.remove(QLocale::system().groupSeparator()).toLongLong();
str = settings.value("firstmax", 100).toString();
firstMaxIR = str.remove(QLocale::system().groupSeparator()).toLongLong();
str = settings.value("lastmin", 2).toString();
lastMinIR = str.remove(QLocale::system().groupSeparator()).toLongLong();
str = settings.value("lastmax", 100).toString();
lastMaxIR = str.remove(QLocale::system().groupSeparator()).toLongLong();
} else {
firstMin = BigFixedPoint(settings.value("firstmin", 2).toString());
firstMax = BigFixedPoint(settings.value("firstmax", 100).toString());
lastMin = BigFixedPoint(settings.value("lastmin", 2).toString());
lastMax = BigFixedPoint(settings.value("lastmax", 100).toString());
}
largestNumberFirst = settings.value("largestNumberFirst", false).toBool();
decimalPlaces = settings.value("decimalplaces", 0).toInt();
settings.endGroup();
// Make config frame
configFrame = new DivisionConfigFrame();
configFrame->setModule(this);
if (integersOnly)
{
configFrame->setFirstMinimum(QString("%L1").arg(firstMinIR));
configFrame->setFirstMaximum(QString("%L1").arg(firstMaxIR));
configFrame->setLastMinimum(QString("%L1").arg(lastMinIR));
configFrame->setLastMaximum(QString("%L1").arg(lastMaxIR));
} else {
configFrame->setFirstMinimum(firstMin.toString());
configFrame->setFirstMaximum(firstMax.toString());
configFrame->setLastMinimum(lastMin.toString());
configFrame->setLastMaximum(lastMax.toString());
}
configFrame->setLargestNumberFirst(largestNumberFirst);
configFrame->setDecimalPlaces(decimalPlaces);
configFrame->setRoundingMode((roundingMode == true)
? DivisionConfigFrame::ROUNDING_ROUND
: DivisionConfigFrame::ROUNDING_TRUNCATE);
configFrame->setIntegersOnly(integersOnly);
// Make display frame
#ifdef USE_MATH_DISPLAY
displayFrame = (QuestionDisplay*)(new MathDisplayForm());
#else
displayFrame = (QuestionDisplay*)(new QuestionDisplayForm());
#endif
if (integersOnly)
{
firstRangeUpdated();
}
}
DivisionModule::~DivisionModule()
{
assert(configFrame != 0);
assert(displayFrame != 0);
delete genFirst;
genFirst = 0;
this->mainWindow->layout()->removeWidget(configFrame);
configFrame->close();
delete configFrame;
configFrame = 0;
delete displayFrame;
displayFrame = 0;
}
/*! Return config frame.
*/
QFrame* DivisionModule::getConfigFrame()
{
return configFrame;
}
/*! Return display frame.
*/
QuestionDisplay* DivisionModule::getDisplayFrame()
{
return displayFrame;
}
/*! Tests to see if any divisors divide any of the possible dividends.
* \param firstMin Minimum of the dividend.
* \param firstMax Maximum of the dividend.
* \param lastMin Minimum of the divisor.
* \param lastMax Maximum of the divisor.
* \returns true if at least one of the possible divisors divides at
* least one of the possible dividends, false otherwise.
*/
bool DivisionModule::isRangeOk(qint64 firstMin, qint64 firstMax,
qint64 lastMin, qint64 lastMax) const
{
bool isOk = false;
// We ignore firstMax because we don't care if it's only divisible by itself
for (qint64 i = firstMax-1; i >= firstMin; --i)
{
qint64 last = std::min(static_cast<qint64>(qSqrt(i)), lastMax) + 1;
for (qint64 j = lastMin; j < last; ++j)
{
if (((i % j) == 0) && (i != j))
{
isOk = true;
break;
}
}
if (isOk)
break;
}
return isOk;
}
/*! Finds divisors in a certain range for a number.
* \param num Number for which we need divisors.
* \param min Minimum divisor to consider.
* \param max Maximum divisor to consider.
* \returns Vector of divisors between min and max of num.
*/
std::vector<qint64> *DivisionModule::getDivisors(qint64 num,
qint64 min,
qint64 max) const
{
std::vector<qint64> *divisors = new std::vector<qint64>();
// No use going past sqrt(num)
qint64 last = std::min(static_cast<qint64>(qSqrt(num)), max) + 1;
const int partitions = 100000000;
QProgressDialog progress("Finding divisors, please wait...", "Cancel",
0, partitions, configFrame);
progress.setWindowModality(Qt::WindowModal);
int prog = 0;
for (qint64 i = min; i <= last; ++i)
{
int newProg = (int)qRound(((double)(i - min) / (double)last) * (double)partitions);
if (newProg > prog)
{
prog = newProg;
}
progress.setValue(prog);
if (progress.wasCanceled())
{
/*
if (divisors->empty())
{
delete divisors;
divisors = 0;
return 0;
}
*/
break;
}
if ((num % i) == 0)
{
divisors->push_back(i);
if ((num / i) > last)
{
divisors->push_back(num/i);
}
}
}
progress.setValue(partitions);
return divisors;
}
/*! \returns QString containing new question in MathML format.
*/
QString DivisionModule::question()
{
if (integersOnly)
{
assert(genFirst != 0);
QApplication::setOverrideCursor(Qt::WaitCursor);
// Ensure we don't get a division by 0
do {
// Generate the numbers
firstNumberIR = (*genFirst)();
std::vector<qint64> *divisors = getDivisors(firstNumberIR,
lastMinIR, lastMaxIR);
assert(divisors != 0);
/*
std::cout << "Divisors for " << firstNumberIR << ": ";
for (auto it = divisors->begin(); it != divisors->end(); ++it)
{
std::cout << (*it) << " ";
}
std::cout << std::endl;
*/
if (divisors->empty()) {
// No divisors, try again
lastNumberIR = 0;
delete divisors;
continue;
} else {
int random = qrand() % divisors->size();
lastNumberIR = (qint64)(*divisors)[random];
delete divisors;
}
answerIR = static_cast<quint64>(firstNumberIR)
/ static_cast<quint64>(lastNumberIR);
} while (lastNumberIR == 0);
QApplication::restoreOverrideCursor();
#ifdef USE_MATH_DISPLAY
QString q = QString("<math><mfrac><mi>%L1</mi><mn>%L2</mn></mfrac></math>\n")
.arg(firstNumberIR)
.arg(lastNumberIR);
#else
QString q = QString("%L1\n/%L2")
.arg(firstNumberIR)
.arg(lastNumberIR);
#endif
return q;
} else {
firstNumber = BigFixedPoint::random(firstMin, firstMax);
lastNumber = BigFixedPoint::random(lastMin, lastMax);
// Swap largest number first, if necessary and set to do so
if (largestNumberFirst && (lastNumber > firstNumber))
{
BigFixedPoint tmp = firstNumber;
firstNumber = lastNumber;
lastNumber = tmp;
}
// Calculate answer
BigFixedPoint firstNumberScaled = firstNumber;
firstNumberScaled.scale(firstNumber.getDecimalPlaces()+lastNumber.getDecimalPlaces()+decimalPlaces+1);
answer = firstNumberScaled / lastNumber;
assert(decimalPlaces >= 0);
answer.scale(decimalPlaces);
int decimals = 0;
decimals = std::max(firstNumber.getDecimalPlaces(), lastNumber.getDecimalPlaces());
BigFixedPoint firstDisplay(firstNumber);
firstDisplay.scale(decimals);
BigFixedPoint lastDisplay(lastNumber);
lastDisplay.scale(decimals);
// Build question string
#ifdef USE_MATH_DISPLAY
QString q = QString("<math><mfrac><mi>%1</mi><mn>%2</mn></mfrac></math>\n")
.arg(firstDisplay.toString())
.arg(lastDisplay.toString());
#else
QString q = QString("%1\n/ %2")
.arg(firstDisplay.toString())
.arg(lastDisplay.toString());
#endif
return q;
}
}
/*! Tells if the supplied answer is correct.
* \returns true if correct, false otherwise.
*/
bool DivisionModule::isCorrect(const QString& answerGiven)
{
if (integersOnly)
{
qint64 answerNum = answerGiven.toLongLong();
//qDebug() << "isCorrect(qint64): answerGiven = " << answerNum << "; answerIR = " << answerIR;
if (answerNum == answerIR)
{
return true;
} else {
return false;
}
} else {
BigFixedPoint answerNum(answerGiven);
answerNum.scale(answer.getDecimalPlaces());
//qDebug() << "isCorrect(decimals): answerGiven = " << answerNum.toString() << "; answer = " << answer.toString();
if (answerNum == answer)
{
return true;
} else {
return false;
}
}
}
/*! Produces answer string for current question.
* \returns QString containing full answer for display.
*/
QString DivisionModule::getAnswerString()
{
if (integersOnly)
{
return QString("%L1 / %L2 = %L3")
.arg(firstNumberIR)
.arg(lastNumberIR)
.arg(answerIR);
} else {
return QString("%1 / %2 = %3")
.arg(firstNumber.toString())
.arg(lastNumber.toString())
.arg(answer.toString());
}
}
void DivisionModule::setSettings(const BigFixedPoint& newFirstMin,
const BigFixedPoint& newFirstMax,
const BigFixedPoint& newLastMin,
const BigFixedPoint& newLastMax,
bool newLargestNumberFirst, bool newIntsOnly,
int newDecimals, bool newRoundingMode)
{
bool settingsChanged = false;
// RANGE
if ((firstMax != newFirstMax)
|| (firstMax.getDecimalPlaces() != newFirstMax.getDecimalPlaces()))
{
firstMax = newFirstMax;
QSettings settings;
settings.setValue("divisionmodule/firstmax", firstMax.toString());
settingsChanged = true;
}
if ((firstMin != newFirstMin)
|| (firstMin.getDecimalPlaces() != newFirstMin.getDecimalPlaces()))
{
firstMin = newFirstMin;
QSettings settings;
settings.setValue("divisionmodule/firstmin", firstMin.toString());
settingsChanged = true;
}
if ((lastMax != newLastMax)
|| (lastMax.getDecimalPlaces() != newLastMax.getDecimalPlaces()))
{
lastMax = newLastMax;
QSettings settings;
settings.setValue("divisionmodule/lastmax", lastMax.toString());
settingsChanged = true;
}
if ((lastMin != newLastMin)
|| (lastMin.getDecimalPlaces() != newLastMin.getDecimalPlaces()))
{
lastMin = newLastMin;
QSettings settings;
settings.setValue("divisionmodule/lastmin", lastMin.toString());
settingsChanged = true;
}
// RESULTS
if (this->largestNumberFirst != newLargestNumberFirst)
{
this->largestNumberFirst = newLargestNumberFirst;
QSettings settings;
settings.setValue("divisionmodule/largestNumberFirst", largestNumberFirst);
settingsChanged = true;
}
if (decimalPlaces != newDecimals)
{
decimalPlaces = newDecimals;
QSettings settings;
settings.setValue("divisionmodule/decimalplaces", decimalPlaces);
settingsChanged = true;
}
if (roundingMode != newRoundingMode)
{
roundingMode = newRoundingMode;
BigFixedPoint::setRounding(roundingMode);
QSettings settings;
settings.setValue("divisionmodule/roundingmode", roundingMode);
settingsChanged = true;
}
if (integersOnly != newIntsOnly)
{
integersOnly = newIntsOnly;
QSettings settings;
settings.setValue("divisionmodule/integersonly", integersOnly);
settingsChanged = true;
}
if (settingsChanged)
{
mainWindow->newQuestion();
}
}
// For integer results only mode
void DivisionModule::setSettings(qint64 newFirstMin, qint64 newFirstMax,
qint64 newLastMin, qint64 newLastMax,
bool newlargestNumberFirst, bool newIntsOnly,
int newDecimals, bool newRoundingMode)
{
bool settingsChanged = false;
if (firstMaxIR != newFirstMax)
{
firstMaxIR = newFirstMax;
QSettings settings;
settings.setValue("divisionmodule/firstmax", QString("%1").arg(firstMaxIR));
settingsChanged = true;
}
if (firstMinIR != newFirstMin)
{
firstMinIR = newFirstMin;
QSettings settings;
settings.setValue("divisionmodule/firstmin", QString("%1").arg(firstMinIR));
settingsChanged = true;
}
if (lastMaxIR != newLastMax)
{
lastMaxIR = newLastMax;
QSettings settings;
settings.setValue("divisionmodule/lastmax", QString("%1").arg(lastMaxIR));
settingsChanged = true;
}
if (lastMinIR != newLastMin)
{
lastMinIR = newLastMin;
QSettings settings;
settings.setValue("divisionmodule/lastmin", QString("%1").arg(lastMinIR));
settingsChanged = true;
}
// RESULTS
if (this->largestNumberFirst != newlargestNumberFirst)
{
this->largestNumberFirst = newlargestNumberFirst;
QSettings settings;
settings.setValue("divisionmodule/largestNumberFirst", largestNumberFirst);
settingsChanged = true;
}
if (decimalPlaces != newDecimals)
{
decimalPlaces = newDecimals;
QSettings settings;
settings.setValue("divisionmodule/decimalplaces", decimalPlaces);
settingsChanged = true;
}
if (roundingMode != newRoundingMode)
{
roundingMode = newRoundingMode;
BigFixedPoint::setRounding(roundingMode);
QSettings settings;
settings.setValue("divisionmodule/roundingmode", roundingMode);
settingsChanged = true;
}
if (integersOnly != newIntsOnly)
{
integersOnly = newIntsOnly;
QSettings settings;
settings.setValue("divisionmodule/integersonly", integersOnly);
settingsChanged = true;
}
if (settingsChanged)
{
firstRangeUpdated();
mainWindow->newQuestion();
}
}
void DivisionModule::firstRangeUpdated()
{
// Get rid of previous generator
delete genFirst;
genFirst = 0;
// Make new generator
qint64 minGen = std::min(firstMinIR, firstMaxIR);
qint64 maxGen = std::max(firstMaxIR, firstMinIR);
genFirst = new RandomInt<qint64>(minGen, maxGen);
}
bool DivisionModule::applyConfig()
{
if (configFrame != 0)
{
return configFrame->applyConfig();
}
return false;
}