C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_simple_example_07.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * Simple Example 07 *
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>
27void logic()
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 expression_string = "not(A and B) or C";
34
35 symbol_table_t symbol_table;
36 symbol_table.create_variable("A");
37 symbol_table.create_variable("B");
38 symbol_table.create_variable("C");
39
40 expression_t expression;
41 expression.register_symbol_table(symbol_table);
42
43 parser_t parser;
44 parser.compile(expression_string,expression);
45
46 printf(" # | A | B | C | %s\n"
47 "---+---+---+---+-%s\n",
48 expression_string.c_str(),
49 std::string(expression_string.size(),'-').c_str());
50
51 for (int i = 0; i < 8; ++i)
52 {
53 symbol_table.get_variable("A")->ref() = T((i & 0x01) ? 1 : 0);
54 symbol_table.get_variable("B")->ref() = T((i & 0x02) ? 1 : 0);
55 symbol_table.get_variable("C")->ref() = T((i & 0x04) ? 1 : 0);
56
57 const int result = static_cast<int>(expression.value());
58
59 printf(" %d | %d | %d | %d | %d \n",
60 i,
61 static_cast<int>(symbol_table.get_variable("A")->value()),
62 static_cast<int>(symbol_table.get_variable("B")->value()),
63 static_cast<int>(symbol_table.get_variable("C")->value()),
64 result);
65 }
66}
67
68int main()
69{
70 logic<double>();
71 return 0;
72}
bool compile(const std::string &expression_string, expression< T > &expr)
Definition exprtk.hpp:24443