2013-04-14 07:01:14 +00:00
|
|
|
/*
|
|
|
|
**************************************************************
|
|
|
|
* C++ Mathematical Expression Toolkit Library *
|
|
|
|
* *
|
|
|
|
* Simple Example 4 *
|
2017-02-06 07:03:16 +00:00
|
|
|
* Author: Arash Partow (1999-2017) *
|
2013-04-14 07:01:14 +00:00
|
|
|
* URL: http://www.partow.net/programming/exprtk/index.html *
|
|
|
|
* *
|
|
|
|
* Copyright notice: *
|
|
|
|
* Free use of the Mathematical Expression Toolkit Library is *
|
|
|
|
* permitted under the guidelines and in accordance with the *
|
|
|
|
* most current version of the Common Public License. *
|
|
|
|
* http://www.opensource.org/licenses/cpl1.0.php *
|
|
|
|
* *
|
|
|
|
**************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
|
|
|
#include "exprtk.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void fibonacci()
|
|
|
|
{
|
|
|
|
typedef exprtk::symbol_table<T> symbol_table_t;
|
|
|
|
typedef exprtk::expression<T> expression_t;
|
|
|
|
typedef exprtk::parser<T> parser_t;
|
|
|
|
typedef exprtk::function_compositor<T> compositor_t;
|
2015-04-28 06:50:54 +00:00
|
|
|
typedef typename compositor_t::function function_t;
|
2013-04-14 07:01:14 +00:00
|
|
|
|
|
|
|
compositor_t compositor;
|
|
|
|
|
|
|
|
compositor
|
2015-04-28 06:50:54 +00:00
|
|
|
.add(
|
2016-10-02 09:23:18 +00:00
|
|
|
function_t( // define function: fibonacci(x)
|
2015-04-28 06:50:54 +00:00
|
|
|
"fibonacci",
|
2014-05-30 21:38:57 +00:00
|
|
|
" var w := 0; "
|
|
|
|
" var y := 0; "
|
|
|
|
" var z := 1; "
|
|
|
|
" switch "
|
|
|
|
" { "
|
|
|
|
" case x == 0 : 0; "
|
|
|
|
" case x == 1 : 1; "
|
|
|
|
" default : "
|
2014-04-19 11:10:15 +00:00
|
|
|
" while ((x -= 1) > 0) "
|
|
|
|
" { "
|
|
|
|
" w := z; "
|
|
|
|
" z := z + y; "
|
|
|
|
" y := w; "
|
|
|
|
" z "
|
|
|
|
" }; "
|
2014-05-30 21:38:57 +00:00
|
|
|
" } ",
|
2015-04-28 06:50:54 +00:00
|
|
|
"x"));
|
2013-04-14 07:01:14 +00:00
|
|
|
|
|
|
|
T x = T(0);
|
|
|
|
|
|
|
|
symbol_table_t& symbol_table = compositor.symbol_table();
|
|
|
|
symbol_table.add_constants();
|
|
|
|
symbol_table.add_variable("x",x);
|
|
|
|
|
|
|
|
std::string expression_str = "fibonacci(x)";
|
|
|
|
|
|
|
|
expression_t expression;
|
|
|
|
expression.register_symbol_table(symbol_table);
|
|
|
|
|
|
|
|
parser_t parser;
|
|
|
|
parser.compile(expression_str,expression);
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < 40; ++i)
|
|
|
|
{
|
|
|
|
x = i;
|
2014-11-03 10:36:41 +00:00
|
|
|
|
2013-04-14 07:01:14 +00:00
|
|
|
T result = expression.value();
|
2014-11-03 10:36:41 +00:00
|
|
|
|
2013-04-14 07:01:14 +00:00
|
|
|
printf("fibonacci(%3d) = %10.0f\n",
|
2014-10-27 10:57:30 +00:00
|
|
|
static_cast<int>(i),
|
2013-04-14 07:01:14 +00:00
|
|
|
result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
fibonacci<double>();
|
|
|
|
return 0;
|
|
|
|
}
|