add answer feature
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Fri, 20 Jan 2023 15:47:08 +0000 (16:47 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Fri, 20 Jan 2023 15:47:08 +0000 (16:47 +0100)
calc.c
parser.c
parser.h

diff --git a/calc.c b/calc.c
index 456443bd4dfeee49173f6e921a4f9959fae6482a..960bc1495e8b616493dba4838e005eeec59044e3 100644 (file)
--- 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'
 
index f3836c0e83550feeb75b1e5672054e3c46bf5546..776d06b65823f858b6bf792d83e5933b1207f3d5 100644 (file)
--- 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;
     }
index 619ebac68b87c2b8a9f447d60608ffa7218fae0b..10fec970e2bb0c055151b4b01bac1f20d6f485ff 100644 (file)
--- 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 */