add answer feature
[calc.git] / calc.c
diff --git a/calc.c b/calc.c
index b247eceb440bdbaa5bcebe308236bf0830f741b5..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,7 +235,10 @@ 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)\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 '-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'
 
 /* vim: set ts=4 sw=4 et: */