50 lines
1.0 KiB
C
50 lines
1.0 KiB
C
#include <M5StickCPlus2.h>
|
|
|
|
// -1 if error
|
|
int a = 0;
|
|
int b = 0;
|
|
int targetResult = 0;
|
|
|
|
enum class Operator{
|
|
plus,
|
|
minus,
|
|
mult
|
|
};
|
|
|
|
void initRandomSeed(){
|
|
randomSeed(millis());
|
|
}
|
|
|
|
String generateQuestion(){
|
|
Operator oper = static_cast<Operator>(random(0, 3));
|
|
switch (oper)
|
|
{
|
|
case Operator::plus:
|
|
a = random(0, 15);
|
|
b = random(1, 15);
|
|
|
|
targetResult = a + b;
|
|
return String(a) + "+" + String(b) + " = ?";
|
|
|
|
case Operator::minus:
|
|
do
|
|
{
|
|
a = random(15, 30);
|
|
b = random(0, 15);
|
|
|
|
targetResult = a - b;
|
|
} while (targetResult <= 0);
|
|
return String(a) + "-" + String(b) + "= ?";
|
|
|
|
case Operator::mult:
|
|
a = random(0, 10);
|
|
b = random(1, 10);
|
|
|
|
targetResult = a * b;
|
|
return String(a) + "*" + String(b) + "= ?";
|
|
|
|
default:
|
|
Serial.println("Failed to create math question");
|
|
return "Error";
|
|
}
|
|
} |