From 9a390136f9040f64ad9a2366232c0713fe798130 Mon Sep 17 00:00:00 2001 From: Gijs Noorlander Date: Sun, 16 Sep 2018 01:04:37 +0200 Subject: [PATCH] Revert "Revert "Added 2 new operators in Calculate() function"" This reverts commit fa42cadfa155295ee81649c3a01c6d7f43e501cc. --- src/Misc.ino | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/Misc.ino b/src/Misc.ino index af1fd33a13..560d487f3f 100644 --- a/src/Misc.ino +++ b/src/Misc.ino @@ -1700,7 +1700,8 @@ float globalstack[STACK_SIZE]; float *sp = globalstack - 1; float *sp_max = &globalstack[STACK_SIZE - 1]; -#define is_operator(c) (c == '+' || c == '-' || c == '*' || c == '/' || c == '^') +#define is_operator(c) (c == '+' || c == '-' || c == '*' || c == '/' || c == '^' || c == '%') +#define is_unary_operator(c) (c == '!') int push(float value) { @@ -1733,6 +1734,8 @@ float apply_operator(char op, float first, float second) return first * second; case '/': return first / second; + case '%': + return round(first) % round(second); case '^': return pow(first, second); default: @@ -1740,6 +1743,17 @@ float apply_operator(char op, float first, float second) } } +float apply_unary_operator(char op, float first) +{ + switch (op) + { + case '!': + return (round(first) == 0) ? 1 : 0; + default: + return 0; + } +} + char *next_token(char *linep) { while (isspace(*(linep++))); @@ -1759,8 +1773,14 @@ int RPNCalculate(char* token) if (push(apply_operator(token[0], first, second))) return CALCULATE_ERROR_STACK_OVERFLOW; - } - else // Als er nog een is, dan deze ophalen + } else if (is_unary_operator(token[0]) && token[1] == 0) + { + float first = pop(); + + if (push(apply_unary_operator(token[0], first))) + return CALCULATE_ERROR_STACK_OVERFLOW; + + } else // Als er nog een is, dan deze ophalen if (push(atof(token))) // is het een waarde, dan op de stack plaatsen return CALCULATE_ERROR_STACK_OVERFLOW; @@ -1776,10 +1796,13 @@ int op_preced(const char c) { switch (c) { + case '!': + return 4; case '^': return 3; case '*': case '/': + case '%': return 2; case '+': case '-': @@ -1797,8 +1820,10 @@ bool op_left_assoc(const char c) case '/': case '+': case '-': + case '%': return true; // left to right - //case '!': return false; // right to left + case '!': + return false; // right to left } return false; } @@ -1812,8 +1837,10 @@ unsigned int op_arg_count(const char c) case '/': case '+': case '-': + case '%': return 2; - //case '!': return 1; + case '!': + return 1; } return 0; } @@ -1854,7 +1881,7 @@ int Calculate(const char *input, float* result) } // If the token is an operator, op1, then: - else if (is_operator(c)) + else if (is_operator(c) || is_unary_operator(c)) { *(TokenPos) = 0; error = RPNCalculate(token);