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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#include <cstdio>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <infix_calc/infix_calc.hpp>
int main(int argc, char *argv[])
{
if( argc != 2 ) {
fprintf(stderr, "Usage: %s expression\n", argv[0]);
return EXIT_FAILURE;
}
const char* data = argv[1];
printf("Expression: %s\n", data);
infix_calc::compiler cc;
cc.variables["x"] = 2.0;
cc.variables["y"] = 3.0;
printf("Vars: %s\n", rpn_calc::to_string(cc.variables).c_str());
{
const bool pok = cc.parse(data, ::strlen(data));
printf("Vanilla RPN: %s\n", cc.rpn_expr.toString().c_str());
if( !pok ) {
std::cerr << "Error occurred @ parsing: " << cc.location() << std::endl;
return EXIT_FAILURE;
}
}
double res = 0.0;
rpn_calc::RPNStatus estatus = cc.eval(res);
if( rpn_calc::RPNStatus::No_Error != estatus ) {
printf("Error occurred @ eval(Vanilla): %s\n", rpn_calc::to_string(estatus).c_str());
return EXIT_FAILURE;
}
printf("Vanilla Result: %f\n", res);
estatus = cc.reduce();
if( rpn_calc::RPNStatus::No_Error != estatus ) {
printf("Error occurred @ reduce: %s\n", rpn_calc::to_string(estatus).c_str());
return EXIT_FAILURE;
}
printf("Reduced RPN: %s\n", cc.rpn_expr.toString().c_str());
double res2 = 0.0;
estatus = cc.eval(res2);
if( rpn_calc::RPNStatus::No_Error != estatus ) {
printf("Error occurred @ eval(Reduced): %s\n", rpn_calc::to_string(estatus).c_str());
return EXIT_FAILURE;
}
printf("Reduced Result: %f\n", res2);
if( res != res2 ) {
printf("Error result vanilla %f != reduced %f\n", res, res2);
return EXIT_FAILURE;
}
printf("Success\n");
return EXIT_SUCCESS;
}
|