C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_fizzbuzz.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk Fizz-Buzz 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 <cstdio>
21#include <string>
22
23#include "exprtk.hpp"
24
25
26template <typename T>
28{
29 typedef exprtk::symbol_table<T> symbol_table_t;
30 typedef exprtk::expression<T> expression_t;
31 typedef exprtk::parser<T> parser_t;
32
33 const std::string fizzbuzz_program =
34 " var x := 1; "
35 " repeat "
36 " var div3 := ((x % 3) == 0); "
37 " var div5 := ((x % 5) == 0); "
38 " [*] "
39 " { "
40 " case div3 : print('fizz'); "
41 " case div5 : print('buzz'); "
42 " case div3 nor div5 : print(x); "
43 " }; "
44 " print('\n') "
45 " until ((x += 1) > 100); ";
46
48
49 symbol_table_t symbol_table;
50 symbol_table.add_function("print",print);
51
52 expression_t expression;
53 expression.register_symbol_table(symbol_table);
54
55 parser_t parser;
56 parser.compile(fizzbuzz_program,expression);
57
58 expression.value();
59}
60
61int main()
62{
63 fizzbuzz<double>();
64 return 0;
65}
void fizzbuzz()
int main()