-
Notifications
You must be signed in to change notification settings - Fork 1
/
postfix.cpp
159 lines (137 loc) · 3.65 KB
/
postfix.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Postfix Expressions
// Jeff Ruhl
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <stack>
#include <cctype>
#include <cstdlib>
using namespace std;
typedef map<char, string> DefinitionMap;
typedef stack<float> OperandStack;
struct BinaryOperands
{
float l;
float r;
};
void define( char, const string& );
float eval( const string&, OperandStack* operands = nullptr );
float op_sub( float , float );
float op_div( float , float );
BinaryOperands op_swap( float , float );
BinaryOperands op_dup( float );
DefinitionMap operator_defs;
string get_line_content( const string& line ) {
int start = line.find( '"' );
int end = line.rfind( '"' );
return line.substr( start + 1, end - start - 1 );
}
int main( int argc, char* argv[] ) {
ifstream in("postfix.in");
if ( in ) {
ofstream out("postfix.out");
string line;
while ( getline( in, line ) ) {
switch ( line[0] ) {
case 'D':
define( line[1], get_line_content( line ) );
break;
case 'E':
out << fixed << setprecision( 3 ) << eval( get_line_content( line ) ) << endl;
break;
case 'Q':
goto ALL_DONE;
}
}
ALL_DONE:
out.close();
in.close();
}
else {
cout << "Could not open file 'postfix.in'" << endl;
}
return 0;
}
void define( char operator_name, const string& content ) {
operator_defs[operator_name] = content;
}
float eval( const string& content, OperandStack* operands ) {
bool created_operands_stack = false;
if ( nullptr == operands ) {
operands = new OperandStack();
created_operands_stack = true;
}
auto get_binary_operands = [&operands]() -> BinaryOperands {
BinaryOperands ops;
// pop in reverse order
ops.r = operands->top(); operands->pop();
ops.l = operands->top(); operands->pop();
return ops;
};
string raw_operand;
for ( int i = 0; i < content.length(); i++ ) {
// parse operand, if any
while ( isdigit( content[i] ) ) {
raw_operand += content[i++];
}
if ( !raw_operand.empty() ) {
// set iterator back one, gets incremented one too much during while loop
i--;
operands->push( (float)atoi( raw_operand.c_str() ) );
raw_operand.clear();
}
// otherwise excecute operator
else {
BinaryOperands ops;
BinaryOperands result;
switch ( content[i] ) {
case '-':
ops = get_binary_operands();
operands->push( op_sub( ops.l, ops.r ) );
break;
case '/':
ops = get_binary_operands();
operands->push( op_div( ops.l, ops.r ) );
break;
case '%':
ops = get_binary_operands();
result = op_swap( ops.l, ops.r );
operands->push( result.l ); operands->push( result.r );
break;
case '!':
result = op_dup( operands->top() ); operands->pop();
operands->push( result.l ); operands->push( result.r );
break;
// ignore spaces
case ' ':
break;
// user defined operator
default:
string oper = operator_defs[content[i]];
operands->push( eval( oper, operands ) );
}
}
}
float result = operands->top();
operands->pop();
if ( created_operands_stack ) delete operands;
return result;
}
float op_sub( float l_operand, float r_operand ) {
return l_operand - r_operand;
}
float op_div( float l_operand, float r_operand ) {
if ( 0.0f == r_operand ) return 0.0f;
float result = l_operand / r_operand;
return result;
}
BinaryOperands op_swap( float l_operand, float r_operand ) {
BinaryOperands result = { r_operand, l_operand };
return result;
}
BinaryOperands op_dup( float operand ) {
BinaryOperands result = { operand, operand };
return result;
}