new function: format
authorLaurent Mazet <mazet@softndesign.org>
Sat, 25 Feb 2023 15:04:42 +0000 (16:04 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Sat, 25 Feb 2023 15:04:42 +0000 (16:04 +0100)
calc.c
element.h
format.c
format.h
parser.c
stack.c
storage.c

diff --git a/calc.c b/calc.c
index 1432acc275fb7c496e29b3b024da4db6783364df..bd816b982b0a7a2d2c58ea659e858628f54360b4 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -130,7 +130,7 @@ int main (int argc, char *argv[])
                 VERBOSE (ERROR, fprintf (stderr, "%s: missing output prompt\n", progname); usage (1));
                 return 1;
             }
-            oprompt = arg;
+            set_prompt (oprompt = arg);
             break;
         case 'p':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
@@ -138,7 +138,7 @@ int main (int argc, char *argv[])
                 VERBOSE (ERROR, fprintf (stderr, "%s: missing precision\n", progname); usage (1));
                 return 1;
             }
-            precision = atoi (arg);
+            set_precision (precision = atoi (arg));
             break;
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
@@ -155,7 +155,7 @@ int main (int argc, char *argv[])
     }
 
     /* set format */
-    set_format (oprompt, precision);
+    set_format ();
 
     /* completion list*/
     completion_list = generate_completion_list ();
@@ -404,6 +404,12 @@ int main (int argc, char *argv[])
 // test: echo -e 'set (1, 2, -5, 6, -8, 9, -2, 23, 4)\nmin (5, -3)\nmax (-1)\nmin\nmean\nmed\nmax\nord\nprod\nsum\nvar\nquit' | calc.exe -n -v 3 | grep -q bye
 // test: echo -e 'min\nmean\nmed\nmax\nprod\nsum\nvar\nord\nset (1)\nord' | calc.exe -n | grep -c error | xargs test 9 =
 // test: echo -e 'prog (1, cos(pi * arg (1))) / 4' | calc.exe | grep -c error | xargs test 1 = 
+// test: echo -e 'format\n.12345678901' | calc.exe | grep -n '=> 6'
+// test: echo -e 'format (8)\n.12345678901' | calc.exe | grep -n '=> 0.12345679'
+// test: echo -e 'format (12)\n.12345678901' | calc.exe | grep -n '=> 0.12345678901'
+// test: echo -e 'format (4)\n.12345678901\format' | calc.exe | grep -n '=> 4'
+// test: echo -e 'format (0)' | calc.exe | grep -n 'error'
+
 // Gauss sequence
 // test: echo -e '{sto (1, 0), sto (10, 0), while (inc (10) <= 100, {sto (1, rcl (1) + rcl (10)), print (rcl (1))})};' | calc.exe | grep -q '=> 5050'
 
index 15e6ecc2eb991302afb9a4ec412c839b3944c918..c2acb4dca61dab002177f2c5d5ebdc4884dc679e 100644 (file)
--- a/element.h
+++ b/element.h
@@ -22,7 +22,8 @@ typedef enum {
     Cond, While, Code, Print,
     Prog, Arg, Call, List, Edit, Del,
     Get, Length, Pop, Push, Put, Set, Show,
-    Max, Mean, Median, Min, Order, Prod, Sum, Variance
+    Max, Mean, Median, Min, Order, Prod, Sum, Variance,
+    Precision
 } func_t;
 
 /* keyword type */
index 93000a2d462f178d33cb32841c4d5dc58b4ba80b..152875022ad8a10f826378c7d611a5abd5d6edd4 100644 (file)
--- a/format.c
+++ b/format.c
@@ -7,36 +7,76 @@
 /* global variables */
 
 #define DEFAULT_FORMAT "=> %.6g\n"
-char *format = NULL;
-char *minform = NULL;
+char *_format = NULL;
+#define DEFAULT_MINFORM "%.6g"
+char *_minform = NULL;
+
+int _precision = 6;
+
+#define DEFAULT_PROMPT "=> "
+char *_prompt = NULL;
 
 /* print function */
 
-void set_format (char *prompt, int precision)
+void set_precision (int precision)
+{
+    _precision = precision;
+}
+
+int get_precision ()
+{
+    return _precision;
+}
+
+void set_prompt (char *prompt)
+{
+    if (_prompt) {
+        free (_prompt);
+    }
+    _prompt = strdup (prompt);
+}
+
+void set_format ()
 {
     char buffer[128] = {0};
-    free_format ();
-    sprintf (buffer, "%s%%.%dg\n", prompt, precision);
-    format = strdup (buffer);
-    sprintf (buffer, "%%.%dg", precision);
-    minform = strdup (buffer);
+    sprintf (buffer, "%s%%.%dg\n", _prompt ? _prompt : DEFAULT_PROMPT, _precision);
+    if (_format) {
+        free (_format);
+    }
+    _format = strdup (buffer);
+    sprintf (buffer, "%%.%dg", _precision);
+    if (_minform) {
+        free (_minform);
+    }
+    _minform = strdup (buffer);
 }
 
 void free_format ()
 {
-    if (format) {
-        free (format);
-        format = NULL;
+    if (_format) {
+        free (_format);
+        _format = NULL;
+    }
+    if (_minform) {
+        free (_minform);
+        _minform = NULL;
     }
-    if (minform) {
-        free (minform);
-        minform = NULL;
+    if (_prompt) {
+        free (_prompt);
+        _prompt = NULL;
     }
 }
 
 double print (double value)
 {
-    fprintf (stdout, format ? format : DEFAULT_FORMAT, value);
+    fprintf (stdout, _format ? _format : DEFAULT_FORMAT, value);
+    fflush (stdout);
+    return value;
+}
+
+double printl (double value)
+{
+    fprintf (stdout, _minform ? _minform : DEFAULT_MINFORM, value);
     fflush (stdout);
     return value;
 }
index 98492056673e6bffafeb1463bed5e27efa7754ba..dbbaec1ff3b54585311ced76fb96572063c713b9 100644 (file)
--- a/format.h
+++ b/format.h
@@ -3,16 +3,17 @@
 
 /* global variables */
 
-extern char *format;
-extern char *minform;
-
 /* print function */
 
-void set_format (char *prompt, int precision);
+void set_precision (int precision);
+int get_precision ();
+void set_prompt (char *prompt);
+void set_format ();
 
 void free_format ();
 
 double print (double value);
+double printl (double value);
 
 #endif /* __FORMAT_H__ */
 
index bf1a7dc9a0a95c17096d51f6b89850ed62d659c0..b9823abaa25f10acd08cc4f40e2667a2e94ea902 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -62,7 +62,7 @@ keyword_t operators[NB_OPERATORS] = {
     { "|",   Or, 2, 1, -2}
 };
 
-#define NB_FUNCTIONS 50
+#define NB_FUNCTIONS 51
 keyword_t functions[NB_FUNCTIONS] = {
     { "sqrt", Sqr, 1, 4, 5},
     { "pow",  Pow, 2, 3, 5},
@@ -114,6 +114,7 @@ keyword_t functions[NB_FUNCTIONS] = {
     { "prod", Prod, 0, 4, 5},
     { "sum",  Sum, 0, 3, 5},
     { "var",  Variance, 2, 3, 5},
+    { "format", Precision, 1, 6, 9}
 };
 
 #define NB_CONSTANTS 3
@@ -522,6 +523,7 @@ void print_element (element_t *root, int level)
     case Prod: func = "Product"; break;
     case Sum: func = "Sum"; break;
     case Variance: func = "Variance"; break;
+    case Precision: func = "Precision"; break;
     }
 
     fprintf (stdout, "Function: %s\n", func);
@@ -617,7 +619,7 @@ void help (void)
     fprintf (stdout, "stack func.:");
     fprintf (stdout, " max mean med min ord prod sum var\n");
     fprintf (stdout, "control management:");
-    fprintf (stdout, " help quit\n");
+    fprintf (stdout, " format help quit\n");
     fprintf (stdout, "constants:");
     fprintf (stdout, " ans e pi\n");
 }
@@ -774,6 +776,10 @@ double evaluate_element (element_t *root, char mask)
             op0 = evaluate_element (root->ops[0], 0);
             op1 = (root->ops[1]) ? evaluate_element (root->ops[1], 0) : answer;
         }
+        break;
+    case Precision:
+        op0 = (root->ops[0]) ? evaluate_element (root->ops[0], 0) : -1;
+        break;
     }
 
     switch (root->func) {
@@ -887,6 +893,15 @@ double evaluate_element (element_t *root, char mask)
             return op0 * op0 + op1 * op1;
         }
         return variance ();
+    case Precision:
+        if (op0 > 0) {
+            set_precision ((int)op0);
+            set_format ();
+        } else if (op0 != -1) {
+            VERBOSE (WARNING, fprintf (stdout, "error incorrect precision (%g)\n", op0));
+            return 0;
+        }
+        return get_precision();
     }
 
     return 0;
diff --git a/stack.c b/stack.c
index 081023fa985c4d7f3d097e7483662c04096d59a3..f4117f5ce0fd397432933cc8062c219f6e15bbf0 100644 (file)
--- a/stack.c
+++ b/stack.c
@@ -62,12 +62,12 @@ double set (int nbops, element_t **ops)
 void show (void)
 {
     int i, n = size_tab (stack);
-    fprintf (stdout, "stack:");
+    printf ("stack:");
     for (i = 0; i < n; i++) {
-        fprintf (stdout, " ");
-        fprintf (stdout, minform, get_tab (stack, i + 1));
+        printf (" ");
+        printl (get_tab (stack, i + 1));
     }
-    fprintf (stdout, "\n");
+    printf ("\n");
 }
 
 /* stack functions */
index 54815e2d98e69c299e208920ad4350128ce3b03c..15a64702a46b92278eaf34bf44f0ff5816419acd 100644 (file)
--- a/storage.c
+++ b/storage.c
@@ -73,12 +73,12 @@ void display (void)
         memory (DEFAULT_STORAGE_SIZE);
     }
     int i, n = size_tab (storage);
-    fprintf (stdout, "storage:");
+    printf ("storage:");
     for (i = 0; i < n; i++) {
-        fprintf (stdout, " ");
-        fprintf (stdout, minform, get_tab (storage, i + 1));
+        printf (" ");
+        printl (get_tab (storage, i + 1));
     }
-    fprintf (stdout, "\n");
+    printf ("\n");
 }
 
 void clear ()