Easier to understand number generation for hard problems. Initial support for input and checking correctness.

This commit is contained in:
David Vereb 2019-04-01 17:14:30 -05:00
parent 5ab9e9c5f3
commit 860a237faa
2 changed files with 66 additions and 21 deletions

View File

@ -2,6 +2,7 @@
#include <cmath>
#include <locale.h>
#include <syslog.h>
bool Problem::seeded = false;
@ -83,17 +84,6 @@ void Problem::GenerateNumbers(PROBLEM_TYPE type, unsigned short max_digits)
number_bottom += temp_number_bottom * (pow(10, digit));
}
break;
case HARD_ADDITION:
for(auto digit = 0; digit < digits; ++digit)
{
unsigned short temp_number_top = rand() % 10;
unsigned short temp_number_bottom = rand() % 10;
// Place into correct digit posititon:
number_top += temp_number_top * (pow(10, digit));
number_bottom += temp_number_bottom * (pow(10, digit));
}
break;
case EASY_SUBTRACTION:
for(auto digit = 0; digit < digits; ++digit)
{
@ -110,16 +100,13 @@ void Problem::GenerateNumbers(PROBLEM_TYPE type, unsigned short max_digits)
number_bottom += temp_number_bottom * (pow(10, digit));
}
break;
case HARD_ADDITION:
number_top = rand() % int(pow(10, digits));
number_bottom = rand() % int(pow(10, digits));
break;
case HARD_SUBTRACTION:
for(auto digit = 0; digit < digits; ++digit)
{
unsigned short temp_number_top = rand() % 10;
unsigned short temp_number_bottom = rand() % 10;
// Place into correct digit posititon:
number_top += temp_number_top * (pow(10, digit));
number_bottom += temp_number_bottom * (pow(10, digit));
}
number_top = rand() % int(pow(10, digits));
number_bottom = rand() % int(pow(10, digits));
// NOTE(dev): don't allow negative numbers:
if(number_bottom > number_top)
{
@ -152,3 +139,48 @@ void Problem::GenerateWindow(unsigned win_x, unsigned win_y)
if(!win)
return;
}
int Problem::Input() const
{
int rtn = 0;
try {
rtn = std::stoi(input.c_str());
} catch (const std::exception &e) {
syslog(LOG_USER|LOG_DEBUG, "%s:%d:%s Invalid std::stoi: %s",
__FILE__,__LINE__,__PRETTY_FUNCTION__,e.what());
exit(-1); // INVALID INPUT!!!
}
return rtn;
}
void Problem::Input(char character)
{
// TODO(dev): ASSERT
if(character < '0' || character > '9')
exit(-1);
if(input_style == INPUT_STYLE::INPUT_PREPEND)
input = std::to_string(character) + input;
else
input += std::to_string(character);
}
bool Problem::Correct() const
{
// NOTE(dev): I know I don't need "break;", but it just makes me happier knowing they're there.
switch(problem_type)
{
case EASY_ADDITION:
case HARD_ADDITION:
return number_top + number_bottom == Input();
break;
case EASY_SUBTRACTION:
case HARD_SUBTRACTION:
return number_top - number_bottom == Input();
break;
};
// TODO(dev): Assert that all conditions are handled!
exit(-1);
return false;
}

View File

@ -1,13 +1,14 @@
#ifndef DVEREB_PROBLEM_H
#define DVEREB_PROBLEM_H
#include <string>
#include <cstdlib>
#include <ncurses.h>
enum PROBLEM_TYPE
{
// NOTE(dev): EASY means each digit can be handled without carryover.
// e.g. 33 + 46 is easy because 6+3 < 10 and 3+4 is < 10.
// e.g. 33 + 46 is easy because 3+6 < 10 and 3+4 is < 10.
// HARD means you might have to carry over.
// e.g. 33 + 97 is hard because 7 + 3 = 10, carry the 1, 9 + 3 + 1 = 13, 130.
EASY_ADDITION = 1,
@ -16,6 +17,12 @@ enum PROBLEM_TYPE
HARD_SUBTRACTION,
};
enum INPUT_STYLE
{
INPUT_PREPEND = 1,
INPUT_APPEND,
};
class Problem
{
public:
@ -23,6 +30,8 @@ public:
~Problem();
void Draw(bool selected);
void Input(char character);
bool Correct() const;
private:
Problem() = delete;
@ -35,6 +44,10 @@ private:
int number_top;
int number_bottom;
INPUT_STYLE input_style = INPUT_STYLE::INPUT_PREPEND;
int Input() const;
std::string input;
// Helper Functions:
void GenerateNumbers(PROBLEM_TYPE type, unsigned short max_digits);
void GenerateWindow(unsigned win_x, unsigned win_y);