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 pathStackQueue.cpp
More file actions
312 lines (266 loc) · 10.3 KB
/
Copy pathStackQueue.cpp
File metadata and controls
312 lines (266 loc) · 10.3 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
//=================================================================================================
// Author: Jerome Richards
// Assignment: Program 3 Stack and Queue
// File: StackQueue.cpp
// Instructor: Dr. Reza Sanati
// Class: CS 2420-002
// Date Written: 22-September-2015
//
// Description:
//
// Read Stack and Queue commands from a file and execute the appropriate command for the correct
// abstract data type - Stack or Queue.
//
// Print the contents of each abstract data type, stack and queue, when done.
//=================================================================================================
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include "Stack.h"
#include "Queue.h"
using namespace std;
void displayProlog();
bool openFile(ifstream& inFile);
bool readCommand(ifstream& inFile, string& commandLine);
void tokenize(string& commandLine, vector<string>& tokens, const char delimeter = ' ');
void displayLine(const string& command, const string& adt, const int* value, const char* result);
//-------------------------------------------------------------------------------------------------
// Function Name: main
//
// Purpose: start of program
//
// use support functions to open and read a list of stack and queue commands
// from a file
//
// calls funtions on abstract data types, stack and queue, to process the
// appropriate command
//
// Parameters: none
// Returns: exit code
// Pre-conditions: none
// Post-conditions: none
//-------------------------------------------------------------------------------------------------
int main()
{
const string ADT_STACK("Stack");
const string STACK_PUSH("push");
const string STACK_POP("pop");
const string STACK_TOP("top");
const string ADT_QUEUE("Queue");
const string QUEUE_APPEND("append");
const string QUEUE_SERVE("serve");
ifstream inFile;
displayProlog();
while (openFile(inFile))
{
cout << "Operation ADT Value Result " << endl;
cout << "--------- ----- ----------- ---------" << endl;
Stack stack;
Queue queue;
string commandLine;
while (!inFile.eof())
{
if (readCommand(inFile, commandLine))
{
vector<string> tokens;
tokenize(commandLine, tokens);
if (tokens.size() > 0)
{
if (_stricmp(STACK_PUSH.c_str(), tokens[0].c_str()) == 0)
{
if (tokens.size() >= 2)
{
int value = stoi(tokens[1]);
Stack::StackError errorCode = stack.push(value);
displayLine(tokens[0], ADT_STACK, &value, stack.errorToString(errorCode));
}
}
else if (_stricmp(STACK_POP.c_str(), tokens[0].c_str()) == 0)
{
Stack::StackError errorCode = stack.pop();
displayLine(tokens[0], ADT_STACK, nullptr, stack.errorToString(errorCode));
}
else if (_stricmp(QUEUE_APPEND.c_str(), tokens[0].c_str()) == 0)
{
if (tokens.size() >= 2)
{
int value = stoi(tokens[1]);
Queue::QueueError errorCode = queue.append(value);
displayLine(tokens[0], ADT_QUEUE, &value, queue.errorToString(errorCode));
}
}
else if (_stricmp(QUEUE_SERVE.c_str(), tokens[0].c_str()) == 0)
{
Queue::QueueError errorCode = queue.serve();
displayLine(tokens[0], ADT_QUEUE, nullptr, queue.errorToString(errorCode));
}
}
}
}
inFile.close();
// list Stack values
cout << endl;
cout << "Stack Values" << endl;
cout << "------------" << endl;
int value;
while (stack.top(value) == Stack::success)
{
cout << value << endl;
stack.pop();
}
// list Queue values
cout << endl;
cout << "Queue Values" << endl;
cout << "------------" << endl;
while (queue.front(value) == Queue::success)
{
cout << value << endl;
queue.serve();
}
// allow user to process again
cout << endl;
cout << "End of processing. Press ENTER to continue.";
cin.get();
displayProlog();
}
return 0;
}
//-------------------------------------------------------------------------------------------------
// Function Name: displayProlog
// Purpose: Display author name, class, and assignment information
// Parameters: none
// Returns: none
// Pre-conditions: none
// Post-conditions: none
//-------------------------------------------------------------------------------------------------
void displayProlog()
{
system("cls");
cout << "Author: Jerome Richards" << endl;
cout << "Assignment: Program 3 Stack and Queue" << endl;
cout << "Instructor: Dr. Reza Sanati" << endl;
cout << "Class: CS 2420-002" << endl;
cout << "Description: Read Stack and Queue commands from a file and execute the appropriate" << endl;
cout << " command for the correct abstract data type - Stack or Queue." << endl;
cout << " Print the contents of each abstract data type, stack and queue, when done." << endl;
cout << "========================================================================================" << endl;
cout << endl << endl;
}
//-------------------------------------------------------------------------------------------------
// Function Name: openFile
//
// Purpose: open a file for input
// allow the user to enter the filename and verify that it exists
//
// Parameters: ifstream reference (inFile) is the stream to use
// Returns: true the file was opened, false no file opened
// Pre-conditions: none
// Post-conditions: none
//-------------------------------------------------------------------------------------------------
bool openFile(ifstream& inFile)
{
bool isOpen = false;
char filename[_MAX_PATH];
while (!isOpen)
{
cout << "Enter filename <leave blank to exit>: ";
cin.getline(filename, _MAX_PATH);
if (strlen(filename) == 0)
{
cout << "No filename entered. Press ENTER to exit.";
cin.get();
exit(0);
}
inFile.open(filename);
isOpen = inFile.good();
if (!isOpen)
{
cout << "Error opening file " << filename << endl;
inFile.clear();
}
}
return isOpen;
}
//-------------------------------------------------------------------------------------------------
// Function Name: readCommand
// Purpose: reads the next command in the stream
//
// Parameters: ifstream reference (inFile) used to extract the next integer from
// string reference (commandLine) used to hold the command read from the stream
//
// Returns: true if successful, false otherwise
// Pre-conditions: ifstream is opened and ready
// Post-conditions: none
//-------------------------------------------------------------------------------------------------
bool readCommand(ifstream& inFile, string& commandLine)
{
bool isOk = false;
getline(inFile, commandLine);
if (!inFile.fail())
{
isOk = true;
}
return isOk;
}
//-------------------------------------------------------------------------------------------------
// Function Name: tokenize
// Purpose: uses stringstream to split a command line into tokens
//
// Parameters: string reference (commandLine) holds the command line to be tokenized
// vector<string> reference (tokens) used to hold the tokens
// char (delimeter) used to split the command line into tokens
//
// Returns: void
// Pre-conditions: none
// Post-conditions: none
//-------------------------------------------------------------------------------------------------
void tokenize(string& commandLine, vector<string>& tokens, const char delimeter)
{
istringstream streamCommandLine(commandLine);
string token;
while (getline(streamCommandLine, token, delimeter))
{
tokens.push_back(token);
}
}
//-------------------------------------------------------------------------------------------------
// Function Name: displayLine
// Purpose: displays the formatted results of the processed line
//
// Parameters: string reference (command) that was read from the file
// string reference (adt) is the abstract data type (stack, queue)
// int pointer (value) read from the file, if any
// char pointer (error) description from the command results
//
// Returns: void
// Pre-conditions: none
// Post-conditions: none
//-------------------------------------------------------------------------------------------------
void displayLine(const string& command, const string& adt, const int* value, const char* result)
{
const int COMMAND_WIDTH = 9;
const int ADT_WIDTH = 5;
const int VALUE_WIDTH = 11;
const int RESULT_WIDTH = 9;
const int SPACER_WIDTH = 3;
cout.setf(ios::left, ios::adjustfield);
cout << setw(COMMAND_WIDTH)
<< command
<< setw(SPACER_WIDTH)
<< ' '
<< setw(ADT_WIDTH)
<< adt
<< setw(SPACER_WIDTH)
<< ' ';
cout.setf(ios::right, ios::adjustfield);
cout << setw(VALUE_WIDTH) << (value != nullptr ? *value : ' ');
cout.setf(ios::left, ios::adjustfield);
cout << setw(SPACER_WIDTH)
<< ' '
<< setw(RESULT_WIDTH)
<< (result != nullptr ? result : " ")
<< endl;
}