From bc4a271d172b765a54347ef89bcc67cadb782239 Mon Sep 17 00:00:00 2001 From: "David Vereb (Home)" Date: Mon, 1 Apr 2019 17:47:16 -0500 Subject: [PATCH] WIP. --- Makefile | 2 +- Problem.cpp | 33 ++++++++------ Problem.h | 3 +- main.cpp | 125 +++++++++++++++++++++++++++++++++++----------------- 4 files changed, 105 insertions(+), 58 deletions(-) diff --git a/Makefile b/Makefile index c74f605..8043f31 100644 --- a/Makefile +++ b/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 diff --git a/Problem.cpp b/Problem.cpp index 9f08507..cdb5428 100644 --- a/Problem.cpp +++ b/Problem.cpp @@ -3,6 +3,7 @@ #include #include #include +#include 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 diff --git a/Problem.h b/Problem.h index 06bed60..a458710 100644 --- a/Problem.h +++ b/Problem.h @@ -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; diff --git a/main.cpp b/main.cpp index af65c4c..bf9f468 100644 --- a/main.cpp +++ b/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 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 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 problems = GenerateProblems(addition, subtraction); + std::vector::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 GenerateProblems(bool addition, bool subtraction) +{ + std::vector 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; +}