OpcUaCanOpen
CANopen OPC-UA server
exprtk_simple_example_09.cpp
Go to the documentation of this file.
1 /*
2  **************************************************************
3  * C++ Mathematical Expression Toolkit Library *
4  * *
5  * Simple Example 9 *
6  * Author: Arash Partow (1999-2019) *
7  * URL: http://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  * http://www.opensource.org/licenses/MIT *
14  * *
15  **************************************************************
16 */
17 
18 
19 #include <cstdio>
20 #include <string>
21 
22 #include "exprtk.hpp"
23 
24 
25 template <typename T>
26 void primes()
27 {
31  typedef exprtk::function_compositor<T> compositor_t;
32  typedef typename compositor_t::function function_t;
33 
34  T x = T(0);
35 
36  symbol_table_t symbol_table;
37 
38  symbol_table.add_constants();
39  symbol_table.add_variable("x",x);
40 
41  compositor_t compositor(symbol_table);
42 
43  //Mode 1 - if statement based
44  compositor
45  .add(
46  function_t( // define function: is_prime_impl1(x,y)
47  "is_prime_impl1",
48  " if (y == 1,true, "
49  " if (0 == (x % y),false, "
50  " is_prime_impl1(x,y - 1))) ",
51  "x","y"));
52 
53  compositor
54  .add(
55  function_t( // define function: is_prime1(x)
56  "is_prime1",
57  " if (frac(x) != 0, false, "
58  " if (x <= 0, false, "
59  " is_prime_impl1(x,min(x - 1,trunc(sqrt(x)) + 1)))) ",
60  "x"));
61 
62  //Mode 2 - switch statement based
63  compositor
64  .add(
65  function_t( // define function: is_prime_impl2(x,y)
66  "is_prime_impl2",
67  " switch "
68  " { "
69  " case y == 1 : true; "
70  " case (x % y) == 0 : false; "
71  " default : is_prime_impl2(x,y - 1); "
72  " } ",
73  "x","y"));
74 
75  compositor
76  .add(
77  function_t( // define function: is_prime2(x)
78  "is_prime2",
79  " switch "
80  " { "
81  " case x <= 0 : false; "
82  " case frac(x) != 0 : false; "
83  " default : is_prime_impl2(x,min(x - 1,trunc(sqrt(x)) + 1)); "
84  " } ",
85  "x"));
86 
87  //Mode 3 - switch statement and while-loop based
88  compositor
89  .add(
90  function_t( // define function: is_prime_impl3(x,y)
91  "is_prime_impl3",
92  " while (y > 0) "
93  " { "
94  " switch "
95  " { "
96  " case y == 1 : ~(y := 0,true); "
97  " case (x % y) == 0 : ~(y := 0,false); "
98  " default : y := y - 1; "
99  " } "
100  " } ",
101  "x","y"));
102 
103  compositor
104  .add(
105  function_t( // define function: is_prime3(x)
106  "is_prime3",
107  " switch "
108  " { "
109  " case x <= 0 : false; "
110  " case frac(x) != 0 : false; "
111  " default : is_prime_impl3(x,min(x - 1,trunc(sqrt(x)) + 1)); "
112  " } ",
113  "x"));
114 
115  std::string expression_str1 = "is_prime1(x)";
116  std::string expression_str2 = "is_prime2(x)";
117  std::string expression_str3 = "is_prime3(x)";
118 
119  expression_t expression1;
120  expression_t expression2;
121  expression_t expression3;
122  expression1.register_symbol_table(symbol_table);
123  expression2.register_symbol_table(symbol_table);
124  expression3.register_symbol_table(symbol_table);
125 
127 
128  parser.compile(expression_str1,expression1);
129  parser.compile(expression_str2,expression2);
130  parser.compile(expression_str3,expression3);
131 
132  for (std::size_t i = 0; i < 100; ++i)
133  {
134  x = static_cast<T>(i);
135 
136  T result1 = expression1.value();
137  T result2 = expression2.value();
138  T result3 = expression3.value();
139 
140  printf("%03d Result1: %c Result2: %c Result3: %c\n",
141  static_cast<unsigned int>(i),
142  (result1 == T(1)) ? 'T' : 'F',
143  (result2 == T(1)) ? 'T' : 'F',
144  (result3 == T(1)) ? 'T' : 'F');
145  }
146 }
147 
148 int main()
149 {
150  primes<double>();
151  return 0;
152 }