exprtk_simple_example_03.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. **************************************************************
  3. * C++ Mathematical Expression Toolkit Library *
  4. * *
  5. * Simple Example 3 *
  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 polynomial()
  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 =
  27. "25x^5 - 35x^4 - 15x^3 + 40x^2 - 15x + 1";
  28. const T r0 = T(0);
  29. const T r1 = T(1);
  30. T x = T(0);
  31. symbol_table_t symbol_table;
  32. symbol_table.add_variable("x",x);
  33. expression_t expression;
  34. expression.register_symbol_table(symbol_table);
  35. parser_t parser;
  36. parser.compile(expression_string,expression);
  37. const T delta = T(1.0 / 100.0);
  38. for (x = r0; x <= r1; x += delta)
  39. {
  40. printf("%19.15f\t%19.15f\n", x, expression.value());
  41. }
  42. }
  43. int main()
  44. {
  45. polynomial<double>();
  46. return 0;
  47. }