-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRPNCalcStart.cpp
394 lines (336 loc) · 10.9 KB
/
RPNCalcStart.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "RPNCalc.h"
//-------------------------------------------------------------------------------------------
// Class: CRPNCalc
//
// File: RPNCalc.cpp
//
// Description: This file contains the function definitions for CRPNCalc
//
// Programmer:
//
// Date:
//
// Version: 1.0
//
// Environment: Intel Xeon PC
// Software: MS Windows 7 for execution;
// Compiles under Microsoft Visual C++.Net 2010
//
// class CRPNCalc:
//
// Properties:
// double m_registers[10];
// stack<string> m_stack;
// vector<list<string>> m_programs(NUMPROGRAMS);
// istringstream m_inStrStream;
//
//
// Non-inline Methods:
//
// CRPNCalc(bool on)
// run()
// print(ostream& ostr)
// parse()
// void add() --
// void bin_prep(double& d1, double& d2) --
// void clear() --
// void clearAll() --
// void divide() --
// void exp() --
// void getReg(int reg) --
// void loadProgram() --
// void mod() --
// void multiply() --
// void neg() --
// void parse() --
// void recordProgram() --
// void rotateUp() --
// void rotateDown() --
// void runProgram() --
// void saveToFile() --
// void setReg(int reg) --
// void subtract() --
// void unary_prep(double& d) --
// related functions:
// ostream &operator <<(ostream &ostr, const CRPNCalc &calc)
// istream &operator >>(istream &istr, CRPNCalc &calc)
//
// History Log:
//
// ----------------------------------------------------------------------------
namespace PB_CALC
{
// ----------------------------------------------------------------------------
// constructor
// ----------------------------------------------------------------------------
CRPNCalc::CRPNCalc(bool on) : m_on(on), m_error(false), m_helpOn(true),
m_programRunning(false)
{
for (int i = 0; i < NUMREGS; i++)
m_registers[i] = 0.0;
if (m_on)
run();
}
// ----------------------------------------------------------------------------
// starts the calculator running
// ----------------------------------------------------------------------------
void CRPNCalc::run()
{
if (!m_on)
EXIT_SUCCESS;
}
// ----------------------------------------------------------------------------
// prints out calculator screen
// ----------------------------------------------------------------------------
void CRPNCalc::print(ostream& ostr)
{
double d = 0.0;
ostr << "[RPN Programmable Calculator] by Paul Bladek" << endl;
if (m_helpOn)
cout << helpMenu;
else
cout << endl << endl << endl;
cout << line;
if (!m_stack.empty())
{
d = m_stack.front();
ostr << d;
}
ostr << endl << endl;
if (m_error)
{
ostr << "<<error>>" << endl;
m_error = false;
}
}
// ----------------------------------------------------------------------------
// parses the next command from m_instrStream
// ----------------------------------------------------------------------------
void CRPNCalc::parse()
{
}
// ----------------------------------------------------------------------------
// if possible, pops top 2 elements from the stack, adds them
// and pushes the result onto the stack
// ----------------------------------------------------------------------------
void CRPNCalc::add()
{
double a = m_stack.front();
m_stack.pop_front();
double b = m_stack.front();
m_stack.pop_front();
binary_prep(a, b);
m_stack.push_front(a + b);
}
// ----------------------------------------------------------------------------
// sets the args to the popped values from the stack, if possible
// set error state otherwise
// ----------------------------------------------------------------------------
void CRPNCalc::binary_prep(double& d1, double& d2)
{
}
// ----------------------------------------------------------------------------
// removes the top element from the stack
// ----------------------------------------------------------------------------
void CRPNCalc::clearEntry()
{
m_stack.pop_front();
}
// ----------------------------------------------------------------------------
// empties the stack
// ----------------------------------------------------------------------------
void CRPNCalc::clearAll()
{
while (!m_stack.empty())
m_stack.pop_front();
}
// ----------------------------------------------------------------------------
// if possible, pops top 2 elements from the stack, divides them
// and pushes the result onto the stack
// ----------------------------------------------------------------------------
void CRPNCalc::divide()
{
double a = m_stack.front();
m_stack.pop_front();
double b = m_stack.front();
m_stack.pop_front();
m_stack.push_front(a / b);
}
// ----------------------------------------------------------------------------
// if possible, pops top 2 elements from the stack,
// raises one element to the other power
// and pushes the result onto the stack
// ----------------------------------------------------------------------------
void CRPNCalc::exp()
{
double a = m_stack.front();
m_stack.pop_front();
double b = m_stack.front();
m_stack.pop_front();
m_stack.push_front(pow(a, b));
}
// ----------------------------------------------------------------------------
// pushes the given register's value onto the stack
// ----------------------------------------------------------------------------
void CRPNCalc::getReg(int reg)
{
m_stack.push_back(m_registers[reg]);
}
// ----------------------------------------------------------------------------
// retrieves the filename from the user and loads it into m_program
// ----------------------------------------------------------------------------
void CRPNCalc::loadProgram()
{
string line;
cout << "Enter the name of the file : ";
getline(cin, line);
ifstream myfile(line + ".prog");
if (myfile.is_open())
{
while (getline(myfile, line))
{
m_program.push_front(line);
}
myfile.close();
}
else
cout << "File cannot be Opened";
}
// ----------------------------------------------------------------------------
// if possible, pops top 2 elements from the stack, mods them
// and pushes the result onto the stack
// ----------------------------------------------------------------------------
void CRPNCalc::mod()
{
double a = m_stack.front();
m_stack.pop_front();
double b = m_stack.front();
m_stack.pop_front();
m_stack.push_front(fmod(a, b));
}
// ----------------------------------------------------------------------------
// if possible, pops top 2 elements from the stack, multiplies them
// and pushes the result onto the stack
// ----------------------------------------------------------------------------
void CRPNCalc::multiply()
{
double a = m_stack.front();
m_stack.pop_front();
double b = m_stack.front();
m_stack.pop_front();
m_stack.push_front(a * b);
}
// ----------------------------------------------------------------------------
// resets the top element of the stack to it's negative
// ----------------------------------------------------------------------------
void CRPNCalc::neg()
{
double elem = m_stack.front();
m_stack.pop_front();
m_stack.push_front(elem * -1);
}
// ----------------------------------------------------------------------------
// sets the arg to the popped value from the stack, if possible
// sets error state otherwise
// ----------------------------------------------------------------------------
void CRPNCalc::unary_prep(double& d)
{
}
// ----------------------------------------------------------------------------
// takes command-line input and loads it into m_program
// ----------------------------------------------------------------------------
void CRPNCalc::recordProgram()
{
char c;
int i = 0;
while (c != 'p')
{
cout << i << "> ";
cin.get(c);
cout << endl;
string s(1,c);
m_program.push_front(s);
i++;
}
}
// ----------------------------------------------------------------------------
// removes the bottom of the stack and adds it to the top
// ----------------------------------------------------------------------------
void CRPNCalc::rotateDown()
{
double temp = m_stack.back();
m_stack.pop_back();
m_stack.push_front(temp);
}
// ----------------------------------------------------------------------------
// removes the top of the stack and adds it to the bottom
// ----------------------------------------------------------------------------
void CRPNCalc::rotateUp()
{
double temp = m_stack.front();
m_stack.pop_front();
m_stack.push_front(temp);
}
// ----------------------------------------------------------------------------
// runs the program in m_program
// ----------------------------------------------------------------------------
void CRPNCalc::runProgram()
{
}
// ----------------------------------------------------------------------------
// asks the user for a filename and saves m_program to that file
// ----------------------------------------------------------------------------
void CRPNCalc::saveToFile()
{
string fileName = " ";
cout << "Enter the name of the file : ";
getline(cin, fileName);
ofstream myfile;
myfile.open(fileName+".prog");
for (int i = 0; i < m_program.size(); i++)
myfile << m_program.front();
}
// ----------------------------------------------------------------------------
// gets the value from the top of the stack
// and places it into the given register
// ----------------------------------------------------------------------------
void CRPNCalc::setReg(int reg)
{
m_registers[reg] = m_stack.front();
m_stack.pop_front();
}
// ----------------------------------------------------------------------------
// if possible, pops top 2 elements from the stack, subtracts them
// and pushes the result onto the stack
// ----------------------------------------------------------------------------
void CRPNCalc::subtract()
{
double a = m_stack.front();
m_stack.pop_front();
double b = m_stack.front();
m_stack.pop_front();
m_stack.push_front(b - a);
}
// ----------------------------------------------------------------------------
// inputs a line from the given stream
// ----------------------------------------------------------------------------
void CRPNCalc::input(istream &istr)
{
}
// ----------------------------------------------------------------------------
// ostream's << defined for CRPNCalc
// ----------------------------------------------------------------------------
ostream &operator <<(ostream &ostr, CRPNCalc &calc)
{
calc.print(ostr);
return ostr;
}
// ----------------------------------------------------------------------------
// istream's >> defined for CRPNCalc
// ----------------------------------------------------------------------------
istream &operator >>(istream &istr, CRPNCalc &calc)
{
calc.input(istr);
return istr;
}
} // end namespace PB_CALC