C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_tower_of_hanoi.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk Tower Of Hanoi 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
34 std::vector<T> disks = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
35
37
38 symbol_table_t symbol_table;
39 symbol_table.add_vector ("disks", disks);
40 symbol_table.add_package(io_package);
41
42 compositor_t compositor(symbol_table);
43
44 compositor.load_variables(true);
45 compositor.load_vectors (true);
46
47 compositor.add(
48 function_t("move_disk")
49 .vars("disk", "src_rod", "dest_rod")
50 .expression
51 (
52 "println('Move disk', disk ,' from rod[', src_rod ,'] to rod[', dest_rod ,']');"
53 ));
54
55 compositor.add(
56 function_t("tower_of_hanoi")
57 .vars("n", "src", "dest", "aux")
58 .expression
59 (
60 " if (n == 1) "
61 " { "
62 " move_disk(disks[0], src, dest); "
63 " return [true]; "
64 " }; "
65 " "
66 " tower_of_hanoi(n - 1, src, aux, dest); "
67 " "
68 " move_disk(disks[n - 1], src, dest); "
69 " "
70 " tower_of_hanoi(n - 1, aux, dest, src); "
71 " "
72 ));
73
74 const std::string tower_of_hanoi_program =
75 " tower_of_hanoi(disks[], 1, 2, 3); ";
76
77 expression_t expression;
78 expression.register_symbol_table(symbol_table);
79
80 parser_t parser;
81 parser.compile(tower_of_hanoi_program,expression);
82
83 expression.value();
84}
85
86int main()
87{
88 tower_of_hanoi_example<double>();
89 return 0;
90}
91
92
void tower_of_hanoi_example()