X-Git-Url: https://secure.softndesign.org/git/?a=blobdiff_plain;f=calc.c;h=82ec9712dde481364f8359303c27313cc2915509;hb=686c4bc2a512a57b6c672792b6b44e2d61458dfb;hp=456443bd4dfeee49173f6e921a4f9959fae6482a;hpb=471da7c96240e4a061ed952d82ecdb85dbaceb6e;p=calc.git diff --git a/calc.c b/calc.c index 456443b..82ec971 100644 --- a/calc.c +++ b/calc.c @@ -71,7 +71,7 @@ int main (int argc, char *argv[]) while (argc-- > 1) { char *arg = *(++argv); if (arg[0] != '-') { - fprintf (stderr, "%s: invalid option -- %s\n", progname, arg); + fprintf (stderr, "%s: invalid option -- '%s'\n", progname, arg); return usage (1); } char c = arg[1]; @@ -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 */ @@ -142,11 +142,12 @@ int main (int argc, char *argv[]) } nb = 0; char *pt = line[nb++] = buffer; - while (*pt++ != '\0') { + while (*pt != '\0') { if (*pt == '\n') { *pt = '\0'; - line[nb++] = ++pt; + line[nb++] = pt + 1; } + pt++; } VERBOSE (INFO, fprintf (stdout, "line: '%s'\n", buffer)); } @@ -158,14 +159,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; @@ -224,11 +223,17 @@ int main (int argc, char *argv[]) // test: echo "95 - 6.3 * 15 - 1" | calc.exe | grep -q '=> -0\.5' // test: echo "95-6.3+15" | calc.exe | grep -q '=> 103.7' // test: echo "-cos (0) + 1" | calc.exe | grep -q '=> -0' +// test: echo "-cos(0)+1" | calc.exe | grep -q '=> -0' // test: echo "quit" | calc.exe | grep -q 'bye' // test: echo "help" | calc.exe | grep -q 'miscellaneous' // test: echo "1 + 2 *" | calc.exe | grep -q 'error' // test: echo "* 1 - 2" | calc.exe | grep -q 'error' // test: echo "2 + * 3" | calc.exe | grep -q 'error' +// test: echo "2 + cos(3 *)" | calc.exe | grep -q 'error' +// test: echo "2 + (foo)" | calc.exe | grep -q 'error' +// test: echo "2 + cos (pi +" | calc.exe | grep -q 'error' +// test: echo "2 + cos (pi" | calc.exe | grep -vq 'error' +// test: echo "cos (1, 2)" | 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' @@ -237,9 +242,44 @@ 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 '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 'sin (pi / 2)' | calc.exe | grep -q 1 +// 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\npi\nsto (1)\nrcl (2)\ndisp\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 12 = +// test: echo -e '1 + 1\nans' | calc.exe -p 3 | grep -c 2 | xargs test 2 = +// test: echo -e 'sin (pi / 2)' | calc.exe -p 4 | grep -q 1 // test: echo -e 'e ^ 2' | calc.exe | grep -q '7\.38906' +// test: echo -e '\n\n\n' | calc.exe | grep -qv 'error' +// test: echo -e '\n\n\n' | calc.exe -n +// test: echo -e '1.5\nsto (2)\n3 + rcl(2) * 4\nsto (5)' | calc.exe | grep -q 9 +// test: echo -e '1\nsto (0)\nsto (11)\nrcl (0)\nrcl (11)' | calc.exe | grep -c invalid | xargs test 4 = +// test: echo -e '1\nsto (2)\n3\nsto (5, 7)\nsto(9)\ndisp' | calc.exe | grep -q '0 1 0 0 7 0 0 0 7 0' +// test: echo -e '1+1 == 2' | calc.exe | grep -q '=> 1' +// test: echo -e '1 + 1 == 2 - 0' | calc.exe | grep -q '=> 1' +// test: echo -e '1 == 1 + 1 == 1' | calc.exe | grep -q '=> 0' +// test: echo -e '1-cos(3*pi/4) != 0.5^-2' | calc.exe | grep -q '=> 1' +// test: echo -e '1 >= 2' | calc.exe | grep -q '=> 0' +// test: echo -e '2 >= 2' | calc.exe | grep -q '=> 1' +// test: echo -e '1 <= 2' | calc.exe | grep -q '=> 1' +// test: echo -e '2 <= 2' | calc.exe | grep -q '=> 1' +// test: echo -e '1 > 2' | calc.exe | grep -q '=> 0' +// test: echo -e '2 > 2' | calc.exe | grep -q '=> 0' +// test: echo -e '1 < 2' | calc.exe | grep -q '=> 1' +// test: echo -e '2 < 2' | calc.exe | grep -q '=> 0' +// test: echo -e '1 == 1\n1 != 1\n1 >= 1\n1 <= 1\n1 > 1\n1 < 1\nquit' | calc.exe -v 3 | grep -q bye +// test: echo -e '(3 == 3) & (2 > 1)' | calc.exe | grep -q '=> 1' +// test: echo -e '(3 == 4) & (2 > 1)' | calc.exe | grep -q '=> 0' +// test: echo -e '(3 == 3) & (2 > 2)' | calc.exe | grep -q '=> 0' +// test: echo -e '(3 == 4) & (2 > 2)' | calc.exe | grep -q '=> 0' +// test: echo -e '(3 == 3) | (2 > 1)' | calc.exe | grep -q '=> 1' +// test: echo -e '(3 == 4) | (2 > 1)' | calc.exe | grep -q '=> 1' +// test: echo -e '(3 == 3) | (2 > 2)' | calc.exe | grep -q '=> 1' +// test: echo -e '(3 == 4) | (2 > 2)' | calc.exe | grep -q '=> 0' +// test: echo -e '!(3 == 4)' | calc.exe | grep -q '=> 1' +// test: echo -e '!(3 == 3)' | calc.exe | grep -q '=> 0' +// test: echo -e '1 & 1\n1 | 1\n!(1)\nquit' | calc.exe -v 3 | grep -q bye +// test: echo -e '(3 == 3) & (4 > 2)' | calc.exe | grep -q '=> 1' +// test: echo -e '3 == 3 & 4 > 2' | calc.exe | grep -q '=> 1' +// test: echo -e '1 + quit' | calc.exe 2>&1 | grep -q error +// test: echo -e 'cos (quit)' | calc.exe 2>&1 | grep -q error +// test: echo -e '(quit)' | calc.exe 2>&1 | grep -q error /* vim: set ts=4 sw=4 et: */