Merge branch 'master' of https://secure.softndesign.org/git/calc
[calc.git] / calc.c
diff --git a/calc.c b/calc.c
index e255bd2ca421baf948ee8400f8685859555d1960..f57f1a455dfba634e1ef23971892dd790ed55616 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -26,6 +26,7 @@
 /* gobal variables */
 
 char *progname = NULL;
+int precision = 6;
 
 /* help function */
 
@@ -34,6 +35,7 @@ int usage (int ret)
     int fd = ret ? stdfderr : stdfdout;
     fdprintf (fd, "usage: %s\n", progname);
     fdprintf (fd, " -h : help message\n");
+    fdprintf (fd, " -p : precision (%d)\n", precision);
     fdprintf (fd, " -v : verbose level (%d)\n", verbose);
 
     return ret;
@@ -70,6 +72,14 @@ int main (int argc, char *argv[])
         }
         char c = arg[1];
         switch (c) {
+        case 'p':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                PRINTERR ("%s: missing precision\n", progname);
+                return usage (1);
+            }
+            precision = atoi (arg);
+            break;
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
@@ -83,6 +93,10 @@ int main (int argc, char *argv[])
             return usage (c != 'h');
         }
     }
+    
+    /* format */
+    char format[8] = "=> %.f\n";
+    format[4] = '0' + precision;
 
     /* read from input stream */
 
@@ -104,7 +118,7 @@ int main (int argc, char *argv[])
                     ret = 1;
                        } else {
                     VERBOSE (INFO, print_element (element, 0));
-                    PRINTOUT ("=> %f\n", evaluate_element (element, 0));
+                    PRINTOUT (format, evaluate_element (element, 0));
                     delelement (element);
                     ret = 0;
                 }
@@ -133,6 +147,9 @@ int main (int argc, char *argv[])
 // test: echo 1 | calc.exe -v3 | grep -q value
 // test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
 // test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
+// test: calc.exe error 2>&1 | grep -q 'invalid option'
+// test: calc.exe -p 2>&1 | grep -q 'missing precision'
+// test: calc.exe -v 2>&1 | grep -q 'missing verbose'
 // test: echo "1 + 2" | calc.exe | grep -q '=> 3'
 // test: echo "1 - 2" | calc.exe | grep -q '=> -1'
 // test: echo "2 * 3" | calc.exe | grep -q '=> 6'
@@ -174,5 +191,10 @@ int main (int argc, char *argv[])
 // test: echo "2 + * 3" | calc.exe | grep -q 'error'
 // test: echo "sqrt 2" | calc.exe | grep -q 'error'
 // test: echo "pow (2)" | calc.exe | grep -q 'error'
+// test: echo "1.23456789" | calc.exe -p 3 | grep -q '1\.234'
+// test: echo . | calc.exe
+// 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 6.4e1
+// 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)\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 =
 
 /* vim: set ts=4 sw=4 et: */