exprtk_simple_example_07.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. **************************************************************
  3. * C++ Mathematical Expression Toolkit Library *
  4. * *
  5. * Simple Example 7 *
  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 logic()
  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. const std::string expression_string = "not(A and B) or C";
  27. symbol_table_t symbol_table;
  28. symbol_table.create_variable("A");
  29. symbol_table.create_variable("B");
  30. symbol_table.create_variable("C");
  31. expression_t expression;
  32. expression.register_symbol_table(symbol_table);
  33. parser_t parser;
  34. parser.compile(expression_string,expression);
  35. printf(" # | A | B | C | %s\n"
  36. "---+---+---+---+-%s\n",
  37. expression_string.c_str(),
  38. std::string(expression_string.size(),'-').c_str());
  39. for (int i = 0; i < 8; ++i)
  40. {
  41. symbol_table.get_variable("A")->ref() = T((i & 0x01) ? 1 : 0);
  42. symbol_table.get_variable("B")->ref() = T((i & 0x02) ? 1 : 0);
  43. symbol_table.get_variable("C")->ref() = T((i & 0x04) ? 1 : 0);
  44. const int result = static_cast<int>(expression.value());
  45. printf(" %d | %d | %d | %d | %d \n",
  46. i,
  47. static_cast<int>(symbol_table.get_variable("A")->value()),
  48. static_cast<int>(symbol_table.get_variable("B")->value()),
  49. static_cast<int>(symbol_table.get_variable("C")->value()),
  50. result);
  51. }
  52. }
  53. int main()
  54. {
  55. logic<double>();
  56. return 0;
  57. }