exprtk_simple_example_09.cpp 5.5 KB

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