C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_calc.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* ExprTk Simple Calculator Example *
6
* Author: Arash Partow (1999-2025) *
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 <iostream>
22
#include <string>
23
24
#include "
exprtk.hpp
"
25
26
27
int
main
()
28
{
29
typedef
exprtk::symbol_table<double>
symbol_table_t;
30
typedef
exprtk::expression<double>
expression_t;
31
typedef
exprtk::parser<double>
parser_t;
32
typedef
exprtk::parser_error::type
err_t;
33
34
for
( ; ; )
35
{
36
double
x = 1.0;
37
double
y = 2.0;
38
double
z = 3.0;
39
double
w = 4.0;
40
double
u = 5.0;
41
double
v = 6.0;
42
43
symbol_table_t symbol_table;
44
symbol_table.add_variable(
"x"
,x);
45
symbol_table.add_variable(
"y"
,y);
46
symbol_table.add_variable(
"z"
,z);
47
symbol_table.add_variable(
"w"
,w);
48
symbol_table.add_variable(
"u"
,u);
49
symbol_table.add_variable(
"v"
,v);
50
symbol_table.add_constants();
51
52
expression_t expression;
53
expression.register_symbol_table(symbol_table);
54
55
std::string expression_str;
56
57
printf(
">> "
);
58
std::getline(std::cin,expression_str);
59
60
if
(expression_str.empty())
61
continue
;
62
else
if
(
"exit"
== expression_str)
63
break
;
64
else
if
(
"quit"
== expression_str)
65
break
;
66
67
parser_t parser;
68
69
if
(!parser.compile(expression_str,expression))
70
{
71
printf(
"Error: %s\tExpression: %s\n"
,
72
parser.error().c_str(),
73
expression_str.c_str());
74
75
for
(std::size_t i = 0; i < parser.error_count(); ++i)
76
{
77
err_t error = parser.get_error(i);
78
printf(
"Error: %02d Pos: %02d Type: [%14s] Message: %s\tExpression: %s\n"
,
79
static_cast<
unsigned
int
>
(i),
80
static_cast<
unsigned
int
>
(error.token.position),
81
exprtk::parser_error::to_str
(error.mode).c_str(),
82
error.diagnostic.c_str(),
83
expression_str.c_str());
84
}
85
86
continue
;
87
}
88
89
double
result = expression.value();
90
91
printf(
"result: %20.10f\n"
,result);
92
}
93
94
return
0;
95
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
main
int main()
Definition
exprtk_calc.cpp:27
exprtk::parser_error::to_str
std::string to_str(error_mode mode)
Definition
exprtk.hpp:22447
exprtk::parser_error::type
Definition
exprtk.hpp:22404
Generated by
1.17.0