From a8cf32baa8ea2ef818547186e3b2ea03a07191d6 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Mon, 23 Jan 2023 23:17:23 +0100 Subject: [PATCH] add increase and decrease features --- calc.c | 2 ++ parser.c | 32 ++++++++++++++++++++++++++++++-- parser.h | 2 +- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/calc.c b/calc.c index 4ced168..02d0d98 100644 --- a/calc.c +++ b/calc.c @@ -287,5 +287,7 @@ int main (int argc, char *argv[]) // test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe | grep -q '=> 1808\.04' // test: echo -e 'cond (0, 1, 2)' | calc.exe -v 3 | grep -q Cond // test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe 2>&1 | grep -c error | xargs test 3 = +// test: echo -e 'sto (1, 4)\ninc (1)\ninc (1)\nrcl (1) == 6' | calc.exe -v 3 | grep -q '=> 1' +// test: echo -e 'inc\ninc (\ndec\ndec ('| calc.exe 2>&1 | grep -c error | xargs test 4 = /* vim: set ts=4 sw=4 et: */ diff --git a/parser.c b/parser.c index 8007f86..c347287 100644 --- a/parser.c +++ b/parser.c @@ -87,7 +87,7 @@ keyword_t operators[NB_OPERATORS] = { { "|", Or, 2, 1, -2} }; -#define NB_FUNCTIONS 14 +#define NB_FUNCTIONS 16 keyword_t functions[NB_FUNCTIONS] = { { "sqrt", Sqr, 1, 4, 5}, { "pow", Pow, 2, 3, 5}, @@ -98,6 +98,8 @@ keyword_t functions[NB_FUNCTIONS] = { { "log", Log, 1, 3, 5}, { "sto", Store, 2, 3, 9}, { "rcl", Recall, 1, 3, 5}, + { "inc", Inc, 1, 3, 5}, + { "dec", Dec, 1, 3, 5}, { "disp", Disp, 0, 4, 9}, { "quit", Quit, 0, 4, 9}, { "help", Help, 0, 4, 9}, @@ -416,6 +418,8 @@ void print_element (element_t *root, int level) case Exp: func = "Exponantial"; break; case Store: func = "Store"; break; case Recall: func = "Recall"; break; + case Inc: func = "Increase"; break; + case Dec: func = "Decrease"; break; case Disp: func = "Display"; break; case Quit: func = "Quit"; break; case Help: func = "Help"; break; @@ -470,6 +474,26 @@ double recall (int index) return 0; } +double increase (int index) +{ + if ((index > 0) && (index <= STORAGE_SIZE)) { + return storage[index - 1]++; + } else { + VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE)); + } + return 0; +} + +double decrease (int index) +{ + if ((index > 0) && (index <= STORAGE_SIZE)) { + return storage[index - 1]--; + } else { + VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE)); + } + return 0; +} + void display (void) { int i; @@ -502,7 +526,7 @@ void help (void) fprintf (stdout, "supported functions:"); fprintf (stdout, " pow sqrt cos sin atan log exp\n"); fprintf (stdout, "storage functions:"); - fprintf (stdout, " sto rcl\n"); + fprintf (stdout, " sto rcl inc dec\n"); fprintf (stdout, "conditional functions:"); fprintf (stdout, " cond\n"); fprintf (stdout, "miscellaneous functions:"); @@ -580,6 +604,8 @@ double evaluate_element (element_t *root, char mask) case Log: case Exp: case Recall: + case Inc: + case Dec: case Not: case Cond: if (root->ops[0]) { @@ -615,6 +641,8 @@ double evaluate_element (element_t *root, char mask) case Exp: return exp (op0); case Store: return store ((int)op0, (op1) ? op1 : answer); case Recall: return recall ((int)op0); + case Inc: return increase ((int)op0); + case Dec: return decrease ((int)op0); case Disp: display (); break; case Quit: quit (); break; case Help: help (); break; diff --git a/parser.h b/parser.h index 409b4d4..540f1c8 100644 --- a/parser.h +++ b/parser.h @@ -14,7 +14,7 @@ typedef enum { Pow, Sqr, Cos, Sin, Atan, Log, Exp, - Store, Recall, Disp, + Store, Recall, Inc, Dec, Disp, Quit, Help, Ans, E, Pi, Equal, Diff, Ge, Le, Gt, Lt, -- 2.30.2