exprtk_simple_example_12.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. **************************************************************
  3. * C++ Mathematical Expression Toolkit Library *
  4. * *
  5. * Simple Example 12 *
  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 bubble_sort()
  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 bubblesort_program =
  27. " var upper_bound := v[]; "
  28. " var swapped := false; "
  29. " repeat "
  30. " swapped := false; "
  31. " for (var i := 0; i < upper_bound; i += 1) "
  32. " { "
  33. " for (var j := i + 1; j < upper_bound; j += 1) "
  34. " { "
  35. " if (v[i] > v[j]) "
  36. " { "
  37. " v[i] <=> v[j]; "
  38. " swapped := true; "
  39. " }; "
  40. " }; "
  41. " }; "
  42. " upper_bound -= 1; "
  43. " until (not(swapped) or (upper_bound == 0)); ";
  44. T v[] = { T(9.9), T(2.2), T(1.1), T(5.5), T(7.7), T(4.4), T(3.3) };
  45. symbol_table_t symbol_table;
  46. symbol_table.add_vector("v",v);
  47. expression_t expression;
  48. expression.register_symbol_table(symbol_table);
  49. parser_t parser;
  50. parser.compile(bubblesort_program,expression);
  51. expression.value();
  52. }
  53. int main()
  54. {
  55. bubble_sort<double>();
  56. return 0;
  57. }