P-1
p1-start.cpp
p1-start.cpp
/**
@file
p1-start.cpp
*
*
@author
Student Programmer
*
@cwid
12345678
*
@assg
Programming Assignment #1
*
@date
January 14, 2019
*
@ide
Fabulous IDE 2.5
*
*
@descrption
Example of using the C++ Standard Template Library
* (STL) classes to implement solution for assignment 01.
* In this assignment, we maintain a stack and a queue,
* and demonstrate instantiationg STL data stuctures that
* can be used as stacks and queues to simulate items
* being added and removed from each data structure as
* requested by the simulation.
*/
#include
<
stdlib
.
h
>
#include
<
iostream
>
#include
<
fstream
>
#include
<
string
>
using
namespace
std
;
/** The stack/queue simulator
* The main loop for running tests of the queue data type.
* The file contains commands to enqueue and deque items
* from the queue.
*
*
@param
simfilename The name of the file (e.g. simfile-01.sim) to
* open and read commands to test the queue and stack data type
* on.
*/
void
runSimulation
(
char
*
simfilename
)
{
ifstream simfile
(
simfilename
);
string command
;
int
queueItem
;
string stackItem
;
// open the input simulation file to read and process simulation
// comands
cout
<<
"Processing stack/queue simulation file: "
<<
simfilename
<<
endl
;
if
(
!
simfile
.
is_open
())
{
cout
<<
"Error: could not open simulation file: "
<<
simfilename
<<
endl
;
exit
(
1
);
}
// read and process stack/queue simulation comands line by line
while
(
!
simfile
.
eof
())
{
simfile
>>
command
;
if
(
command
==
"enqueue"
)
{
simfile
>>
queueItem
;
cout
<<
"Enque : "
<<
queueItem
<<
endl
;
}
else
if
(
command
==
"dequeue"
)
{
cout
<<
"Dequeue: "
<<
queueItem
<<
endl
;
}
else
if
(
command
==
"push"
)
{
simfile
>>
stackItem
;
cout
<<
"Push : "
<<
stackItem
<<
endl
;
}
else
if
(
command
==
"pop"
)
{
cout
<<
"Popped : "
<<
stackItem
<<
endl
;
}
else
if
(
command
==
"exit"
)
// normal termination of simulation
{
cout
<<
"Simulation ends"
<<
endl
;
exit
(
0
);
}
else
{
cout
<<
"ERROR: unknown command: "
<<
command
<<
endl
;
exit
(
1
);
}
// you should display the contents of your stack and queue
// here, after processing the simulation command
cout
<<
endl
;
}
}
/** Main entry point of simulator
* The main entry point of the process simulator. We simply set up
* and initialize the environment, then call the appropriate function
* to begin the simulation. We expect a single command line argument
* which is the name of the simulation event file to process.
*
*
@param
argc The argument count
*
@param
argv The command line argument values. We expect argv[1] to be the
* name of a file in the current directory holding process events
* to simulate.
*
*
@returns
int Returns 0 to OS to indicate successful completion, or non zero
* value to indicate an error.
*/
int
main
(
int
argc
,
char
**
argv
)
{
// validate command line arguments
if
(
argc
!=
2
)
{
cout
<<
"Error: expecting simulation file as first command line parameter"
<<
endl
;
cout
<<
"Usage: "
<<
argv
[
0
]
<<
" simfile.sim"
<<
endl
;
exit
(
1
);
}
// Invoke the function to actually run the simulation
runSimulation
(
argv
[
1
]);
//runSimulation((char*)"simfile-01.sim");
// return status 0 to indicate successful program completion
return
0
;
}
prog-01.pdf
Programming Assignment #1
Stacks / Queues Simulator
CSci 430 Fall 2018
Dates:
Assigned: Monday January 21, 2019 Due: Wednesday January 30, 2019 (before Midnight)
Objectives:
� Programming in C/C++ review.
� Introduction to using STL data types
� Review of queues, stacks and other data types.
� Introduction to class programming assignment structures.
� Introduction to class style guidelines and expectations.
Description:
The programming assignments for this class have several objectives in mind. You are required to do the programming assignments using the C/C++ lan- guage. C and C++ are widely used for system programming tasks, such as building operating systems software, and other low level computing tasks that require good performance. This �rst programming assignment also acts as a review for several topics. In this assignment you need to use a simple queue data structure to perform the assigned task. You are required to im- plement your queue using C++ standard template library (STL) data types. So we will be reviewing or introducing you to using STL library types for programming. And �nally, this assignment will introduce you to the struc- ture and format of programming assignments for this class. The assignments
1
for this class take the form of terminal based simulations. We make use of command line parameters in order to specify options and inputs. Most of the assignments after this �rst ones are implementations of simulations of various aspects of operating system functions, such as memory management or process scheduling.
In this �rst assignment, you are to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (onto the queue) or pushed and popped (onto the stack). You are required to use STL data structures to implement and create the stack and queue for your program. There are several tutorials and references for the C++ STL library listed in our class website. I tend to use the following site as a reference for the STL container data types:
http://www.cplusplus.com/reference/stl/
The input �le for this simulation will simply be a list of operations to perform on either a stack or a queue. For example:
----- testfile1.tst --------
enqueue 5
enqueue 7
push blooper
push rookie
dequeue
push demerits
pop
enqueue 3
enqueue 8
push showplace
enqueue 9
dequeue
pop
dequeue
push palmetto
push zebra
pop
push collapse
dequeue
dequeue
dequeue
pop
pop
2
pop
pop
pop
enqueue 2
enqueue 4
push penguins
push sleeping
exit
---------------------------
Example correct output for this set of enqueue/dequeue operations will look like the following:
Processing stack/queue simulation file: simfile-01.sim
Enque : 5
Stack :
Queue : 5
Enque : 7
Stack :
Queue : 5 7
Push : blooper
Stack : blooper
Queue : 5 7
Push : rookie
Stack : rookie blooper
Queue : 5 7
Dequeue: 5
Stack : rookie blooper
Queue : 7
Push : demerits
Stack : demerits rookie blooper
Queue : 7
Popped : demerits
3
Stack : rookie blooper
Queue : 7
Enque : 3
Stack : rookie blooper
Queue : 7 3
Enque : 8
Stack : rookie blooper
Queue : 7 3 8
Push : showplace
Stack : showplace rookie blooper
Queue : 7 3 8
Enque : 9
Stack : showplace rookie blooper
Queue : 7 3 8 9
Dequeue: 7
Stack : showplace rookie blooper
Queue : 3 8 9
Popped : showplace
Stack : rookie blooper
Queue : 3 8 9
Dequeue: 3
Stack : rookie blooper
Queue : 8 9
Push : palmetto
Stack : palmetto rookie blooper
Queue : 8 9
Push : zebra
Stack : zebra palmetto rookie blooper
Queue : 8 9
Popped : zebra
4
Stack : palmetto rookie blooper
Queue : 8 9
Push : collapse
Stack : collapse palmetto rookie blooper
Queue : 8 9
Dequeue: 8
Stack : collapse palmetto rookie blooper
Queue : 9
Dequeue: 9
Stack : collapse palmetto rookie blooper
Queue :
Dequeue request from empty queue
Stack : collapse palmetto rookie blooper
Queue :
Popped : collapse
Stack : palmetto rookie blooper
Queue :
Popped : palmetto
Stack : rookie blooper
Queue :
Popped : rookie
Stack : blooper
Queue :
Popped : blooper
Stack :
Queue :
Pop request from empty stack
Stack :
Queue :
Enque : 2
5
Stack :
Queue : 2
Enque : 4
Stack :
Queue : 2 4
Push : penguins
Stack : penguins
Queue : 2 4
Push : sleeping
Stack : sleeping penguins
Queue : 2 4
Simulation ends
The stack should be a stack of strings, able to push and pop string items onto it. And your queue should be a queue of integers. A summary of the simulation commands
� enqueue intval
� Put the indicated integer onto the back of the current queue.
� dequeue
� Remove the item from the head of the current queue and display it.
� push stringval
� Push the indicated string onto the top of the current stack.
� pop
� Pop the top string from the stack and display it.
You will note that after each simulated operation, the contents of the stack and queue are always displayed. You should always show all of the items on your stack and queue. The front of the queue should be the �rst item shown (reading from left to right), and the back item will be the last item displayed. Likewise, for the stack, the top of the stack is always the
6
�rst item shown, and the bottom of the stack is the last item. If the stack is empty then of course nothing is displayed. Also note, that if a pop or dequeue is attempted from an emtpry stack or queue, you should detect this and display an error message, but then ignore the request. Subsequent pushes or enqueues should work normally to add items to your data structures.
The C++ STL library has stack and queue containers. However, unfor- tunately, these containers do not allow you to iterate over and display all of the contents of the containers. You can use any STL container you want that you can get to work. However I would suggest you use a simple list container for both your stack and queue. The list has methods to push_front() and push_back() as well as pop_front() and pop_back(). Thus you can use these methods to implement either stack (FILO) or queue (FIFO) behavior of the items you manage in the container.
A starting template has been provided for you named p1-start.cpp. You are required to use this template for your assignment. I have given you code to open and read in the simulation commands. In addition, you will see that a single command line parameter is expected and required for the template �le. For this �rst assignment, we simply specify the name of a �le of commands to open and simulate (e.g. `sim�le-01.sim`). Future assignments might require more complex initial arguments and parameters for your simulations. We will discuss using command line arguments in class.
Also besides `sim�le-01.sim` I have provided a `sim�le-01.res` �le. You need to display your output to standard output (do not open and write output to a �le). And your output needs to exactly match the expected output given in the result �le. I partially grade your assignments by doing a di� between your output, and the expected correct output. There are also some additional simulation and result �les provided, so that you can further test your program against the expected results.
Assignment Submission and Requirements
All source �les you create for you solution (.c or .cpp/.c++ and .h header �les) should be uploaded to the MyLeo Online submission folder created for this assignment by the deadline. You should not attach any �les besides the source �les containing your C/C++ code. But you should make sure you attach all needed �les you create to your submission, so that I can compile and run your solution.
You are required to write the program in standard C/C++ programming language. You should use a relatively recent version of the C/C++ compiler
7
(C90 C++98 is �ne, or the more recent C99 C++11 will also be acceptable), and/or recent IDE that has an up to date compiler. You should only use standard C/C++ libraries, do not use Microsoft speci�c or other third-party developed external libraries. You must use the C++ standard template library containers (like the list and queue items) to implement the data structures you need.
8
simfile-01.sim
enqueue 5 enqueue 7 push blooper push rookie dequeue push demerits pop enqueue 3 enqueue 8 push showplace enqueue 9 dequeue pop dequeue push palmetto push zebra pop push collapse dequeue dequeue dequeue pop pop pop pop pop enqueue 2 enqueue 4 push penguins push sleeping exit
simfile-02.sim
pop pop enqueue 62 pop dequeue dequeue dequeue dequeue dequeue pop push transcrib push pugilis pop dequeue push uf dequeue dequeue dequeue pop push infrequenc enqueue 93 push sierr enqueue 27 enqueue 87 enqueue 17 push ejectin pop enqueue 38 pop enqueue 47 pop enqueue 99 push turnke dequeue dequeue dequeue dequeue pop enqueue 83 pop pop dequeue pop pop push e push dinnere pop enqueue 76 dequeue dequeue dequeue dequeue push kindnesse enqueue 27 enqueue 56 push beekeepe push stingray dequeue enqueue 17 pop pop pop dequeue pop pop push slander dequeue enqueue 72 push boroug enqueue 56 dequeue dequeue push transatlanti pop dequeue exit
simfile-03.sim
enqueue 54 enqueue 84 pop push rus enqueue 77 pop push mutinyin push ordur enqueue 14 push disarrangin pop enqueue 6 dequeue dequeue enqueue 9 dequeue pop dequeue push sasse push culture enqueue 52 enqueue 66 pop enqueue 82 push roo push exhaustin dequeue enqueue 80 enqueue 59 pop pop pop pop pop push wadin enqueue 15 dequeue enqueue 13 enqueue 59 enqueue 35 pop pop enqueue 49 dequeue push succum pop pop push vulgarize enqueue 48 dequeue pop pop dequeue dequeue dequeue dequeue dequeue push yalt push geronim enqueue 21 push servicewoma push disfigure dequeue dequeue dequeue dequeue enqueue 27 push nowa push luminousl dequeue push cougar dequeue pop dequeue dequeue pop pop push inconsisten enqueue 88 pop push proteste push harmin pop dequeue pop push intricatel pop pop pop dequeue dequeue dequeue pop pop dequeue enqueue 30 dequeue push nonagenaria push uniforme push lingerer enqueue 83 push travelogue pop enqueue 17 dequeue dequeue push materialisti dequeue enqueue 93 dequeue enqueue 94 enqueue 55 push midstrea dequeue enqueue 34 pop pop enqueue 100 push disengage dequeue enqueue 87 dequeue dequeue enqueue 80 push nobe push contraceptive push precipitatin dequeue dequeue dequeue push nann push mas pop pop enqueue 38 dequeue enqueue 81 push outfield pop dequeue enqueue 35 dequeue enqueue 35 pop enqueue 22 pop enqueue 74 pop pop dequeue dequeue enqueue 39 enqueue 13 pop pop enqueue 18 enqueue 87 enqueue 33 push jewelrie enqueue 55 dequeue push dispassionat dequeue push harpe enqueue 66 pop enqueue 56 dequeue push titula dequeue pop dequeue dequeue push bristo push gallivant dequeue dequeue dequeue push whitefishe enqueue 92 dequeue dequeue pop dequeue dequeue dequeue enqueue 93 enqueue 99 dequeue push taver pop dequeue push presse dequeue push feedbag push eccentric enqueue 44 dequeue enqueue 44 pop exit
simfile-01.res
simfile-02.res
simfile-03.res