C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
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-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 <string>
21
22#include "exprtk.hpp"
23
24
25template <typename T>
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
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
74int main()
75{
76 recursive_fibonacci_example<double>();
77 return 0;
78}
double time() const
Definition exprtk.hpp:43502
void recursive_fibonacci_example()