This commit is contained in:
David Vereb 2019-04-01 17:47:16 -05:00
parent 860a237faa
commit bc4a271d17
4 changed files with 105 additions and 58 deletions

View File

@ -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 clang++ -o mathprac -std=c++11 -g main.cpp Problem.cpp -lncurses

View File

@ -3,6 +3,7 @@
#include <cmath> #include <cmath>
#include <locale.h> #include <locale.h>
#include <syslog.h> #include <syslog.h>
#include <stdexcept>
bool Problem::seeded = false; bool Problem::seeded = false;
@ -19,6 +20,8 @@ Problem::Problem(PROBLEM_TYPE type, unsigned short max_digits, unsigned win_x, u
seeded = true; seeded = true;
} }
input = "";
GenerateNumbers(type, (max_digits > 5 ? 5 : max_digits)); GenerateNumbers(type, (max_digits > 5 ? 5 : max_digits));
GenerateWindow(win_x, win_y); GenerateWindow(win_x, win_y);
@ -37,9 +40,9 @@ void Problem::Draw(bool selected)
// Draw the window: // Draw the window:
if(selected) if(selected)
box(win, '?', '?');
else
box(win, 0, 0); // 0, 0 gives default characters for the vertical and horizontal lines 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, 2, 4, "%'6d", number_top);
mvwprintw(win, 3, 4, "%'6d", number_bottom); mvwprintw(win, 3, 4, "%'6d", number_bottom);
@ -55,6 +58,7 @@ void Problem::Draw(bool selected)
break; break;
}; };
mvwprintw(win, 4, 2, "========"); mvwprintw(win, 4, 2, "========");
mvwprintw(win, 5, 4, "%'6d", Input());
mvwprintw(win, 5, 9, ""); mvwprintw(win, 5, 9, "");
wrefresh(win); wrefresh(win);
} }
@ -142,27 +146,28 @@ void Problem::GenerateWindow(unsigned win_x, unsigned win_y)
int Problem::Input() const int Problem::Input() const
{ {
int rtn = 0; return atoi(input.c_str());
try { // int rtn = 0;
rtn = std::stoi(input.c_str()); // try {
} catch (const std::exception &e) { // rtn = std::stoi(input.c_str());
syslog(LOG_USER|LOG_DEBUG, "%s:%d:%s Invalid std::stoi: %s", // } catch (std::invalid_argument &e) {
__FILE__,__LINE__,__PRETTY_FUNCTION__,e.what()); // syslog(LOG_USER|LOG_DEBUG, "%s:%d:%s Invalid std::stoi: \"%s\".",
exit(-1); // INVALID INPUT!!! // __FILE__,__LINE__,__FUNCTION__,e.what());
} // exit(-1); // INVALID INPUT!!!
return rtn; // }
// return rtn;
} }
void Problem::Input(char character) void Problem::Input(const INPUT_STYLE input_style, char character)
{ {
// TODO(dev): ASSERT // TODO(dev): ASSERT
if(character < '0' || character > '9') if(character < '0' || character > '9')
exit(-1); exit(-1);
if(input_style == INPUT_STYLE::INPUT_PREPEND) if(input_style == INPUT_STYLE::INPUT_PREPEND)
input = std::to_string(character) + input; input = character + input;
else else
input += std::to_string(character); input += character;
} }
bool Problem::Correct() const bool Problem::Correct() const

View File

@ -30,7 +30,7 @@ public:
~Problem(); ~Problem();
void Draw(bool selected); void Draw(bool selected);
void Input(char character); void Input(const INPUT_STYLE input_style, char character);
bool Correct() const; bool Correct() const;
private: private:
@ -44,7 +44,6 @@ private:
int number_top; int number_top;
int number_bottom; int number_bottom;
INPUT_STYLE input_style = INPUT_STYLE::INPUT_PREPEND;
int Input() const; int Input() const;
std::string input; std::string input;

107
main.cpp
View File

@ -12,22 +12,8 @@
#define PROMPT " > " #define PROMPT " > "
char MainMenu() std::vector<Problem*> GenerateProblems(bool addition, bool subtraction);
{ 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();
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -58,7 +44,77 @@ int main(int argc, char *argv[])
clear(); clear();
wrefresh(stdscr); wrefresh(stdscr);
std::vector<Problem*> problems; std::vector<Problem*> problems = GenerateProblems(addition, subtraction);
std::vector<Problem*>::iterator selected = problems.begin();
for(auto *problem : problems)
if(problem)
problem->Draw(problem == *selected);
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)
delete problem;
}
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 y = 1; y < LINES - 10; y += 9)
{ {
for(auto x = 2; x < COLS - 16; x += 15) for(auto x = 2; x < COLS - 16; x += 15)
@ -78,21 +134,8 @@ int main(int argc, char *argv[])
else else
test = new Problem(HARD_SUBTRACTION, 5, x, y); test = new Problem(HARD_SUBTRACTION, 5, x, y);
} }
problems.push_back(test); rtn.push_back(test);
} }
} }
return rtn;
for(auto *problem : problems)
if(problem)
problem->Draw(false);
getch();
for(auto *problem : problems)
if(problem)
delete problem;
}
endwin();
return 0;
} }