// 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)\nans\ne\npi\nhelp\nquit' | calc.exe -v 3 | grep -q bye
+// 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 11 =
// 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)\ndisp' | calc.exe | grep -q '0 1 0 0 3 0 0 0 0 0'
/* vim: set ts=4 sw=4 et: */
double answer = 0;
+#define STORAGE_SIZE 10
+double storage[STORAGE_SIZE] = {0};
+
/* compare codes */
int codecmp (char *ref, char *str)
{ "^", Pow, 2, 1, 4}
};
-#define NB_FUNCTIONS 9
+#define NB_FUNCTIONS 12
keyword_t functions[NB_FUNCTIONS] = {
{ "sqrt", Sqr, 1, 4, 5},
{ "pow", Pow, 2, 3, 5},
{ "atan", Atan, 1, 4, 5},
{ "exp", Exp, 1, 3, 5},
{ "log", Log, 1, 3, 5},
+ { "sto", Store, 1, 3, 5},
+ { "rcl", Recall, 1, 3, 5},
+ { "disp", Disp, 0, 4, 5},
{ "quit", Quit, 0, 4, 5},
{ "help", Help, 0, 4, 5}
};
case Atan: func = "Arc Tangent"; break;
case Log: func = "Logarithm"; break;
case Exp: func = "Exponantial"; break;
+ case Store: func = "Store"; break;
+ case Recall: func = "Recall"; break;
+ case Disp: func = "Display"; break;
case Quit: func = "Quit"; break;
case Help: func = "Help"; break;
case Ans: func = "Ans"; break;
}
}
+/* storage functions */
+
+double store (int index)
+{
+ if ((index > 0) && (index <= STORAGE_SIZE)) {
+ storage[index - 1] = answer;
+ } else {
+ VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
+ }
+ return answer;
+}
+
+double recall (int index)
+{
+ if ((index > 0) && (index <= STORAGE_SIZE)) {
+ return storage[index - 1];
+ } else {
+ VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
+ }
+ return 0;
+}
+
+void display (void)
+{
+ int i;
+ fprintf (stdout, "storage:");
+ for (i = 0; i < STORAGE_SIZE; i++) {
+ fprintf (stdout, " %g", storage[i]);
+ }
+ fprintf (stdout, "\n");
+}
+
/* quit function */
void quit (void)
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");
case Atan:
case Log:
case Exp:
+ case Store:
+ case Recall:
if (root->ops[0]) {
op0 = evaluate_element (root->ops[0], 0);
} else {
return 0;
}
break;
+ case Disp:
case Quit:
case Help:
case Ans:
case Atan: return atan (op0);
case Log: return log (op0);
case Exp: return exp (op0);
+ case Store: return store ((int)op0);
+ case Recall: return recall ((int)op0);
+ case Disp: display (); break;
case Quit: quit (); break;
case Help: help (); break;
case Ans: return answer;