C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_simple_example_18.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * Simple Example 18 *
6 * Author: Arash Partow (1999-2024) *
7 * URL: https://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 * https://www.opensource.org/licenses/MIT *
14 * SPDX-License-Identifier: MIT *
15 * *
16 **************************************************************
17*/
18
19
20#include <cstdio>
21#include <string>
22
23#include "exprtk.hpp"
24
25
26template <typename T>
27void file_io()
28{
29 typedef exprtk::symbol_table<T> symbol_table_t;
30 typedef exprtk::expression<T> expression_t;
31 typedef exprtk::parser<T> parser_t;
32
33 const std::string fileio_program =
34 " var file_name := 'file.txt'; "
35 " var stream := null; "
36 " "
37 " if (stream := open(file_name,'w')) "
38 " println('Successfully opened file: ' + file_name); "
39 " else "
40 " { "
41 " println('Failed to open file: ' + file_name); "
42 " return [false]; "
43 " }; "
44 " "
45 " var s := 'Hello world...\n'; "
46 " "
47 " for (var i := 0; i < 10; i += 1) "
48 " { "
49 " write(stream,s); "
50 " }; "
51 " "
52 " if (close(stream)) "
53 " println('Sucessfully closed file: ' + file_name); "
54 " else "
55 " { "
56 " println('Failed to close file: ' + file_name); "
57 " return [false]; "
58 " } ";
59
62
63 symbol_table_t symbol_table;
64 symbol_table.add_function("println",println);
65 symbol_table.add_package (fileio_package );
66
67 expression_t expression;
68 expression.register_symbol_table(symbol_table);
69
70 parser_t parser;
71 parser.compile(fileio_program,expression);
72
73 printf("Result %10.3f\n",expression.value());
74}
75
76int main()
77{
78 file_io<double>();
79 return 0;
80}