From 6ba1dd0f49be10ea6691f912aaa5a8a9c144234e Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sat, 21 Jan 2023 00:55:29 +0100 Subject: [PATCH] storage feature --- calc.c | 5 ++++- parser.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- parser.h | 1 + 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/calc.c b/calc.c index 6ecd2f9..9e21344 100644 --- a/calc.c +++ b/calc.c @@ -237,12 +237,15 @@ int main (int argc, char *argv[]) // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe -n | grep -q 2 // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe | grep -q 64 // test: echo -e '1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1\n1 + 1' | calc.exe | grep -q 2 -// test: echo -e '-cos (1)\n1 + 1\n1 - 1\n1 * 1\n1 / 1\n3%2\n2^2\nsqrt (2)\ncos (0)\nsin (0)\natan (0)\nlog (1)\nexp (1)\nans\ne\npi\nhelp\nquit' | calc.exe -v 3 | grep -q bye +// test: echo -e '-cos (1)\n1 + 1\n1 - 1\n1 * 1\n1 / 1\n3%2\n2^2\nsqrt (2)\ncos (0)\nsin (0)\natan (0)\nlog (1)\nexp (1)\nans\ne\npi\nsto (1)\nrcl (2)\ndisp\nhelp\nquit' | calc.exe -v 3 | grep -q bye // test: echo -e '1 +\n1 -\n1 * 1\n1 /\n3%\n2^\nsqrt ()\ncos ()\nsin ()\natan ()\nlog ()\nexp ()\n1 + (' | calc.exe | grep -c error | xargs test 11 = // test: echo -e '1 + 1\nans' | calc.exe -p 3 | grep -c 2 | xargs test 2 = // test: echo -e 'sin (pi / 2)' | calc.exe -p 4 | grep -q 1 // test: echo -e 'e ^ 2' | calc.exe | grep -q '7\.38906' // test: echo -e '\n\n\n' | calc.exe | grep -qv 'error' // test: echo -e '\n\n\n' | calc.exe -n +// test: echo -e '1.5\nsto (2)\n3 + rcl(2) * 4\nsto (5)' | calc.exe | grep -q 9 +// test: echo -e '1\nsto (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c invalid | xargs test 4 = +// test: echo -e '1\nsto (2)\n3\nsto (5)\ndisp' | calc.exe | grep -q '0 1 0 0 3 0 0 0 0 0' /* vim: set ts=4 sw=4 et: */ diff --git a/parser.c b/parser.c index a1c6666..e2c4673 100644 --- a/parser.c +++ b/parser.c @@ -11,6 +11,9 @@ double answer = 0; +#define STORAGE_SIZE 10 +double storage[STORAGE_SIZE] = {0}; + /* compare codes */ int codecmp (char *ref, char *str) @@ -77,7 +80,7 @@ keyword_t operators[NB_OPERATORS] = { { "^", Pow, 2, 1, 4} }; -#define NB_FUNCTIONS 9 +#define NB_FUNCTIONS 12 keyword_t functions[NB_FUNCTIONS] = { { "sqrt", Sqr, 1, 4, 5}, { "pow", Pow, 2, 3, 5}, @@ -86,6 +89,9 @@ keyword_t functions[NB_FUNCTIONS] = { { "atan", Atan, 1, 4, 5}, { "exp", Exp, 1, 3, 5}, { "log", Log, 1, 3, 5}, + { "sto", Store, 1, 3, 5}, + { "rcl", Recall, 1, 3, 5}, + { "disp", Disp, 0, 4, 5}, { "quit", Quit, 0, 4, 5}, { "help", Help, 0, 4, 5} }; @@ -368,6 +374,9 @@ void print_element (element_t *root, int level) case Atan: func = "Arc Tangent"; break; case Log: func = "Logarithm"; break; case Exp: func = "Exponantial"; break; + case Store: func = "Store"; break; + case Recall: func = "Recall"; break; + case Disp: func = "Display"; break; case Quit: func = "Quit"; break; case Help: func = "Help"; break; case Ans: func = "Ans"; break; @@ -389,6 +398,38 @@ void print_element (element_t *root, int level) } } +/* storage functions */ + +double store (int index) +{ + if ((index > 0) && (index <= STORAGE_SIZE)) { + storage[index - 1] = answer; + } else { + VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE)); + } + return answer; +} + +double recall (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; + fprintf (stdout, "storage:"); + for (i = 0; i < STORAGE_SIZE; i++) { + fprintf (stdout, " %g", storage[i]); + } + fprintf (stdout, "\n"); +} + /* quit function */ void quit (void) @@ -406,6 +447,8 @@ void help (void) fprintf (stdout, " + - * / %% ^\n\n"); fprintf (stdout, "supported functions:\n"); fprintf (stdout, " pow sqrt cos sin atan log exp\n\n"); + fprintf (stdout, "storage functions:\n"); + fprintf (stdout, " sto rcl\n\n"); fprintf (stdout, "miscellaneous functions:\n"); fprintf (stdout, " quit help\n\n"); fprintf (stdout, "supported constants:\n"); @@ -471,6 +514,8 @@ double evaluate_element (element_t *root, char mask) case Atan: case Log: case Exp: + case Store: + case Recall: if (root->ops[0]) { op0 = evaluate_element (root->ops[0], 0); } else { @@ -478,6 +523,7 @@ double evaluate_element (element_t *root, char mask) return 0; } break; + case Disp: case Quit: case Help: case Ans: @@ -501,6 +547,9 @@ double evaluate_element (element_t *root, char mask) case Atan: return atan (op0); case Log: return log (op0); case Exp: return exp (op0); + case Store: return store ((int)op0); + case Recall: return recall ((int)op0); + case Disp: display (); break; case Quit: quit (); break; case Help: help (); break; case Ans: return answer; diff --git a/parser.h b/parser.h index 10fec97..95bfa2f 100644 --- a/parser.h +++ b/parser.h @@ -14,6 +14,7 @@ typedef enum { Pow, Sqr, Cos, Sin, Atan, Log, Exp, + Store, Recall, Disp, Quit, Help, Ans, E, Pi } func_t; -- 2.30.2