From 5075f6ea34c1613e1c352c86e871ecd6d38b5d34 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Fri, 20 Jan 2023 16:47:08 +0100 Subject: [PATCH] add answer feature --- calc.c | 15 +++++++-------- parser.c | 16 ++++++++++++---- parser.h | 6 +++++- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/calc.c b/calc.c index 456443b..960bc14 100644 --- a/calc.c +++ b/calc.c @@ -103,8 +103,8 @@ int main (int argc, char *argv[]) } /* format */ - char format[5] = "%..g"; - format[2] = '0' + precision; + char format[9] = "=> %..g\n"; + format[5] = '0' + precision; /* read from input stream */ @@ -158,14 +158,12 @@ int main (int argc, char *argv[]) } element_t *element = parser (line[i], NULL, 0); if (element == ERROR_OP) { - VERBOSE (WARNING, fprintf (stdout, "error while parsing: %s\n", line[i])); + VERBOSE (WARNING, fprintf (stdout, "error while parsing: %s\n", line[i]); fflush (stdout)); ret = 1; } else { VERBOSE (INFO, print_element (element, 0)); - double value = evaluate_element (element, 0); - char number[256] = {0}; - sprintf (number, format, value); - fprintf (stdout, "=> %s\n", number); + answer = evaluate_element (element, 0); + fprintf (stdout, format, answer); fflush (stdout); delelement (element); ret = 0; @@ -237,8 +235,9 @@ 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)\ne\n\pi\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\n\pi\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 | grep -c 2 | xargs test 2 = // test: echo -e 'sin (pi / 2)' | calc.exe | grep -q 1 // test: echo -e 'e ^ 2' | calc.exe | grep -q '7\.38906' diff --git a/parser.c b/parser.c index f3836c0..776d06b 100644 --- a/parser.c +++ b/parser.c @@ -7,6 +7,10 @@ #include "parser.h" +/* global variables */ + +double answer = 0; + /* compare codes */ int codecmp (char *ref, char *str) @@ -86,10 +90,11 @@ keyword_t functions[NB_FUNCTIONS] = { { "help", Help, 0, 4, 5} }; -#define NB_CONSTANTS 2 +#define NB_CONSTANTS 3 keyword_t constants[NB_CONSTANTS] = { - { "e", E, 0, 1, 5}, - { "pi", Pi, 0, 2, 5} + { "ans", Ans, 0, 3, 5}, + { "e", E, 0, 1, 5}, + { "pi", Pi, 0, 2, 5} }; /* subparser function */ @@ -261,7 +266,7 @@ element_t *parser (char *str, char **next, int prio) if (codecmp (constant->keyword, str) == 0) { VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n")); if (root == NULL) { - VERBOSE (INFO, fprintf (stdout, "Func: %d\n", constant->func)); + VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func)); new = newelement (constant->func, constant->nbops, constant->prio); if (new == NULL) { return ERROR_OP; @@ -370,6 +375,7 @@ void print_element (element_t *root, int level) case Exp: func = "Exponantial"; break; case Quit: func = "Quit"; break; case Help: func = "Help"; break; + case Ans: func = "Ans"; break; case Pi: func = "Pi"; break; case E: func = "E"; break; } @@ -479,6 +485,7 @@ double evaluate_element (element_t *root, char mask) break; case Quit: case Help: + case Ans: case Pi: case E: break; @@ -501,6 +508,7 @@ double evaluate_element (element_t *root, char mask) case Exp: return exp (op0); case Quit: quit (); break; case Help: help (); break; + case Ans: return answer; case Pi: return M_PI; case E: return M_E; } diff --git a/parser.h b/parser.h index 619ebac..10fec97 100644 --- a/parser.h +++ b/parser.h @@ -1,6 +1,10 @@ #ifndef __PARSER_H__ #define __PARSER_H__ +/* global variables */ + +extern double answer; + /* function type */ typedef enum { @@ -11,7 +15,7 @@ typedef enum { Cos, Sin, Atan, Log, Exp, Quit, Help, - E, Pi + Ans, E, Pi } func_t; /* keyword type */ -- 2.30.2