WIP.
This commit is contained in:
parent
860a237faa
commit
bc4a271d17
2
Makefile
2
Makefile
@ -1,2 +1,2 @@
|
||||
mathprac: main.cpp Problem.cpp
|
||||
mathprac: main.cpp Problem.cpp Problem.h
|
||||
clang++ -o mathprac -std=c++11 -g main.cpp Problem.cpp -lncurses
|
||||
|
33
Problem.cpp
33
Problem.cpp
@ -3,6 +3,7 @@
|
||||
#include <cmath>
|
||||
#include <locale.h>
|
||||
#include <syslog.h>
|
||||
#include <stdexcept>
|
||||
|
||||
bool Problem::seeded = false;
|
||||
|
||||
@ -19,6 +20,8 @@ Problem::Problem(PROBLEM_TYPE type, unsigned short max_digits, unsigned win_x, u
|
||||
seeded = true;
|
||||
}
|
||||
|
||||
input = "";
|
||||
|
||||
GenerateNumbers(type, (max_digits > 5 ? 5 : max_digits));
|
||||
|
||||
GenerateWindow(win_x, win_y);
|
||||
@ -37,9 +40,9 @@ void Problem::Draw(bool selected)
|
||||
|
||||
// Draw the window:
|
||||
if(selected)
|
||||
box(win, '?', '?');
|
||||
else
|
||||
box(win, 0, 0); // 0, 0 gives default characters for the vertical and horizontal lines
|
||||
// else
|
||||
// box(win, ' ', ' ');
|
||||
|
||||
mvwprintw(win, 2, 4, "%'6d", number_top);
|
||||
mvwprintw(win, 3, 4, "%'6d", number_bottom);
|
||||
@ -55,6 +58,7 @@ void Problem::Draw(bool selected)
|
||||
break;
|
||||
};
|
||||
mvwprintw(win, 4, 2, "========");
|
||||
mvwprintw(win, 5, 4, "%'6d", Input());
|
||||
mvwprintw(win, 5, 9, "");
|
||||
wrefresh(win);
|
||||
}
|
||||
@ -142,27 +146,28 @@ void Problem::GenerateWindow(unsigned win_x, unsigned win_y)
|
||||
|
||||
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;
|
||||
return atoi(input.c_str());
|
||||
// int rtn = 0;
|
||||
// try {
|
||||
// rtn = std::stoi(input.c_str());
|
||||
// } catch (std::invalid_argument &e) {
|
||||
// syslog(LOG_USER|LOG_DEBUG, "%s:%d:%s Invalid std::stoi: \"%s\".",
|
||||
// __FILE__,__LINE__,__FUNCTION__,e.what());
|
||||
// exit(-1); // INVALID INPUT!!!
|
||||
// }
|
||||
// return rtn;
|
||||
}
|
||||
|
||||
void Problem::Input(char character)
|
||||
void Problem::Input(const INPUT_STYLE input_style, 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;
|
||||
input = character + input;
|
||||
else
|
||||
input += std::to_string(character);
|
||||
input += character;
|
||||
}
|
||||
|
||||
bool Problem::Correct() const
|
||||
|
@ -30,7 +30,7 @@ public:
|
||||
~Problem();
|
||||
|
||||
void Draw(bool selected);
|
||||
void Input(char character);
|
||||
void Input(const INPUT_STYLE input_style, char character);
|
||||
bool Correct() const;
|
||||
|
||||
private:
|
||||
@ -44,7 +44,6 @@ private:
|
||||
int number_top;
|
||||
int number_bottom;
|
||||
|
||||
INPUT_STYLE input_style = INPUT_STYLE::INPUT_PREPEND;
|
||||
int Input() const;
|
||||
std::string input;
|
||||
|
||||
|
125
main.cpp
125
main.cpp
@ -12,22 +12,8 @@
|
||||
|
||||
#define PROMPT " > "
|
||||
|
||||
char MainMenu()
|
||||
{
|
||||
clear();
|
||||
printw("\n");
|
||||
printw(" Welcome to Math Practice.\n");
|
||||
printw(" Please choose from the following menu:\n");
|
||||
printw(" 1. Addition\n");
|
||||
printw(" 2. Subtraction\n");
|
||||
printw(" 3. Both / Mixed\n");
|
||||
printw(" q. Quit\n");
|
||||
printw("\n");
|
||||
printw(PROMPT);
|
||||
refresh();
|
||||
|
||||
return getch();
|
||||
}
|
||||
std::vector<Problem*> GenerateProblems(bool addition, bool subtraction);
|
||||
char MainMenu();
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
@ -58,35 +44,47 @@ int main(int argc, char *argv[])
|
||||
clear();
|
||||
wrefresh(stdscr);
|
||||
|
||||
std::vector<Problem*> problems;
|
||||
for(auto y = 1; y < LINES - 10; y += 9)
|
||||
{
|
||||
for(auto x = 2; x < COLS - 16; x += 15)
|
||||
{
|
||||
Problem *test = nullptr;
|
||||
if(addition != subtraction)
|
||||
{
|
||||
if(addition)
|
||||
test = new Problem(EASY_ADDITION, 3, x, y);
|
||||
if(subtraction)
|
||||
test = new Problem(EASY_SUBTRACTION, 3, x, y);
|
||||
}
|
||||
if(addition && subtraction)
|
||||
{
|
||||
if(rand() % 2)
|
||||
test = new Problem(HARD_ADDITION, 5, x, y);
|
||||
else
|
||||
test = new Problem(HARD_SUBTRACTION, 5, x, y);
|
||||
}
|
||||
problems.push_back(test);
|
||||
}
|
||||
}
|
||||
std::vector<Problem*> problems = GenerateProblems(addition, subtraction);
|
||||
std::vector<Problem*>::iterator selected = problems.begin();
|
||||
|
||||
for(auto *problem : problems)
|
||||
if(problem)
|
||||
problem->Draw(false);
|
||||
problem->Draw(problem == *selected);
|
||||
|
||||
getch();
|
||||
INPUT_STYLE input_style = INPUT_STYLE::INPUT_PREPEND;
|
||||
while(selected != problems.end() && *selected)
|
||||
{
|
||||
Problem *current_problem = *selected;
|
||||
current_problem->Draw(true); // refresh with new input
|
||||
auto input = getch();
|
||||
if(input == 'q') // early exit
|
||||
break;
|
||||
switch(input)
|
||||
{
|
||||
case KEY_LEFT:
|
||||
input_style = INPUT_STYLE::INPUT_APPEND;
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
input_style = INPUT_STYLE::INPUT_PREPEND;
|
||||
break;
|
||||
case '0':
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
case '8':
|
||||
case '9':
|
||||
current_problem->Input(input_style, input);
|
||||
current_problem->Draw(true); // refresh with new input
|
||||
break;
|
||||
};
|
||||
|
||||
if(current_problem->Correct())
|
||||
++selected;
|
||||
}
|
||||
|
||||
for(auto *problem : problems)
|
||||
if(problem)
|
||||
@ -96,3 +94,48 @@ int main(int argc, char *argv[])
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
||||
char MainMenu()
|
||||
{
|
||||
clear();
|
||||
printw("\n");
|
||||
printw(" Welcome to Math Practice.\n");
|
||||
printw(" Please choose from the following menu:\n");
|
||||
printw(" 1. Addition\n");
|
||||
printw(" 2. Subtraction\n");
|
||||
printw(" 3. Both / Mixed\n");
|
||||
printw(" q. Quit\n");
|
||||
printw("\n");
|
||||
printw(PROMPT);
|
||||
refresh();
|
||||
|
||||
return getch();
|
||||
}
|
||||
|
||||
std::vector<Problem*> GenerateProblems(bool addition, bool subtraction)
|
||||
{
|
||||
std::vector<Problem*> rtn;
|
||||
for(auto y = 1; y < LINES - 10; y += 9)
|
||||
{
|
||||
for(auto x = 2; x < COLS - 16; x += 15)
|
||||
{
|
||||
Problem *test = nullptr;
|
||||
if(addition != subtraction)
|
||||
{
|
||||
if(addition)
|
||||
test = new Problem(EASY_ADDITION, 3, x, y);
|
||||
if(subtraction)
|
||||
test = new Problem(EASY_SUBTRACTION, 3, x, y);
|
||||
}
|
||||
if(addition && subtraction)
|
||||
{
|
||||
if(rand() % 2)
|
||||
test = new Problem(HARD_ADDITION, 5, x, y);
|
||||
else
|
||||
test = new Problem(HARD_SUBTRACTION, 5, x, y);
|
||||
}
|
||||
rtn.push_back(test);
|
||||
}
|
||||
}
|
||||
return rtn;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user