diff options
Diffstat (limited to 'src/displayapp/screens/Calculator.cpp')
| -rw-r--r-- | src/displayapp/screens/Calculator.cpp | 48 |
1 files changed, 17 insertions, 31 deletions
diff --git a/src/displayapp/screens/Calculator.cpp b/src/displayapp/screens/Calculator.cpp index c44f781..42688a3 100644 --- a/src/displayapp/screens/Calculator.cpp +++ b/src/displayapp/screens/Calculator.cpp @@ -4,7 +4,6 @@ #include <cmath> #include <map> #include <memory> -#include <string> using namespace Pinetime::Applications::Screens; @@ -174,7 +173,7 @@ Calculator::Calculator(DisplayApp* app, Controllers::MotorController& motorContr lv_obj_set_event_cb(buttonMatrix, eventHandler); } -void Calculator::Eval() { +bool Calculator::Eval() { Stack<char, 32> input; for (int8_t i = position - 1; i >= 0; i--) { input.push(text[i]); @@ -191,9 +190,7 @@ void Calculator::Eval() { } if (isdigit(input.top())) { - if (!parseFloat(input, output.push().val, sign)) { - goto eval_error; - } + if (!parseFloat(input, output.push().val, sign)) return false; output.top().op = 0; sign = +1; expectingNumber = false; @@ -207,12 +204,8 @@ void Calculator::Eval() { [[fallthrough]]; case '+': input.pop(); - break; - default: - goto not_sign; + continue; } - continue; - not_sign: ; } char next = input.top(); @@ -233,9 +226,7 @@ void Calculator::Eval() { // or (the operator at the top of the operator stack has equal precedence and the token is left associative)) || (getPrecedence(operators.top()) == getPrecedence(next) && leftAssociative(next)))) { // need two elements on the output stack to add a binary operator - if (output.size() < 2) { - goto eval_error; - } + if (output.size() < 2) return false; output.pushOperator(operators.top()); operators.pop(); } @@ -262,14 +253,10 @@ void Calculator::Eval() { case ')': while (operators.top() != '(') { // need two elements on the output stack to add a binary operator - if (output.size() < 2) { - goto eval_error; - } + if (output.size() < 2) return false; output.pushOperator(operators.top()); operators.pop(); - if (operators.empty()) { - goto eval_error; - } + if (operators.empty()) return false; } // discard the left parentheses operators.pop(); @@ -282,7 +269,7 @@ void Calculator::Eval() { // need two elements on the output stack to add a binary operator (output.size() < 2)) { - goto eval_error; + return false; } output.pushOperator(op); } @@ -294,7 +281,7 @@ void Calculator::Eval() { resultFloat = -resultFloat; } if (!(resultFloat <= UINT32_MAX)) { - goto eval_error; // if too large or NaN + return false; // if too large or NaN } if (sign) { text[0] = '-'; @@ -318,9 +305,7 @@ void Calculator::Eval() { position--; } } - return; - eval_error: - motorController.RunForDuration(10); + return true; } void Calculator::Reset() { @@ -329,16 +314,14 @@ void Calculator::Reset() { } void Calculator::OnButtonEvent(lv_obj_t* obj, lv_event_t event) { - if (event == LV_EVENT_CLICKED) { + switch (event) { + case LV_EVENT_CLICKED: if (obj == buttonMatrix) { const char* buttonstr = lv_btnmatrix_get_active_btn_text(obj); if (*buttonstr == '=') { - Eval(); + if (!Eval()) break; } else { - if (position >= 30) { - motorController.RunForDuration(10); - return; - } + if (position >= 30) break; text[position] = *buttonstr; position++; } @@ -350,10 +333,13 @@ void Calculator::OnButtonEvent(lv_obj_t* obj, lv_event_t event) { return; } } - text[position] = '\0'; lv_label_set_text_static(result, text); + [[fallthrough]]; + default: + return; } + motorController.RunForDuration(10); } bool Calculator::OnTouchEvent(Pinetime::Applications::TouchEvents event) { |
