MCPcopy Create free account
hub / github.com/coin-or/CppAD / StackMachine

Function StackMachine

example/general/stack_machine.cpp:48–118  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

46}
47
48void StackMachine(
49 std::stack< std::string > &token_stack ,
50 CppAD::vector< CppAD::AD<double> > &variable )
51{ using std::string;
52 using std::stack;
53
54 using CppAD::AD;
55
56 stack< AD<double> > value_stack;
57 string token;
58 AD<double> value_one;
59 AD<double> value_two;
60
61 while( ! token_stack.empty() )
62 { string s = token_stack.top();
63 token_stack.pop();
64
65 if( is_number(s) )
66 { value_one = std::atof( s.c_str() );
67 value_stack.push( value_one );
68 }
69 else if( is_variable(s) )
70 { value_one = variable[ size_t(s[0]) - size_t('a') ];
71 value_stack.push( value_one );
72 }
73 else if( is_binary(s) )
74 { assert( value_stack.size() >= 2 );
75 value_one = value_stack.top();
76 value_stack.pop();
77 value_two = value_stack.top();
78 value_stack.pop();
79
80 switch( s[0] )
81 {
82 case '+':
83 value_stack.push(value_one + value_two);
84 break;
85
86 case '-':
87 value_stack.push(value_one - value_two);
88 break;
89
90 case '*':
91 value_stack.push(value_one * value_two);
92 break;
93
94 case '/':
95 value_stack.push(value_one / value_two);
96 break;
97
98 default:
99 assert(0);
100 }
101 }
102 else if( s[0] == '=' )
103 { assert( value_stack.size() >= 1 );
104 assert( token_stack.size() >= 1 );
105 //

Callers

nothing calls this directly

Calls 9

is_numberFunction · 0.85
is_variableFunction · 0.85
is_binaryFunction · 0.85
epsilonFunction · 0.85
ForwardMethod · 0.80
JacobianMethod · 0.80
IndependentFunction · 0.70
NearEqualFunction · 0.70
sizeMethod · 0.45

Tested by

no test coverage detected