C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
Classes | Functions
exprtk_arithmetic_evaluator.cpp File Reference
#include <cstdio>
#include <string>
#include "exprtk.hpp"
Include dependency graph for exprtk_arithmetic_evaluator.cpp:

Go to the source code of this file.

Classes

struct  char_process< T, Process >
 

Functions

template<typename T >
is_digit_func (const unsigned char c)
 
template<typename T >
is_operator_func (const unsigned char c)
 
template<typename T >
to_num_func (const unsigned char c)
 
template<typename T >
void basic_arithmetic_parser_evaluator ()
 
int main ()
 

Function Documentation

◆ basic_arithmetic_parser_evaluator()

template<typename T >
void basic_arithmetic_parser_evaluator ( )

Definition at line 67 of file exprtk_arithmetic_evaluator.cpp.

68{
69 typedef exprtk::symbol_table<T> symbol_table_t;
70 typedef exprtk::expression<T> expression_t;
71 typedef exprtk::parser<T> parser_t;
72
73 const std::string arithmetic_parser_evaluator_program =
74 " var curr_val := 0; "
75 " var prev_val := 0; "
76 " var result := 0; "
77 " var op := '+'; "
78 " "
79 " for (var i := 0; i < expression[]; i += 1) "
80 " { "
81 " var c := expression[i : i + 1]; "
82 " "
83 " if (is_digit(c)) "
84 " { "
85 " curr_val := curr_val * 10 + to_num(c); "
86 " }; "
87 " "
88 " if (is_opr(c) or (i == expression[] - 1)) "
89 " { "
90 " prev_val := "
91 " switch "
92 " { "
93 " case op == '+' : "
94 " ~{ "
95 " result += prev_val; "
96 " curr_val; "
97 " }; "
98 " "
99 " case op == '-' : "
100 " ~{ "
101 " result += prev_val; "
102 " -curr_val; "
103 " }; "
104 " "
105 " case op == '*' : prev_val * curr_val; "
106 " case op == '/' : prev_val / curr_val; "
107 " }; "
108 " "
109 " curr_val := 0; "
110 " op := c; "
111 " } "
112 " }; "
113 " "
114 " result += prev_val; "
115 " "
116 " println(result, ' = ', expression); "
117 " ";
118
119 std::string arithmetic_expression;
120
125
126 symbol_table_t symbol_table;
127 symbol_table.add_stringvar("expression", arithmetic_expression);
128 symbol_table.add_function ("println" , println );
129 symbol_table.add_function ("is_digit" , isdigit );
130 symbol_table.add_function ("is_opr" , isopr );
131 symbol_table.add_function ("to_num" , tonum );
132
133 expression_t expression;
134 expression.register_symbol_table(symbol_table);
135
136 parser_t parser;
137 parser.compile(arithmetic_parser_evaluator_program, expression);
138
139 /*
140 Note: Expressions can be comprised of integers,
141 basic arithmetic operators (+, -, *, /),
142 and shall not contain parenthesises.
143 */
144 const std::vector<std::string> arithmetic_expressions =
145 {
146 "5 + 18 / 3",
147 "4 + 3 * 15",
148 "1 - 2 + 52 / 2 - 81 / 3",
149 };
150
151 for (const auto& expr : arithmetic_expressions)
152 {
153 arithmetic_expression = expr;
154 expression.value();
155 }
156}

◆ is_digit_func()

template<typename T >
T is_digit_func ( const unsigned char  c)

Definition at line 48 of file exprtk_arithmetic_evaluator.cpp.

49{
50 return (('0' <= c) && (c <= '9')) ? T(1) : T(0);
51}

◆ is_operator_func()

template<typename T >
T is_operator_func ( const unsigned char  c)

Definition at line 54 of file exprtk_arithmetic_evaluator.cpp.

55{
56 return ('+' == c) || ('-' == c) ||
57 ('*' == c) || ('/' == c) ;
58}

◆ main()

int main ( )

Definition at line 158 of file exprtk_arithmetic_evaluator.cpp.

159{
160 basic_arithmetic_parser_evaluator<double>();
161 return 0;
162}

◆ to_num_func()

template<typename T >
T to_num_func ( const unsigned char  c)

Definition at line 61 of file exprtk_arithmetic_evaluator.cpp.

62{
63 return static_cast<T>(c - '0');
64}