From 7fe742c952b6b315442284b2eea8ddb4c66f24c6 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sun, 22 Jan 2023 15:30:46 +0100 Subject: [PATCH] add comparison operators --- calc.c | 15 +++++++++++++++ parser.c | 32 +++++++++++++++++++++++++++++--- parser.h | 3 ++- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/calc.c b/calc.c index fe75240..0134b74 100644 --- a/calc.c +++ b/calc.c @@ -229,6 +229,10 @@ int main (int argc, char *argv[]) // 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 "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' @@ -247,5 +251,16 @@ int main (int argc, char *argv[]) // 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-cos(3*pi/4) != 0.5^-2' | calc.exe | grep -q '=> 0' +// 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 /* vim: set ts=4 sw=4 et: */ diff --git a/parser.c b/parser.c index 43595e5..844e941 100644 --- a/parser.c +++ b/parser.c @@ -69,7 +69,7 @@ void delelement (element_t *root) /* functions */ -#define NB_OPERATORS 6 +#define NB_OPERATORS 12 keyword_t operators[NB_OPERATORS] = { { "+\t", Add, 2, 1, 1}, @@ -77,7 +77,13 @@ keyword_t operators[NB_OPERATORS] = { { "*", Mul, 2, 1, 2}, { "/", Div, 2, 1, 2}, { "%", Mod, 2, 1, 3}, - { "^", Pow, 2, 1, 4} + { "^", Pow, 2, 1, 4}, + { "==", Equal, 2, 2, 6}, + { "!=", Diff, 2, 2, 6}, + { ">=", Ge, 2, 2, 6}, + { "<=", Le, 2, 2, 6}, + { ">", Gt, 2, 1, 6}, + { "<", Lt, 2, 1, 6} }; #define NB_FUNCTIONS 12 @@ -91,7 +97,7 @@ keyword_t functions[NB_FUNCTIONS] = { { "log", Log, 1, 3, 5}, { "sto", Store, 2, 3, 5}, { "rcl", Recall, 1, 3, 5}, - { "disp", Disp, 0, 4, 5}, + { "disp", Disp, 0, 4, 5}, { "quit", Quit, 0, 4, 5}, { "help", Help, 0, 4, 5} }; @@ -382,6 +388,12 @@ void print_element (element_t *root, int level) case Ans: func = "Ans"; break; case Pi: func = "Pi"; break; case E: func = "E"; break; + case Equal: func = "Equal"; break; + case Diff: func = "Different"; break; + case Ge: func = "Greater or equal"; break; + case Le: func = "Lesser or equal"; break; + case Gt: func = "Greater"; break; + case Lt: func = "Lesser"; break; } fprintf (stdout, "Function: %s\n", func); @@ -445,6 +457,8 @@ void help (void) fprintf (stdout, "calc is a simple calculator\n\n"); fprintf (stdout, "supported operators:\n"); fprintf (stdout, " + - * / %% ^\n\n"); + fprintf (stdout, "camparison operators:\n"); + fprintf (stdout, " == != >= <= > <\n\n"); fprintf (stdout, "supported functions:\n"); fprintf (stdout, " pow sqrt cos sin atan log exp\n\n"); fprintf (stdout, "storage functions:\n"); @@ -502,6 +516,12 @@ double evaluate_element (element_t *root, char mask) case Mod: case Pow: case Store: + case Equal: + case Diff: + case Ge: + case Le: + case Gt: + case Lt: if (root->ops[1]) { op1 = evaluate_element (root->ops[1], nextmask); } else if (root->func != Store) { @@ -555,6 +575,12 @@ double evaluate_element (element_t *root, char mask) case Ans: return answer; case Pi: return M_PI; case E: return M_E; + case Equal: return op0 == op1; + case Diff: return op0 != op1; + case Ge: return op0 >= op1; + case Le: return op0 <= op1; + case Gt: return op0 > op1; + case Lt: return op0 < op1; } return 0; diff --git a/parser.h b/parser.h index 95bfa2f..fb09da2 100644 --- a/parser.h +++ b/parser.h @@ -16,7 +16,8 @@ typedef enum { Log, Exp, Store, Recall, Disp, Quit, Help, - Ans, E, Pi + Ans, E, Pi, + Equal, Diff, Ge, Le, Gt, Lt } func_t; /* keyword type */ -- 2.30.2