-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.cpp
More file actions
71 lines (60 loc) · 1.7 KB
/
example.cpp
File metadata and controls
71 lines (60 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "MiniParser.hpp"
#include <iostream>
#include <cstdlib>
class myfunc
{
public:
myfunc() : mp( "x + (y**2)" ) {}
double operator()( double x, double y )
{
MiniParser::IDMap ids ;
ids[ "x" ] = x ;
ids[ "y" ] = y ;
return mp.eval( ids ) ;
}
private:
MiniParser mp ;
};
int main( int argc, char* argv[] )
{
if( argc == 3 )
{
myfunc func ;
double x = atof( argv[1] ) ;
double y = atof( argv[2] ) ;
std::cout << "f( " << x << ", " << y << " ): " << func( x, y ) << std::endl ;
}
else if( argc > 3 )
{
std::cout << "Parsing: " << argv[1] << std::endl ;
MiniParser mp( argv[1] ) ;
MiniParser::IDMap ids ;
char var[] = "a" ;
for( int i = 2 ; i < argc ; ++i )
{
var[0] = 'a' + i-2 ;
ids[ var ] = atof( argv[i] ) ;
}
std::cout << "eval( " ;
for(
MiniParser::IDMap::const_iterator idit = ids.begin() ;
idit != ids.end() ;
++idit
)
{
if( idit != ids.begin() )
std::cout << ", " ;
std::cout << idit->first << " = " << idit->second ;
}
std::cout << " ): " << mp.eval( ids ) << std::endl ;
}
else
{
std::cerr << "Usage: " << argv[0] << " x y" << std::endl ;
std::cerr << "\tor" << std::endl ;
std::cerr << "Usage: " << argv[0] << " expression [a [b [c [...]]]]" << std::endl ;
std::cerr << "(Remember: order-of-operations is left-associative! use parentheses!)" << std::endl ;
exit( -1 ) ;
}
exit( 0 ) ;
}