C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_recursive_fibonacci.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* ExprTk Recursive Fibonacci Example *
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 <string>
21
22
#include "
exprtk.hpp
"
23
24
25
template
<
typename
T>
26
void
recursive_fibonacci_example
()
27
{
28
typedef
exprtk::symbol_table<T>
symbol_table_t;
29
typedef
exprtk::expression<T>
expression_t;
30
typedef
exprtk::parser<T>
parser_t;
31
typedef
exprtk::function_compositor<T>
compositor_t;
32
typedef
typename
compositor_t::function function_t;
33
34
exprtk::rtl::io::package<T>
io_package;
35
36
symbol_table_t symbol_table;
37
38
symbol_table.add_package(io_package);
39
40
compositor_t compositor(symbol_table);
41
42
compositor.add(
43
function_t(
"fibonacci"
)
44
.var(
"n"
)
45
.expression
46
(
" (n < 2) ? n : fibonacci(n - 1) + fibonacci(n - 2); "
));
47
48
49
const
std::string fibonacci_program =
50
" const var n := 35; "
51
" "
52
" for (var i := 0; i <= n; i += 1) "
53
" { "
54
" var fib_i := fibonacci(i); "
55
" println('fib(',i,') = ', fib_i); "
56
" } "
;
57
58
expression_t expression;
59
expression.register_symbol_table(symbol_table);
60
61
parser_t parser;
62
parser.compile(fibonacci_program,expression);
63
64
exprtk::timer
timer;
65
timer.
start
();
66
67
expression.value();
68
69
timer.
stop
();
70
71
printf(
"Total time: %8.4fsec\n"
,timer.
time
());
72
}
73
74
int
main
()
75
{
76
recursive_fibonacci_example<double>
();
77
return
0;
78
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::function_compositor
Definition
exprtk.hpp:43057
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk::timer
Definition
exprtk.hpp:44056
exprtk::timer::stop
void stop()
Definition
exprtk.hpp:44103
exprtk::timer::time
double time() const
Definition
exprtk.hpp:44125
exprtk::timer::start
void start()
Definition
exprtk.hpp:44097
exprtk.hpp
recursive_fibonacci_example
void recursive_fibonacci_example()
Definition
exprtk_recursive_fibonacci.cpp:26
main
int main()
Definition
exprtk_recursive_fibonacci.cpp:74
exprtk::rtl::io::package
Definition
exprtk.hpp:44296
Generated by
1.17.0