C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_arithmetic_evaluator.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * Basic Arithmetic Infix Expression Parser And Evaluator *
6 * Author: Arash Partow (1999-2024) *
7 * URL: https://www.partow.net/programming/exprtk/index.html *
8 * *
9 * Copyright notice: *
10 * Free use of the Mathematical Expression Toolkit Library is *
11 * permitted under the guidelines and in accordance with the *
12 * most current version of the MIT License. *
13 * https://www.opensource.org/licenses/MIT *
14 * SPDX-License-Identifier: MIT *
15 * *
16 **************************************************************
17*/
18
19
20#include <cstdio>
21#include <string>
22
23#include "exprtk.hpp"
24
25
26template <typename T, T Process(const unsigned char)>
28{
33
34 using exprtk::igeneric_function<T>::operator();
35
37 : exprtk::igeneric_function<T>("S")
38 {}
39
40 inline T operator()(parameter_list_t parameters)
41 {
42 const unsigned char c = string_t(parameters[0])[0];
43 return Process(c);
44 }
45};
46
47template <typename T>
48T is_digit_func(const unsigned char c)
49{
50 return (('0' <= c) && (c <= '9')) ? T(1) : T(0);
51}
52
53template <typename T>
54T is_operator_func(const unsigned char c)
55{
56 return ('+' == c) || ('-' == c) ||
57 ('*' == c) || ('/' == c) ;
58}
59
60template <typename T>
61T to_num_func(const unsigned char c)
62{
63 return static_cast<T>(c - '0');
64}
65
66template <typename T>
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}
157
158int main()
159{
160 basic_arithmetic_parser_evaluator<double>();
161 return 0;
162}
igeneric_function(const std::string &param_seq="", const return_type rtr_type=e_rtrn_scalar)
Definition exprtk.hpp:19906
void basic_arithmetic_parser_evaluator()
T is_operator_func(const unsigned char c)
T is_digit_func(const unsigned char c)
T to_num_func(const unsigned char c)
igfun_t::parameter_list_t parameter_list_t
generic_type::string_view string_t
T operator()(parameter_list_t parameters)
exprtk::igeneric_function< T > igfun_t
igfun_t::generic_type generic_type