forked from rizonesoft/Notepad3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample2.cpp
More file actions
36 lines (31 loc) · 1.02 KB
/
example2.cpp
File metadata and controls
36 lines (31 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "tinyexpr.h"
#include <stdio.h>
int main(int argc, char* argv[])
{
if (argc < 2) {
printf("Usage: example2 \"expression\"\n");
return 0;
}
const char* expression = argv[1];
printf("Evaluating:\n\t%s\n", expression);
/* This shows an example where the variables
* x and y are bound at eval-time. */
double x{ 0 }, y{ 0 };
// Store variable names and pointers.
te_parser tep;
tep.set_vars({ {"x", &x}, {"y", &y} });
/* This will compile the expression and check for errors. */
if (tep.compile(expression)) {
/* The variables can be changed here, and eval can be called as many
* times as you like. This is fairly efficient because the parsing has
* already been done. */
x = 3; y = 4;
const double r = tep.evaluate();
printf("Result:\n\t%f\n", r);
}
else {
/* Show the user where the error is at. */
printf("\t%*s^\nError near here", tep.get_last_error_position(), "");
}
return 0;
}