// test: echo -e 'cos (quit)' | calc.exe 2>&1 | grep -q error
// test: echo -e '(quit)' | calc.exe 2>&1 | grep -q error
// test: echo -e 'cos 3.14\n!\n! 3 4' | calc.exe 2>&1 | grep -c error | xargs test 3 =
+// test: echo -e 'sto (2, pi)\ncond (rcl (2) > 2, log (64), exp (75))' | calc.exe | grep -q '=> 4\.15888'
+// test: echo -e 'sto (2, 1)\ncond (rcl (2) > 2, log (64), exp (75 / 10))' | calc.exe | grep -q '=> 1808\.04'
+// test: echo -e 'cond (0, 1, 2)' | calc.exe -v 3 | grep -q Cond
+// test: echo -e 'cond\ncond (\ncond (1 >0,'| calc.exe 2>&1 | grep -c error | xargs test 3 =
/* vim: set ts=4 sw=4 et: */
{ "|", Or, 2, 1, -2}
};
-#define NB_FUNCTIONS 13
+#define NB_FUNCTIONS 14
keyword_t functions[NB_FUNCTIONS] = {
{ "sqrt", Sqr, 1, 4, 5},
{ "pow", Pow, 2, 3, 5},
{ "disp", Disp, 0, 4, 9},
{ "quit", Quit, 0, 4, 9},
{ "help", Help, 0, 4, 9},
- { "!", Not, 1, 1, 6}
+ { "!", Not, 1, 1, 6},
+ { "cond", Cond, 3, 4, 5}
};
#define NB_CONSTANTS 3
case And: func = "And"; break;
case Or: func = "Or"; break;
case Not: func = "Not"; break;
+ case Cond: func = "Condition"; break;
}
fprintf (stdout, "Function: %s\n", func);
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, "logical 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");
- fprintf (stdout, " sto rcl\n\n");
- fprintf (stdout, "miscellaneous functions:\n");
- fprintf (stdout, " quit help\n\n");
- fprintf (stdout, "supported constants:\n");
+ fprintf (stdout, "supported operators:");
+ fprintf (stdout, " + - * / %% ^\n");
+ fprintf (stdout, "camparison operators:");
+ fprintf (stdout, " == != >= <= > <\n");
+ fprintf (stdout, "logical operators:");
+ fprintf (stdout, " & | !\n");
+ fprintf (stdout, "supported functions:");
+ fprintf (stdout, " pow sqrt cos sin atan log exp\n");
+ fprintf (stdout, "storage functions:");
+ fprintf (stdout, " sto rcl\n");
+ fprintf (stdout, "conditional functions:");
+ fprintf (stdout, " cond\n");
+ fprintf (stdout, "miscellaneous functions:");
+ fprintf (stdout, " quit help\n");
+ fprintf (stdout, "supported constants:");
fprintf (stdout, " e pi\n");
}
case Exp:
case Recall:
case Not:
+ case Cond:
if (root->ops[0]) {
op0 = evaluate_element (root->ops[0], 0);
} else {
case And: return (op0 != 0) && (op1 != 0);
case Or: return (op0 != 0) || (op1 != 0);
case Not: return (op0 == 0);
+ case Cond:
+ if ((op0) && (root->ops[1])) {
+ return evaluate_element (root->ops[1], 0);
+ } else if ((!op0) && (root->ops[2])) {
+ return evaluate_element (root->ops[2], 0);
+ } else {
+ return 0;
+ }
}
return 0;