C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_simple_example_24.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* Simple Example 24 *
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
to_num_func
(
const
unsigned
char
c)
55
{
56
return
static_cast<
T
>
(c -
'0'
);
57
}
58
59
template
<
typename
T>
60
void
rpn_example
()
61
{
62
typedef
exprtk::symbol_table<T>
symbol_table_t;
63
typedef
exprtk::expression<T>
expression_t;
64
typedef
exprtk::parser<T>
parser_t;
65
66
const
std::string rpn_program =
67
" var stack[1000] := [0]; "
68
" var top_index := 0; "
69
" "
70
" for (var i := 0; i < rpn_expression[]; i += 1) "
71
" { "
72
" var c := rpn_expression[i : i + 1]; "
73
" "
74
" if (c == ' ') "
75
" { "
76
" continue; "
77
" } "
78
" else if (is_digit(c)) "
79
" { "
80
" stack[top_index] := to_num(c); "
81
" top_index += 1; "
82
" } "
83
" else "
84
" { "
85
" var operator := c; "
86
" var operand1 := stack[top_index - 2]; "
87
" var operand2 := stack[top_index - 1]; "
88
" top_index -= 2; "
89
" "
90
" switch "
91
" { "
92
" case operator == '+' : stack[top_index] := operand1 + operand2; "
93
" case operator == '-' : stack[top_index] := operand1 - operand2; "
94
" case operator == '*' : stack[top_index] := operand1 * operand2; "
95
" case operator == '/' : stack[top_index] := operand1 / operand2; "
96
" case operator == '^' : stack[top_index] := operand1 ^ operand2; "
97
" }; "
98
" "
99
" top_index += 1; "
100
" } "
101
" }; "
102
" "
103
" println(stack[0], ' = ', rpn_expression); "
104
" "
;
105
106
std::string rpn_expression;
107
108
char_process<T,is_digit_func<T>
> isdigit;
109
char_process<T,to_num_func<T>
> tonum;
110
exprtk::rtl::io::println<T>
println;
111
112
symbol_table_t symbol_table;
113
symbol_table.add_stringvar(
"rpn_expression"
, rpn_expression);
114
symbol_table.add_function (
"println"
, println );
115
symbol_table.add_function (
"is_digit"
, isdigit );
116
symbol_table.add_function (
"to_num"
, tonum );
117
118
expression_t expression;
119
expression.register_symbol_table(symbol_table);
120
121
parser_t parser;
122
parser.compile(rpn_program, expression);
123
124
const
std::string rpn_expressions[] =
125
{
126
"2 3 8 / ^ 4 6 * + 3 9 / -"
,
// 2 ^ (3 / 8) + 4 * 6 - 3 / 9
127
"1 2 / 6 5 2 - / * 7 +"
,
// (1 / 2) * (6 / (5 - 2)) + 7
128
"1 2 * 3 / 4 * 5 / 6 *"
,
// ((((1 * 2) / 3) * 4) / 5) * 6
129
"8 6 4 + * 2 /"
// (8 * (6 + 4)) / 2
130
};
131
132
for
(std::size_t i = 0; i <
sizeof
(rpn_expressions) /
sizeof
(std::string); ++i)
133
{
134
rpn_expression = rpn_expressions[i];
135
expression.value();
136
}
137
}
138
139
int
main
()
140
{
141
rpn_example<double>
();
142
return
0;
143
}
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
is_digit_func
T is_digit_func(const unsigned char c)
Definition
exprtk_simple_example_24.cpp:48
rpn_example
void rpn_example()
Definition
exprtk_simple_example_24.cpp:60
to_num_func
T to_num_func(const unsigned char c)
Definition
exprtk_simple_example_24.cpp:54
main
int main()
Definition
exprtk_simple_example_24.cpp:139
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_simple_example_24.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_simple_example_24.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