diff options
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/calculator.hpp | 32 | ||||
-rw-r--r-- | src/include/tokenizer.hpp | 47 |
2 files changed, 79 insertions, 0 deletions
diff --git a/src/include/calculator.hpp b/src/include/calculator.hpp new file mode 100644 index 0000000..8c5e97a --- /dev/null +++ b/src/include/calculator.hpp @@ -0,0 +1,32 @@ +#pragma once +#include "LCD_I2C.hpp" +#include "keypad.hpp" +#include "tokenizer.hpp" +#define LCD_COLUMNS 16 +#define LCD_ROWS 2 +#define I2C_ADDRESS 0x27 + +class Calculator { +private: + LCD_I2C *display; + Keypad *default_keypad; + Tokenizer tokenizer = Tokenizer(); + char default_keypad_chars[4][4] = { + {'1', '2', '3', '+'}, + {'4', '5', '6', '-'}, + {'7', '8', '9', '*'}, + {'(', '0', ')', '='} + }; + char secondary_keypad[4][4] = { + {'1', '2', '3', '+'}, + {'4', '5', '6', '-'}, + {'7', '8', '9', '/'}, + {'(', '0', ')', '='} + }; + uint keypad_col_pins[4] = {6, 7, 8, 9}; + uint keypad_row_pins[4] = {16, 17, 18, 19}; +public: + Calculator(); + ~Calculator(); + void run(); +}; diff --git a/src/include/tokenizer.hpp b/src/include/tokenizer.hpp new file mode 100644 index 0000000..a3f9998 --- /dev/null +++ b/src/include/tokenizer.hpp @@ -0,0 +1,47 @@ +#pragma once +#include <regex> +#include <string> +#include <array> + +enum Type { + operand, + sum, + substraction, + multiplication, + division, + left_parens, + right_parens, + equals +}; + +struct Token { + Type type; + int value; +}; + +class Tokenizer { +private: + // Private attributes + std::array<Token *, 16> tokens; + const std::string *to_tokenize; + size_t current_char_index; + short tokens_head; + // Regexes + std::regex operand_regex = std::regex("^0{1}|\\d+"); + std::regex operation_regex = std::regex("\\+|-|/|\\*"); + std::regex parens_regex = std::regex("(|)"); + // End of private attributes + // Private methods + void clearTokens(); + void insertToken(Type type, int value); + void matchOperation(const std::string &operation); + void matchParens(const std::string &operation); + void matchOperand(const std::string &operation); + std::string buildNumber(char *current); + // End of private methods +public: + Tokenizer(); + ~Tokenizer(); + const std::array<Token *, 16> &tokenize(const std::string &operation); + const std::array<Token *, 16> &getTokens(); +}; |