C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
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-2025) *
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
26
template
<
typename
T, T Process(const
unsigned
char
)>
27
struct
char_process
:
public
exprtk::igeneric_function
<T>
28
{
29
typedef
typename
exprtk::igeneric_function<T>
igfun_t
;
30
typedef
typename
igfun_t::parameter_list_t
parameter_list_t
;
31
typedef
typename
igfun_t::generic_type
generic_type
;
32
typedef
typename
generic_type::string_view
string_t
;
33
34
using
exprtk::igeneric_function
<T>::operator();
35
36
char_process
()
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
47
template
<
typename
T>
48
T
is_digit_func
(
const
unsigned
char
c)
49
{
50
return
((
'0'
<= c) && (c <=
'9'
)) ? T(1) : T(0);
51
}
52
53
template
<
typename
T>
54
T
is_operator_func
(
const
unsigned
char
c)
55
{
56
return
(
'+'
== c) || (
'-'
== c) ||
57
(
'*'
== c) || (
'/'
== c) ;
58
}
59
60
template
<
typename
T>
61
T
to_num_func
(
const
unsigned
char
c)
62
{
63
return
static_cast<
T
>
(c -
'0'
);
64
}
65
66
template
<
typename
T>
67
void
basic_arithmetic_parser_evaluator
()
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
121
char_process<T,is_digit_func<T>
> isdigit;
122
char_process<T,to_num_func<T>
> tonum;
123
char_process<T,is_operator_func<T>
> isopr;
124
exprtk::rtl::io::println<T>
println;
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
158
int
main
()
159
{
160
basic_arithmetic_parser_evaluator<double>
();
161
return
0;
162
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::igeneric_function
Definition
exprtk.hpp:19972
exprtk::igeneric_function::parameter_list_t
generic_type::parameter_list parameter_list_t
Definition
exprtk.hpp:19984
exprtk::igeneric_function::igeneric_function
igeneric_function(const std::string ¶m_seq="", const return_type rtr_type=e_rtrn_scalar)
Definition
exprtk.hpp:19986
exprtk::igeneric_function::generic_type
type_store< T > generic_type
Definition
exprtk.hpp:19983
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
basic_arithmetic_parser_evaluator
void basic_arithmetic_parser_evaluator()
Definition
exprtk_arithmetic_evaluator.cpp:67
is_operator_func
T is_operator_func(const unsigned char c)
Definition
exprtk_arithmetic_evaluator.cpp:54
is_digit_func
T is_digit_func(const unsigned char c)
Definition
exprtk_arithmetic_evaluator.cpp:48
to_num_func
T to_num_func(const unsigned char c)
Definition
exprtk_arithmetic_evaluator.cpp:61
main
int main()
Definition
exprtk_arithmetic_evaluator.cpp:158
exprtk
Definition
exprtk.hpp:60
char_process
Definition
exprtk_arithmetic_evaluator.cpp:28
char_process::parameter_list_t
igfun_t::parameter_list_t parameter_list_t
Definition
exprtk_arithmetic_evaluator.cpp:30
char_process::char_process
char_process()
Definition
exprtk_arithmetic_evaluator.cpp:36
char_process::string_t
generic_type::string_view string_t
Definition
exprtk_arithmetic_evaluator.cpp:32
char_process::operator()
T operator()(parameter_list_t parameters)
Definition
exprtk_arithmetic_evaluator.cpp:40
char_process::igfun_t
exprtk::igeneric_function< T > igfun_t
Definition
exprtk_arithmetic_evaluator.cpp:29
char_process::generic_type
igfun_t::generic_type generic_type
Definition
exprtk_arithmetic_evaluator.cpp:31
exprtk::rtl::io::println
Definition
exprtk.hpp:44273
exprtk::type_store::string_view
type_view< char > string_view
Definition
exprtk.hpp:4953
Generated by
1.17.0