This repository was archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.cpp
More file actions
380 lines (290 loc) · 12.4 KB
/
Copy pathdriver.cpp
File metadata and controls
380 lines (290 loc) · 12.4 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
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
/*--------------------------------------------------------------------------+
| Name: Jerome Richards |
| Course: CNS-2400 F01 |
| |
| Program: Integer Stack |
| File: driver.cpp |
+--------------------------------------------------------------------------*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "intStack.h"
using namespace std;
enum CMD { PUSH, POP, UNDEFINED };
struct cmdData
{
CMD itsCommand;
char itsTarget;
int itsData;
};
void dispPrologue();
bool openFile( ifstream& inFile );
bool readData( ifstream& inFile, string& textLine );
void processCmd( Integer_stack& myStack, string textLine );
bool parseCmd( string textLine, cmdData& theCmd );
void dumpStack( Integer_stack& myStack );
/*--------------------------------------------------------------------------+
| main() |
| |
| Establishes the program's environment by displaying the Prologue, and |
| the data file to be processed. |
| |
| The program will terminate if a filename is not entered. |
+--------------------------------------------------------------------------*/
int main( int argc, char* argv[] )
{
ifstream inFile;
Integer_stack myStack;
string textLine;
dispPrologue();
while( openFile( inFile ) )
{
cout << "Operation Stack Value Results" << endl;
cout << "--------- ----- --------- -------" << endl;
while( ! inFile.eof() )
{
if( readData( inFile, textLine ) )
processCmd( myStack, textLine );
}
inFile.close();
inFile.clear();
dumpStack( myStack );
}
return 0;
}
/*--------------------------------------------------------------------------+
| processCmd( Integer_stack& myStack, string textLine ) |
| |
| Purpose: process the command read from the data file |
| |
| Parameters: textLine contains the data to be parsed |
| Pre-conditions: myStack needs to be a valid Integer_stack object |
| Post-conditions: myStack is modified based on command contents |
| |
| Returns: none |
+--------------------------------------------------------------------------*/
void processCmd( Integer_stack& myStack, string textLine )
{
Integer_stack::stackCode tmpCode = Integer_stack::isUNKNOWN;
cmdData theCmd;
if( parseCmd( textLine, theCmd ) )
{
switch( theCmd.itsCommand )
{
case PUSH:
cout << "push ";
switch( theCmd.itsTarget )
{
case 'a':
cout << " a ";
cout.setf( ios_base::right );
cout << setw( 9 ) << theCmd.itsData;
cout.setf( ios_base::left );
cout << " ";
tmpCode = myStack.pusha( theCmd.itsData );
break;
case 'b':
cout << " b ";
cout.setf( ios_base::right );
cout << setw( 9 ) << theCmd.itsData;
cout.setf( ios_base::left );
cout << " ";
tmpCode = myStack.pushb( theCmd.itsData );
break;
default:
cout << "----- --------- ";
break;
}
cout << myStack.getErrStr( tmpCode ) << endl;
break;
case POP:
cout << "pop ";
switch( theCmd.itsTarget )
{
case 'a':
cout << " a ";
cout.setf( ios_base::right );
cout << setw( 9 ) << "";
cout.setf( ios_base::left );
cout << " ";
tmpCode = myStack.popa();
break;
case 'b':
cout << " b ";
cout.setf( ios_base::right );
cout << setw( 9 ) << "";
cout.setf( ios_base::left );
cout << " ";
tmpCode = myStack.popb();
break;
default:
cout << "----- --------- ";
break;
}
cout << myStack.getErrStr( tmpCode ) << endl;
break;
default:
cout << "unrecognized command" << endl;
break;
}
}
cout.setf( ios_base::left );
}
/*--------------------------------------------------------------------------+
| parseCmd( string textLine, cmdData& theCmd ) |
| |
| Purpose: parst the command from a text line |
| |
| Parameters: textLine contains the data to be parsed |
| theCmd will receive the command, if any |
| |
| Pre-conditions: none |
| Post-conditions: theCmd is modified to contain the command/parameters |
| |
| Returns: true if successful, false otherwise |
+--------------------------------------------------------------------------*/
bool parseCmd( string textLine, cmdData& theCmd )
{
int nPos;
theCmd.itsCommand = UNDEFINED;
theCmd.itsTarget = 0x00;
theCmd.itsData = -1;
nPos = textLine.find( "push " );
if( nPos >= 0 )
{
theCmd.itsCommand = PUSH;
theCmd.itsTarget = textLine[ 5 ];
const char* tmpPtr = textLine.c_str();
theCmd.itsData = atoi( tmpPtr+7 );
return true;
}
nPos = textLine.find( "pop " );
if( nPos >= 0 )
{
theCmd.itsCommand = POP;
theCmd.itsTarget = textLine[ 4 ];
return true;
}
return false;
}
/*--------------------------------------------------------------------------+
| dumpStack( Integer_stack& myStack ) |
| |
| Purpose: dumps the contents of the stack(s) to the display |
| |
| Parameters: myStack is a reference to a stack object |
| |
| Pre-conditions: none |
| Post-conditions: myStack is emptied - no items will remain |
| |
| Returns: none |
+--------------------------------------------------------------------------*/
void dumpStack( Integer_stack& myStack )
{
Integer_stack::stackCode tmpCode;
int tmpInt;
int tmpPtr;
cout << endl << endl << "A Stack" << endl;
tmpPtr = 0;
do
{
tmpCode = myStack.topa( tmpInt );
if( tmpCode == Integer_stack::isSUCCESS )
{
cout << "A[" << setw(2) << tmpPtr << "] = " << setw(10) << tmpInt << endl;
tmpPtr++;
myStack.popa();
}
} while( tmpCode == Integer_stack::isSUCCESS );
cout << endl << endl << "B Stack" << endl;
tmpPtr = 0;
do
{
tmpCode = myStack.topb( tmpInt );
if( tmpCode == Integer_stack::isSUCCESS )
{
cout << "B[" << setw(2) << tmpPtr << "] = " << setw(10) << tmpInt << endl;
tmpPtr++;
myStack.popb();
}
} while( tmpCode == Integer_stack::isSUCCESS );
}
/*--------------------------------------------------------------------------+
| readData( ifstream& inFile, string& textLine ) |
| |
| Purpose: read a line of text from the data file |
| |
| Parameters: inFile is the data stream |
| textLine receives the data |
| |
| Pre-conditions: inFile is a valid input stream |
| Post-conditions: textLine is modified to contain the data read |
| |
| Returns: true if successful, false otherwise |
+--------------------------------------------------------------------------*/
bool readData( ifstream& inFile, string& textLine )
{
bool lOkay = false;
getline( inFile, textLine );
if( ! inFile.fail() )
lOkay = true;
return lOkay;
}
/*--------------------------------------------------------------------------+
| openFile( ifstream& inFile ) |
| |
| Purpose: open a user specified data file |
| |
| Parameters: inFile is the data file stream to open |
| |
| Pre-conditions: none |
| Post-conditions: inFile will be opened for input if the file is found |
| |
| Returns: true if successful, false otherwise |
+--------------------------------------------------------------------------*/
bool openFile( ifstream& inFile )
{
const int MAXLEN = 80;
bool lOpen = false;
char fileName[ MAXLEN + 1 ];
while( ! lOpen )
{
cout << "Enter file name (blank to exit): ";
cin.getline( fileName, MAXLEN, '\n' );
if( ! strlen( fileName ) )
{
cout << endl << endl << "Press ENTER to exit the program";
cin.get();
exit( 0 );
}
inFile.open( fileName );
if( ! (lOpen = inFile.good()) )
{
cout << endl << "ERROR: could not open data file " << fileName << endl;
inFile.clear();
}
}
return lOpen;
}
/*--------------------------------------------------------------------------+
| dispPrologue() |
| |
| Purpose: display the author and program name |
| |
| Parameters: none |
| |
| Pre-conditions: none |
| Post-conditions: the screen is cleared, and the prologue is displayed |
| |
| Returns: none |
+--------------------------------------------------------------------------*/
void dispPrologue()
{
system( "cls" );
cout << " Name: Jerome Richards" << endl;
cout << " Course: CNS-2400 F01" << endl;
cout << "Program: Integer Stack" << endl;
cout << " File: jrp2.exe" << endl;
cout << endl << endl;
}