📄 mathquestface.cpp
字号:
#include <iostream>
#include <iomanip>
using namespace std;
#include "mathquestface.h"
#include "randgen.h"
#include "strutils.h"
MathQuestion::MathQuestion()
: myAnswer("*** error ***"),
myNum1(0),
myNum2(0)
{
// nothing to initialize
}
void MathQuestion::Create()
{
RandGen gen;
// generate random numbers until there is no carry
do
{
myNum1 = gen.RandInt(10,49);
myNum2 = gen.RandInt(10,49);
} while ( (myNum1 % 10) + (myNum2 % 10) >= 10);
myAnswer = tostring(myNum1 + myNum2);
}
void MathQuestion::Ask() const
{
const int WIDTH = 7;
cout << setw(WIDTH) << myNum1 << endl;
cout << "+" << setw(WIDTH-1) << myNum2 << endl;
cout << "-------" << endl;
cout << setw(WIDTH-myAnswer.length()) << " ";
}
bool MathQuestion::IsCorrect(const string& answer) const
{
return myAnswer == answer;
}
string MathQuestion::Answer() const
{
return myAnswer;
}
string MathQuestion::Description() const
{
return "addition of two-digit numbers with NO carry";
}
CarryMathQuestion::CarryMathQuestion()
: MathQuestion()
{
// all done in base class constructor
}
void CarryMathQuestion::Create()
{
RandGen gen;
// generate random numbers until there IS a carry
do
{
myNum1 = gen.RandInt(10,49);
myNum2 = gen.RandInt(10,49);
} while ( (myNum1 % 10) + (myNum2 % 10) < 10);
myAnswer = tostring(myNum1 + myNum2);
}
string CarryMathQuestion::Description() const
{
return "addition of two-digit numbers with a carry";
}
HardMathQuestion::HardMathQuestion()
: MathQuestion()
{
// all done in base class constructor
}
void HardMathQuestion::Create()
{
RandGen gen;
myNum1 = gen.RandInt(100,200);
myNum2 = gen.RandInt(100,200);
myAnswer = tostring(myNum1 + myNum2);
}
string HardMathQuestion::Description() const
{
return "addition of three-digit numbers";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -