C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
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-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 to_num_func(const unsigned char c)
55{
56 return static_cast<T>(c - '0');
57}
58
59template <typename T>
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 stack_size := 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[stack_size] := to_num(c); "
81 " stack_size += 1; "
82 " } "
83 " else "
84 " { "
85 " var operator := c; "
86 " var operand1 := stack[stack_size - 2]; "
87 " var operand2 := stack[stack_size - 1]; "
88 " stack_size -= 2; "
89 " "
90 " switch "
91 " { "
92 " case operator == '+' : stack[stack_size] := operand1 + operand2; "
93 " case operator == '-' : stack[stack_size] := operand1 - operand2; "
94 " case operator == '*' : stack[stack_size] := operand1 * operand2; "
95 " case operator == '/' : stack[stack_size] := operand1 / operand2; "
96 " case operator == '^' : stack[stack_size] := operand1 ^ operand2; "
97 " }; "
98 " "
99 " stack_size += 1; "
100 " } "
101 " }; "
102 " "
103 " println(stack[0], ' = ', rpn_expression); "
104 " ";
105
106 std::string rpn_expression;
107
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
139int main()
140{
141 rpn_example<double>();
142 return 0;
143}
igeneric_function(const std::string &param_seq="", const return_type rtr_type=e_rtrn_scalar)
Definition exprtk.hpp:19667
T is_digit_func(const unsigned char c)
void rpn_example()
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