2014-04-21 10:08:53 +00:00
|
|
|
/*
|
|
|
|
**************************************************************
|
|
|
|
* C++ Mathematical Expression Toolkit Library *
|
|
|
|
* *
|
|
|
|
* Simple Example 12 *
|
2024-01-01 00:00:00 +00:00
|
|
|
* Author: Arash Partow (1999-2024) *
|
2023-01-01 02:18:35 +00:00
|
|
|
* URL: https://www.partow.net/programming/exprtk/index.html *
|
2014-04-21 10:08:53 +00:00
|
|
|
* *
|
|
|
|
* Copyright notice: *
|
|
|
|
* Free use of the Mathematical Expression Toolkit Library is *
|
|
|
|
* permitted under the guidelines and in accordance with the *
|
2017-02-21 21:44:24 +00:00
|
|
|
* most current version of the MIT License. *
|
2023-01-01 02:18:35 +00:00
|
|
|
* https://www.opensource.org/licenses/MIT *
|
2024-01-01 00:00:00 +00:00
|
|
|
* SPDX-License-Identifier: MIT *
|
2014-04-21 10:08:53 +00:00
|
|
|
* *
|
|
|
|
**************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <string>
|
2020-01-01 00:00:00 +00:00
|
|
|
|
2014-04-21 10:08:53 +00:00
|
|
|
#include "exprtk.hpp"
|
|
|
|
|
|
|
|
|
2014-07-15 13:09:08 +00:00
|
|
|
template <typename T>
|
2014-04-21 10:08:53 +00:00
|
|
|
void bubble_sort()
|
|
|
|
{
|
|
|
|
typedef exprtk::symbol_table<T> symbol_table_t;
|
2021-01-01 00:00:00 +00:00
|
|
|
typedef exprtk::expression<T> expression_t;
|
|
|
|
typedef exprtk::parser<T> parser_t;
|
2014-04-21 10:08:53 +00:00
|
|
|
|
2020-01-01 00:00:00 +00:00
|
|
|
const std::string bubblesort_program =
|
2024-01-01 00:00:00 +00:00
|
|
|
" var upper_bound := v[]; "
|
|
|
|
" "
|
|
|
|
" repeat "
|
|
|
|
" var new_upper_bound := 0; "
|
|
|
|
" "
|
|
|
|
" for (var i := 1; i < upper_bound; i += 1) "
|
|
|
|
" { "
|
|
|
|
" if (v[i - 1] > v[i]) "
|
|
|
|
" { "
|
|
|
|
" v[i - 1] <=> v[i]; "
|
|
|
|
" new_upper_bound := i; "
|
|
|
|
" }; "
|
|
|
|
" }; "
|
|
|
|
" "
|
|
|
|
" upper_bound := new_upper_bound; "
|
|
|
|
" "
|
|
|
|
" until (upper_bound <= 1); ";
|
2014-04-21 10:08:53 +00:00
|
|
|
|
2024-01-01 00:00:00 +00:00
|
|
|
T v[] = { T(9.1), T(2.2), T(1.3), T(5.4), T(7.5), T(4.6), T(3.7) };
|
2014-04-21 10:08:53 +00:00
|
|
|
|
|
|
|
symbol_table_t symbol_table;
|
|
|
|
symbol_table.add_vector("v",v);
|
|
|
|
|
|
|
|
expression_t expression;
|
|
|
|
expression.register_symbol_table(symbol_table);
|
|
|
|
|
|
|
|
parser_t parser;
|
2014-05-27 21:10:40 +00:00
|
|
|
parser.compile(bubblesort_program,expression);
|
2014-04-21 10:08:53 +00:00
|
|
|
|
|
|
|
expression.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
bubble_sort<double>();
|
|
|
|
return 0;
|
|
|
|
}
|