1 #include "tinyexpr.h"
2 #include <stdio.h>
3
main(int argc,char * argv[])4 int main(int argc, char *argv[])
5 {
6 if (argc < 2) {
7 printf("Usage: example2 \"expression\"\n");
8 return 0;
9 }
10
11 const char *expression = argv[1];
12 printf("Evaluating:\n\t%s\n", expression);
13
14 /* This shows an example where the variables
15 * x and y are bound at eval-time. */
16 double x, y;
17 te_variable vars[] = {{"x", &x}, {"y", &y}};
18
19 /* This will compile the expression and check for errors. */
20 int err;
21 te_expr *n = te_compile(expression, vars, 2, &err);
22
23 if (n) {
24 /* The variables can be changed here, and eval can be called as many
25 * times as you like. This is fairly efficient because the parsing has
26 * already been done. */
27 x = 3; y = 4;
28 const double r = te_eval(n); printf("Result:\n\t%f\n", r);
29
30 te_free(n);
31 } else {
32 /* Show the user where the error is at. */
33 printf("\t%*s^\nError near here", err-1, "");
34 }
35
36
37 return 0;
38 }
39