SlideShare a Scribd company logo
We're writing code for a project that dynamically allocates an array stack. My other code works,
but my Min(), Max(), and Peek() functions fail. I've provided my code for those functions at the
end of the post.
main.cpp
_________________________________________
#include
#include
#include
#include
#include "stack.h"
using namespace std;
int main(int argc, char* argv[])
{
ifstream inputs; // Input file for commands
char op; // Hold operation and optional char input
int value; // Value input from file
string comment; // Holds comment from file
Stack* sPtr = NULL; // Will point to TemplateQ object
// Output usage message if one input file name is not provided
if (argc != 2)
{
cout << "Usage: project03  ";
return 1;
}
// Attempt to open input file -- terminate if file does not open
inputs.open(argv[1]);
if (!inputs)
{
cout << "Error - unable to open input file" << endl;
return 1;
}
// Input and echo header comment from file
getline(inputs, comment); // Input and echo the comment appearing in the test file
cout << endl << '#' << comment << endl;
// Process commands from input file
inputs >> op; // Attempt to input first command
while (inputs)
{
switch (op) // Process operation input from file
{
case '#': // Test file comment
getline(inputs, comment); // Input and echo the comment appearing in the test
file
cout << '#' << comment << endl;
break;
case 'c': // Parameterized Constructor
inputs >> value;
cout << endl << "Stack(" << value << ")";
try
{
sPtr = new Stack(value); // Attempt to create a stack object with array size
value
cout << " -- completed" << endl;
}
catch ( std::bad_alloc )
{
cout << "Failed : Terminating now..." << endl;
return 1;
}
break;
case '+': // Push
inputs >> value;
cout << "Push(" << value << ") -- ";
try
{
sPtr->Push(value);
cout << "completed";
}
catch (StackFull)
{
cout << "Failed Full Stack";
}
catch (...)
{
cout << "operation failed" << endl;
}
cout << endl;
break;
case '-': // Pop
cout << "Pop() -- ";
try
{
sPtr->Pop();
cout << "completed";
}
catch (StackEmpty)
{
cout << "Failed Empty Stack";
}
catch (...)
{
cout << "operation failed" << endl;
}
cout << endl;
break;
case 'f': // IsFull
cout << "IsFull() -- ";
try
{
if (sPtr->IsFull())
cout << "true";
else
cout << "false";
}
catch ( ... )
{
cout << "operation failed";
}
cout << endl;
break;
case 'e': // IsEmpty
cout << "IsEmpty() -- ";
try
{
if (sPtr->IsEmpty())
cout << "true";
else
cout << "false";
}
catch ( ... )
{
cout << "operation failed";
}
cout << endl;
break;
case 'm': // Make Empty
cout << "MakeEmpty() -- ";
try
{
sPtr->MakeEmpty();
cout << "completed" << endl;
}
catch (...)
{
cout << "operation failed" << endl;
}
break;
case 'p': // Print Stack
cout << "Print() -- ";
sPtr->Print();
break;
case 't': // Top of Stack
try
{
cout << "Top() -- " << sPtr->Top() << endl;
}
catch (StackEmpty)
{
cout << "Top() -- Failed Empty Stack" << endl;
}
catch (...)
{
cout << "operation failed" << endl;
}
break;
case '>': // Max value within Stack
try
{
cout << "Max() -- " << sPtr->Max() << endl;
}
catch (StackEmpty)
{
cout << "Max() -- Failed Empty Stack" << endl;
}
catch (...)
{
cout << "operation failed" << endl;
}
break;
case '<': // Min value within Stack
try
{
cout << "Min() -- " << sPtr->Min() << endl;
}
catch (StackEmpty)
{
cout << "Min() -- Failed Empty Stack" << endl;
}
catch (...)
{
cout << "operation failed" << endl;
}
break;
case '?': // Peek(n) Stack
inputs >> value;
try
{
cout << "Peek(" << value << ") -- " << sPtr->Peek(value) << endl;
}
catch (StackInvalidPeek)
{
cout << "Peek(" << value << ") -- Failed Invalid Peek" << endl;
}
catch (...)
{
cout << "operation failed" << endl;
}
break;
case 's': // Size of Stack
cout << "Size() -- ";
try
{
cout << sPtr->Size() << endl;
}
catch (...)
{
cout << "operation failed" << endl;
}
break;
case 'z': // Capacity of Stack
cout << "Capacity() -- ";
try
{
cout << sPtr->Capacity() << endl;
}
catch (...)
{
cout << "operation failed" << endl;
}
break;
case 'd': // Destructor
cout << "~Stack() -- ";
try
{
delete sPtr;
sPtr = NULL;
cout << "completed" << endl << endl;
}
catch (...)
{
cout << "operation failed" << endl << endl;
}
break;
default: // Error
cout << "Error - unrecognized operation '" << op << "'" << endl;
cout << "Terminating now..." << endl;
return 1;
break;
}
inputs >> op; // Attempt to input next command
}
return 0;
} // End main()
_______________________________________________________________
stack.h
_________________________________
//
// The comments have been updated, but there have been no changes to the code.
//
// Specification file for Stack class, a stack of integers implemented
// as an array whose size can be increased to store more data.
//
// ***** DO NOT MODIFY OR SUBMIT THIS FILE *****
//
#include
using namespace std;
#ifndef STACK_H
#define STACK_H
class StackEmpty
{
// Exception class - throw an object of this type when stack is empty
// Hint: there is no code for exception classes
};
class StackFull
{
// Exception class - throw an object of this type when stack is full
};
class StackInvalidPeek
{
// Exception class - throw an object of this type when invalid peek position is used
};
class Stack // Models stack of integers ADT implemented as a dynamically allocated
array
{
private:
int* array; // Points to the stack array
int num; // Holds max number of elements that may be stored in stack array
int top; // Holds the index of the top data value stored on the stack
void Resize(int n); // Attempts to increase size of stack array to 2*num and then push n onto
stack
// If unable to resize, throw StackFull exception
public:
Stack(int n); // Parameterized constructor dynamically allocates an empty stack array
// that may hold no more than n elements and initializes other private variables
~Stack(); // Destructor deallocates all dynamically-allocated memory
// associated with the object
void Push(int n); // Pushes integer n onto top of stack. If stack is full, attempts to
// resize stack and then push n. If unable to resize, throws StackFull exception.
void Pop(); // Removes top integer from stack
// If stack is empty, throws StackEmpty exception
bool IsEmpty() const; // Returns true if stack is empty; false otherwise
bool IsFull() const; // Returns true if stack is full; false otherwise
void MakeEmpty(); // Removes all items from stack leaving an empty, but usable stack
with capacity num
// If stack is already empty, MakeEmpty() does nothing
int Top() const; // Returns value of top integer on stack WITHOUT modifying the stack
// If stack is empty, throws StackEmpty exception
int Size() const; // Returns number of items on stack WITHOUT modifying the stack
int Max() const; // Returns value of largest integer on stack WITHOUT modifying the
stack
// If stack is empty, throws StackEmpty
int Min() const; // Returns value of smallest integer on stack WITHOUT modifying the
stack
// If stack is empty, throws StackEmpty
int Peek(unsigned int n) const; // Returns stack value n levels down from top of stack. Peek(0)
= Top()
// If position n does not exist, throws StackInvalidPeek
int Capacity() const; // Returns total num of elements that maybe stored in stack array
/******* DO NOT MODIFY ANY OF THE CODE FOR PRINT() *******/
/****** DO NOT PLACE A COPY OF PRINT() CODE IN STACK.CPP!!! *******/
void Print() const // Writes stack contents to stdout, separated by a space, followed by endl
{
int index = top;
cout << "Top { ";
while (index != -1)
{
cout << array[index] << " ";
index--;
}
cout << "} Bottom" << endl;
} // End Print()
}; // End Class Stack
#endif
_____________________________________________________________
.txt file
______________________________
# p03input3.txt -- Test Min(), Max(), Peek(...)
# Test Min() and Max()
c 8
<
>
+ 5
+ 30
+ 15
+ 20
+ 50
+ 10
p
<
>
+ 20
+ 50
+ 5
p
<
>
-
-
-
p
<
>
-
-
-
p
<
>
d
# Test Peek(n)
c 8
? 0
+ 5
+ 30
+ 15
+ 20
+ 50
+ 10
p
? 0
? 1
? 2
? 3
? 4
? 5
? 6
p
-
-
-
p
? 0
? 1
? 2
? 5
p
-
-
-
p
? 0
? 1
? 2
? 3
? 4
? 5
+ 25
+ 9
+ 8
p
? 0
? 1
? 2
? 3
? 4
? 5
d
____________________________________________________________
Error Messages
_________________________________________________________
Here's my code.
stack.cpp start of p3input3.txt Min ( ) -- Min ( ) -- Min ( ) -- Min() -- | Min) -- 5 in) -5 | Min() -- 5
Min) -- 5 FAILED Incorrect outputs PASSED No memory leak detected FAILED p031nput3.txt
End of p31nput3.txt
Solution
#include
#include
#include
#include
#include "stack.h"
using namespace std;
int main(int argc, char* argv[])
{
ifstream inputs; // Input file for commands
char op; // Hold operation and optional char input
int value; // Value input from file
string comment; // Holds comment from file
Stack* sPtr = NULL; // Will point to TemplateQ object
// Output usage message if one input file name is not provided
if (argc != 2)
{
cout << "Usage: project03  ";
return 1;
}
// Attempt to open input file -- terminate if file does not open
inputs.open(argv[1]);
if (!inputs)
{
cout << "Error - unable to open input file" << endl;
return 1;
}
// Input and echo header comment from file
getline(inputs, comment); // Input and echo the comment appearing in the test file
cout << endl << '#' << comment << endl;
// Process commands from input file
inputs >> op; // Attempt to input first command
while (inputs)
{
switch (op) // Process operation input from file
{
case '#': // Test file comment
getline(inputs, comment); // Input and echo the comment appearing in the test
file
cout << '#' << comment << endl;
break;
case 'c': // Parameterized Constructor
inputs >> value;
cout << endl << "Stack(" << value << ")";
try
{
sPtr = new Stack(value); // Attempt to create a stack object with array size
value
cout << " -- completed" << endl;
}
catch ( std::bad_alloc )
{
cout << "Failed : Terminating now..." << endl;
return 1;
}
break;
case '+': // Push
inputs >> value;
cout << "Push(" << value << ") -- ";
try
{
sPtr->Push(value);
cout << "completed";
}
catch (StackFull)
{
cout << "Failed Full Stack";
}
catch (...)
{
cout << "operation failed" << endl;
}
cout << endl;
break;
case '-': // Pop
cout << "Pop() -- ";
try
{
sPtr->Pop();
cout << "completed";
}
catch (StackEmpty)
{
cout << "Failed Empty Stack";
}
catch (...)
{
cout << "operation failed" << endl;
}
cout << endl;
break;
case 'f': // IsFull
cout << "IsFull() -- ";
try
{
if (sPtr->IsFull())
cout << "true";
else
cout << "false";
}
catch ( ... )
{
cout << "operation failed";
}
cout << endl;
break;
case 'e': // IsEmpty
cout << "IsEmpty() -- ";
try
{
if (sPtr->IsEmpty())
cout << "true";
else
cout << "false";
}
catch ( ... )
{
cout << "operation failed";
}
cout << endl;
break;
case 'm': // Make Empty
cout << "MakeEmpty() -- ";
try
{
sPtr->MakeEmpty();
cout << "completed" << endl;
}
catch (...)
{
cout << "operation failed" << endl;
}
break;
case 'p': // Print Stack
cout << "Print() -- ";
sPtr->Print();
break;
case 't': // Top of Stack
try
{
cout << "Top() -- " << sPtr->Top() << endl;
}
catch (StackEmpty)
{
cout << "Top() -- Failed Empty Stack" << endl;
}
catch (...)
{
cout << "operation failed" << endl;
}
break;
case '>': // Max value within Stack
try
{
cout << "Max() -- " << sPtr->Max() << endl;
}
catch (StackEmpty)
{
cout << "Max() -- Failed Empty Stack" << endl;
}
catch (...)
{
cout << "operation failed" << endl;
}
break;
case '<': // Min value within Stack
try
{
cout << "Min() -- " << sPtr->Min() << endl;
}
catch (StackEmpty)
{
cout << "Min() -- Failed Empty Stack" << endl;
}
catch (...)
{
cout << "operation failed" << endl;
}
break;
case '?': // Peek(n) Stack
inputs >> value;
try
{
cout << "Peek(" << value << ") -- " << sPtr->Peek(value) << endl;
}
catch (StackInvalidPeek)
{
cout << "Peek(" << value << ") -- Failed Invalid Peek" << endl;
}
catch (...)
{
cout << "operation failed" << endl;
}
break;
case 's': // Size of Stack
cout << "Size() -- ";
try
{
cout << sPtr->Size() << endl;
}
catch (...)
{
cout << "operation failed" << endl;
}
break;
case 'z': // Capacity of Stack
cout << "Capacity() -- ";
try
{
cout << sPtr->Capacity() << endl;
}
catch (...)
{
cout << "operation failed" << endl;
}
break;
case 'd': // Destructor
cout << "~Stack() -- ";
try
{
delete sPtr;
sPtr = NULL;
cout << "completed" << endl << endl;
}
catch (...)
{
cout << "operation failed" << endl << endl;
}
break;
default: // Error
cout << "Error - unrecognized operation '" << op << "'" << endl;
cout << "Terminating now..." << endl;
return 1;
break;
}
inputs >> op; // Attempt to input next command
}
return 0;
}

More Related Content

Similar to Were writing code for a project that dynamically allocates an arra.pdf

Stack queue
Stack queueStack queue
Stack queue
Fraboni Ec
 
Stack queue
Stack queueStack queue
Stack queue
Hoang Nguyen
 
Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdf
armyshoes
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
afgt2012
 
STLStack.pdf
STLStack.pdfSTLStack.pdf
STLStack.pdf
rajaratna4
 
Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more
steviesellars
 
Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docx
MARRY7
 
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
curwenmichaela
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8ecomputernotes
 
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdfWrite message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
eyeonsecuritysystems
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
ChereCheek752
 
Given the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docxGiven the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docx
shericehewat
 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
ARCHANASTOREKOTA
 
package algs13;import stdlib.;import java.util.Iterator;im.docx
package algs13;import  stdlib.;import java.util.Iterator;im.docxpackage algs13;import  stdlib.;import java.util.Iterator;im.docx
package algs13;import stdlib.;import java.util.Iterator;im.docx
gerardkortney
 
Namespaces
NamespacesNamespaces
Namespaces
zindadili
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
AkhilaaReddy
 
Stack, queue and hashing
Stack, queue and hashingStack, queue and hashing
Stack, queue and hashing
Dumindu Pahalawatta
 

Similar to Were writing code for a project that dynamically allocates an arra.pdf (20)

Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdf
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
 
STLStack.pdf
STLStack.pdfSTLStack.pdf
STLStack.pdf
 
Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more
 
Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docx
 
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
 
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdfWrite message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
 
Given the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docxGiven the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docx
 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
 
package algs13;import stdlib.;import java.util.Iterator;im.docx
package algs13;import  stdlib.;import java.util.Iterator;im.docxpackage algs13;import  stdlib.;import java.util.Iterator;im.docx
package algs13;import stdlib.;import java.util.Iterator;im.docx
 
Namespaces
NamespacesNamespaces
Namespaces
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Stack, queue and hashing
Stack, queue and hashingStack, queue and hashing
Stack, queue and hashing
 

More from fsenterprises

Why do you believe money laundering is a difficult fraud to investig.pdf
Why do you believe money laundering is a difficult fraud to investig.pdfWhy do you believe money laundering is a difficult fraud to investig.pdf
Why do you believe money laundering is a difficult fraud to investig.pdf
fsenterprises
 
Which of the following is a characteristic of Baroque instrumental m.pdf
Which of the following is a characteristic of Baroque instrumental m.pdfWhich of the following is a characteristic of Baroque instrumental m.pdf
Which of the following is a characteristic of Baroque instrumental m.pdf
fsenterprises
 
What was the energy that was not passed on to the next trophic le.pdf
What was the energy that was not passed on to the next trophic le.pdfWhat was the energy that was not passed on to the next trophic le.pdf
What was the energy that was not passed on to the next trophic le.pdf
fsenterprises
 
What sort of religious ideas does the megalith Stonehenge suggest A.pdf
What sort of religious ideas does the megalith Stonehenge suggest A.pdfWhat sort of religious ideas does the megalith Stonehenge suggest A.pdf
What sort of religious ideas does the megalith Stonehenge suggest A.pdf
fsenterprises
 
What are successful approaches to forecasting and creating a needed .pdf
What are successful approaches to forecasting and creating a needed .pdfWhat are successful approaches to forecasting and creating a needed .pdf
What are successful approaches to forecasting and creating a needed .pdf
fsenterprises
 
What are the major challenges for managing health care information t.pdf
What are the major challenges for managing health care information t.pdfWhat are the major challenges for managing health care information t.pdf
What are the major challenges for managing health care information t.pdf
fsenterprises
 
Using the Tustin equivalence with the prewarping method, fnd the dis.pdf
Using the Tustin equivalence with the prewarping method, fnd the dis.pdfUsing the Tustin equivalence with the prewarping method, fnd the dis.pdf
Using the Tustin equivalence with the prewarping method, fnd the dis.pdf
fsenterprises
 
Use Java programming Thank you very much Programing requirement for.pdf
Use Java programming Thank you very much Programing requirement for.pdfUse Java programming Thank you very much Programing requirement for.pdf
Use Java programming Thank you very much Programing requirement for.pdf
fsenterprises
 
Translate the following C++ code segment into Mic-1 IJVM machine lan.pdf
Translate the following C++ code segment into Mic-1 IJVM machine lan.pdfTranslate the following C++ code segment into Mic-1 IJVM machine lan.pdf
Translate the following C++ code segment into Mic-1 IJVM machine lan.pdf
fsenterprises
 
Transactions On September 1 of the current year, Joy Tucker estab.pdf
Transactions On September 1 of the current year, Joy Tucker estab.pdfTransactions On September 1 of the current year, Joy Tucker estab.pdf
Transactions On September 1 of the current year, Joy Tucker estab.pdf
fsenterprises
 
To correct the vision of a person who is nearR sighted, we use a div.pdf
To correct the vision of a person who is nearR sighted, we use a div.pdfTo correct the vision of a person who is nearR sighted, we use a div.pdf
To correct the vision of a person who is nearR sighted, we use a div.pdf
fsenterprises
 
The height of the water in the tank pictured below 7 ft. The tank is.pdf
The height of the water in the tank pictured below 7 ft. The tank is.pdfThe height of the water in the tank pictured below 7 ft. The tank is.pdf
The height of the water in the tank pictured below 7 ft. The tank is.pdf
fsenterprises
 
Suppose that a classroom has 4 light bulbs. The probability that eac.pdf
Suppose that a classroom has 4 light bulbs. The probability that eac.pdfSuppose that a classroom has 4 light bulbs. The probability that eac.pdf
Suppose that a classroom has 4 light bulbs. The probability that eac.pdf
fsenterprises
 
Security Standards Please respond to the following A number of .pdf
Security Standards Please respond to the following A number of .pdfSecurity Standards Please respond to the following A number of .pdf
Security Standards Please respond to the following A number of .pdf
fsenterprises
 
Real Estate Finance essay questionWhy did observers at first belie.pdf
Real Estate Finance essay questionWhy did observers at first belie.pdfReal Estate Finance essay questionWhy did observers at first belie.pdf
Real Estate Finance essay questionWhy did observers at first belie.pdf
fsenterprises
 
Provide your key points of Setup Reducation Provide your key points.pdf
Provide your key points of Setup Reducation  Provide your key points.pdfProvide your key points of Setup Reducation  Provide your key points.pdf
Provide your key points of Setup Reducation Provide your key points.pdf
fsenterprises
 
Please help. C++ The program is an interactive program th.pdf
Please help. C++ The program is an interactive program th.pdfPlease help. C++ The program is an interactive program th.pdf
Please help. C++ The program is an interactive program th.pdf
fsenterprises
 
please answer this case using down picturein business ethics emplo.pdf
please answer this case using down picturein business ethics emplo.pdfplease answer this case using down picturein business ethics emplo.pdf
please answer this case using down picturein business ethics emplo.pdf
fsenterprises
 
Al has three times as much money as Bill. Bill has two times as much.pdf
Al has three times as much money as Bill. Bill has two times as much.pdfAl has three times as much money as Bill. Bill has two times as much.pdf
Al has three times as much money as Bill. Bill has two times as much.pdf
fsenterprises
 
A SURVEY is a tool used to collect information about a population. L.pdf
A SURVEY is a tool used to collect information about a population. L.pdfA SURVEY is a tool used to collect information about a population. L.pdf
A SURVEY is a tool used to collect information about a population. L.pdf
fsenterprises
 

More from fsenterprises (20)

Why do you believe money laundering is a difficult fraud to investig.pdf
Why do you believe money laundering is a difficult fraud to investig.pdfWhy do you believe money laundering is a difficult fraud to investig.pdf
Why do you believe money laundering is a difficult fraud to investig.pdf
 
Which of the following is a characteristic of Baroque instrumental m.pdf
Which of the following is a characteristic of Baroque instrumental m.pdfWhich of the following is a characteristic of Baroque instrumental m.pdf
Which of the following is a characteristic of Baroque instrumental m.pdf
 
What was the energy that was not passed on to the next trophic le.pdf
What was the energy that was not passed on to the next trophic le.pdfWhat was the energy that was not passed on to the next trophic le.pdf
What was the energy that was not passed on to the next trophic le.pdf
 
What sort of religious ideas does the megalith Stonehenge suggest A.pdf
What sort of religious ideas does the megalith Stonehenge suggest A.pdfWhat sort of religious ideas does the megalith Stonehenge suggest A.pdf
What sort of religious ideas does the megalith Stonehenge suggest A.pdf
 
What are successful approaches to forecasting and creating a needed .pdf
What are successful approaches to forecasting and creating a needed .pdfWhat are successful approaches to forecasting and creating a needed .pdf
What are successful approaches to forecasting and creating a needed .pdf
 
What are the major challenges for managing health care information t.pdf
What are the major challenges for managing health care information t.pdfWhat are the major challenges for managing health care information t.pdf
What are the major challenges for managing health care information t.pdf
 
Using the Tustin equivalence with the prewarping method, fnd the dis.pdf
Using the Tustin equivalence with the prewarping method, fnd the dis.pdfUsing the Tustin equivalence with the prewarping method, fnd the dis.pdf
Using the Tustin equivalence with the prewarping method, fnd the dis.pdf
 
Use Java programming Thank you very much Programing requirement for.pdf
Use Java programming Thank you very much Programing requirement for.pdfUse Java programming Thank you very much Programing requirement for.pdf
Use Java programming Thank you very much Programing requirement for.pdf
 
Translate the following C++ code segment into Mic-1 IJVM machine lan.pdf
Translate the following C++ code segment into Mic-1 IJVM machine lan.pdfTranslate the following C++ code segment into Mic-1 IJVM machine lan.pdf
Translate the following C++ code segment into Mic-1 IJVM machine lan.pdf
 
Transactions On September 1 of the current year, Joy Tucker estab.pdf
Transactions On September 1 of the current year, Joy Tucker estab.pdfTransactions On September 1 of the current year, Joy Tucker estab.pdf
Transactions On September 1 of the current year, Joy Tucker estab.pdf
 
To correct the vision of a person who is nearR sighted, we use a div.pdf
To correct the vision of a person who is nearR sighted, we use a div.pdfTo correct the vision of a person who is nearR sighted, we use a div.pdf
To correct the vision of a person who is nearR sighted, we use a div.pdf
 
The height of the water in the tank pictured below 7 ft. The tank is.pdf
The height of the water in the tank pictured below 7 ft. The tank is.pdfThe height of the water in the tank pictured below 7 ft. The tank is.pdf
The height of the water in the tank pictured below 7 ft. The tank is.pdf
 
Suppose that a classroom has 4 light bulbs. The probability that eac.pdf
Suppose that a classroom has 4 light bulbs. The probability that eac.pdfSuppose that a classroom has 4 light bulbs. The probability that eac.pdf
Suppose that a classroom has 4 light bulbs. The probability that eac.pdf
 
Security Standards Please respond to the following A number of .pdf
Security Standards Please respond to the following A number of .pdfSecurity Standards Please respond to the following A number of .pdf
Security Standards Please respond to the following A number of .pdf
 
Real Estate Finance essay questionWhy did observers at first belie.pdf
Real Estate Finance essay questionWhy did observers at first belie.pdfReal Estate Finance essay questionWhy did observers at first belie.pdf
Real Estate Finance essay questionWhy did observers at first belie.pdf
 
Provide your key points of Setup Reducation Provide your key points.pdf
Provide your key points of Setup Reducation  Provide your key points.pdfProvide your key points of Setup Reducation  Provide your key points.pdf
Provide your key points of Setup Reducation Provide your key points.pdf
 
Please help. C++ The program is an interactive program th.pdf
Please help. C++ The program is an interactive program th.pdfPlease help. C++ The program is an interactive program th.pdf
Please help. C++ The program is an interactive program th.pdf
 
please answer this case using down picturein business ethics emplo.pdf
please answer this case using down picturein business ethics emplo.pdfplease answer this case using down picturein business ethics emplo.pdf
please answer this case using down picturein business ethics emplo.pdf
 
Al has three times as much money as Bill. Bill has two times as much.pdf
Al has three times as much money as Bill. Bill has two times as much.pdfAl has three times as much money as Bill. Bill has two times as much.pdf
Al has three times as much money as Bill. Bill has two times as much.pdf
 
A SURVEY is a tool used to collect information about a population. L.pdf
A SURVEY is a tool used to collect information about a population. L.pdfA SURVEY is a tool used to collect information about a population. L.pdf
A SURVEY is a tool used to collect information about a population. L.pdf
 

Recently uploaded

How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 

Recently uploaded (20)

How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 

Were writing code for a project that dynamically allocates an arra.pdf

  • 1. We're writing code for a project that dynamically allocates an array stack. My other code works, but my Min(), Max(), and Peek() functions fail. I've provided my code for those functions at the end of the post. main.cpp _________________________________________ #include #include #include #include #include "stack.h" using namespace std; int main(int argc, char* argv[]) { ifstream inputs; // Input file for commands char op; // Hold operation and optional char input int value; // Value input from file string comment; // Holds comment from file Stack* sPtr = NULL; // Will point to TemplateQ object // Output usage message if one input file name is not provided if (argc != 2) { cout << "Usage: project03 "; return 1; } // Attempt to open input file -- terminate if file does not open inputs.open(argv[1]); if (!inputs) { cout << "Error - unable to open input file" << endl; return 1; } // Input and echo header comment from file getline(inputs, comment); // Input and echo the comment appearing in the test file
  • 2. cout << endl << '#' << comment << endl; // Process commands from input file inputs >> op; // Attempt to input first command while (inputs) { switch (op) // Process operation input from file { case '#': // Test file comment getline(inputs, comment); // Input and echo the comment appearing in the test file cout << '#' << comment << endl; break; case 'c': // Parameterized Constructor inputs >> value; cout << endl << "Stack(" << value << ")"; try { sPtr = new Stack(value); // Attempt to create a stack object with array size value cout << " -- completed" << endl; } catch ( std::bad_alloc ) { cout << "Failed : Terminating now..." << endl; return 1; } break; case '+': // Push inputs >> value; cout << "Push(" << value << ") -- "; try { sPtr->Push(value); cout << "completed";
  • 3. } catch (StackFull) { cout << "Failed Full Stack"; } catch (...) { cout << "operation failed" << endl; } cout << endl; break; case '-': // Pop cout << "Pop() -- "; try { sPtr->Pop(); cout << "completed"; } catch (StackEmpty) { cout << "Failed Empty Stack"; } catch (...) { cout << "operation failed" << endl; } cout << endl; break; case 'f': // IsFull cout << "IsFull() -- "; try { if (sPtr->IsFull()) cout << "true"; else
  • 4. cout << "false"; } catch ( ... ) { cout << "operation failed"; } cout << endl; break; case 'e': // IsEmpty cout << "IsEmpty() -- "; try { if (sPtr->IsEmpty()) cout << "true"; else cout << "false"; } catch ( ... ) { cout << "operation failed"; } cout << endl; break; case 'm': // Make Empty cout << "MakeEmpty() -- "; try { sPtr->MakeEmpty(); cout << "completed" << endl; } catch (...) { cout << "operation failed" << endl; }
  • 5. break; case 'p': // Print Stack cout << "Print() -- "; sPtr->Print(); break; case 't': // Top of Stack try { cout << "Top() -- " << sPtr->Top() << endl; } catch (StackEmpty) { cout << "Top() -- Failed Empty Stack" << endl; } catch (...) { cout << "operation failed" << endl; } break; case '>': // Max value within Stack try { cout << "Max() -- " << sPtr->Max() << endl; } catch (StackEmpty) { cout << "Max() -- Failed Empty Stack" << endl; } catch (...) { cout << "operation failed" << endl; } break; case '<': // Min value within Stack
  • 6. try { cout << "Min() -- " << sPtr->Min() << endl; } catch (StackEmpty) { cout << "Min() -- Failed Empty Stack" << endl; } catch (...) { cout << "operation failed" << endl; } break; case '?': // Peek(n) Stack inputs >> value; try { cout << "Peek(" << value << ") -- " << sPtr->Peek(value) << endl; } catch (StackInvalidPeek) { cout << "Peek(" << value << ") -- Failed Invalid Peek" << endl; } catch (...) { cout << "operation failed" << endl; } break; case 's': // Size of Stack cout << "Size() -- "; try { cout << sPtr->Size() << endl; }
  • 7. catch (...) { cout << "operation failed" << endl; } break; case 'z': // Capacity of Stack cout << "Capacity() -- "; try { cout << sPtr->Capacity() << endl; } catch (...) { cout << "operation failed" << endl; } break; case 'd': // Destructor cout << "~Stack() -- "; try { delete sPtr; sPtr = NULL; cout << "completed" << endl << endl; } catch (...) { cout << "operation failed" << endl << endl; } break; default: // Error cout << "Error - unrecognized operation '" << op << "'" << endl; cout << "Terminating now..." << endl; return 1; break; }
  • 8. inputs >> op; // Attempt to input next command } return 0; } // End main() _______________________________________________________________ stack.h _________________________________ // // The comments have been updated, but there have been no changes to the code. // // Specification file for Stack class, a stack of integers implemented // as an array whose size can be increased to store more data. // // ***** DO NOT MODIFY OR SUBMIT THIS FILE ***** // #include using namespace std; #ifndef STACK_H #define STACK_H class StackEmpty { // Exception class - throw an object of this type when stack is empty // Hint: there is no code for exception classes }; class StackFull { // Exception class - throw an object of this type when stack is full }; class StackInvalidPeek { // Exception class - throw an object of this type when invalid peek position is used };
  • 9. class Stack // Models stack of integers ADT implemented as a dynamically allocated array { private: int* array; // Points to the stack array int num; // Holds max number of elements that may be stored in stack array int top; // Holds the index of the top data value stored on the stack void Resize(int n); // Attempts to increase size of stack array to 2*num and then push n onto stack // If unable to resize, throw StackFull exception public: Stack(int n); // Parameterized constructor dynamically allocates an empty stack array // that may hold no more than n elements and initializes other private variables ~Stack(); // Destructor deallocates all dynamically-allocated memory // associated with the object void Push(int n); // Pushes integer n onto top of stack. If stack is full, attempts to // resize stack and then push n. If unable to resize, throws StackFull exception. void Pop(); // Removes top integer from stack // If stack is empty, throws StackEmpty exception bool IsEmpty() const; // Returns true if stack is empty; false otherwise bool IsFull() const; // Returns true if stack is full; false otherwise void MakeEmpty(); // Removes all items from stack leaving an empty, but usable stack with capacity num // If stack is already empty, MakeEmpty() does nothing int Top() const; // Returns value of top integer on stack WITHOUT modifying the stack // If stack is empty, throws StackEmpty exception int Size() const; // Returns number of items on stack WITHOUT modifying the stack int Max() const; // Returns value of largest integer on stack WITHOUT modifying the stack // If stack is empty, throws StackEmpty
  • 10. int Min() const; // Returns value of smallest integer on stack WITHOUT modifying the stack // If stack is empty, throws StackEmpty int Peek(unsigned int n) const; // Returns stack value n levels down from top of stack. Peek(0) = Top() // If position n does not exist, throws StackInvalidPeek int Capacity() const; // Returns total num of elements that maybe stored in stack array /******* DO NOT MODIFY ANY OF THE CODE FOR PRINT() *******/ /****** DO NOT PLACE A COPY OF PRINT() CODE IN STACK.CPP!!! *******/ void Print() const // Writes stack contents to stdout, separated by a space, followed by endl { int index = top; cout << "Top { "; while (index != -1) { cout << array[index] << " "; index--; } cout << "} Bottom" << endl; } // End Print() }; // End Class Stack #endif _____________________________________________________________ .txt file ______________________________ # p03input3.txt -- Test Min(), Max(), Peek(...) # Test Min() and Max() c 8 < > + 5
  • 11. + 30 + 15 + 20 + 50 + 10 p < > + 20 + 50 + 5 p < > - - - p < > - - - p < > d # Test Peek(n) c 8 ? 0 + 5 + 30 + 15 + 20 + 50
  • 12. + 10 p ? 0 ? 1 ? 2 ? 3 ? 4 ? 5 ? 6 p - - - p ? 0 ? 1 ? 2 ? 5 p - - - p ? 0 ? 1 ? 2 ? 3 ? 4 ? 5 + 25 + 9 + 8 p ? 0 ? 1 ? 2
  • 13. ? 3 ? 4 ? 5 d ____________________________________________________________ Error Messages _________________________________________________________ Here's my code. stack.cpp start of p3input3.txt Min ( ) -- Min ( ) -- Min ( ) -- Min() -- | Min) -- 5 in) -5 | Min() -- 5 Min) -- 5 FAILED Incorrect outputs PASSED No memory leak detected FAILED p031nput3.txt End of p31nput3.txt Solution #include #include #include #include #include "stack.h" using namespace std; int main(int argc, char* argv[]) { ifstream inputs; // Input file for commands char op; // Hold operation and optional char input int value; // Value input from file string comment; // Holds comment from file Stack* sPtr = NULL; // Will point to TemplateQ object // Output usage message if one input file name is not provided if (argc != 2) { cout << "Usage: project03 "; return 1; }
  • 14. // Attempt to open input file -- terminate if file does not open inputs.open(argv[1]); if (!inputs) { cout << "Error - unable to open input file" << endl; return 1; } // Input and echo header comment from file getline(inputs, comment); // Input and echo the comment appearing in the test file cout << endl << '#' << comment << endl; // Process commands from input file inputs >> op; // Attempt to input first command while (inputs) { switch (op) // Process operation input from file { case '#': // Test file comment getline(inputs, comment); // Input and echo the comment appearing in the test file cout << '#' << comment << endl; break; case 'c': // Parameterized Constructor inputs >> value; cout << endl << "Stack(" << value << ")"; try { sPtr = new Stack(value); // Attempt to create a stack object with array size value cout << " -- completed" << endl; } catch ( std::bad_alloc ) { cout << "Failed : Terminating now..." << endl; return 1;
  • 15. } break; case '+': // Push inputs >> value; cout << "Push(" << value << ") -- "; try { sPtr->Push(value); cout << "completed"; } catch (StackFull) { cout << "Failed Full Stack"; } catch (...) { cout << "operation failed" << endl; } cout << endl; break; case '-': // Pop cout << "Pop() -- "; try { sPtr->Pop(); cout << "completed"; } catch (StackEmpty) { cout << "Failed Empty Stack"; } catch (...) { cout << "operation failed" << endl; } cout << endl;
  • 16. break; case 'f': // IsFull cout << "IsFull() -- "; try { if (sPtr->IsFull()) cout << "true"; else cout << "false"; } catch ( ... ) { cout << "operation failed"; } cout << endl; break; case 'e': // IsEmpty cout << "IsEmpty() -- "; try { if (sPtr->IsEmpty()) cout << "true"; else cout << "false"; } catch ( ... ) { cout << "operation failed"; } cout << endl; break; case 'm': // Make Empty cout << "MakeEmpty() -- ";
  • 17. try { sPtr->MakeEmpty(); cout << "completed" << endl; } catch (...) { cout << "operation failed" << endl; } break; case 'p': // Print Stack cout << "Print() -- "; sPtr->Print(); break; case 't': // Top of Stack try { cout << "Top() -- " << sPtr->Top() << endl; } catch (StackEmpty) { cout << "Top() -- Failed Empty Stack" << endl; } catch (...) { cout << "operation failed" << endl; } break; case '>': // Max value within Stack try { cout << "Max() -- " << sPtr->Max() << endl; } catch (StackEmpty)
  • 18. { cout << "Max() -- Failed Empty Stack" << endl; } catch (...) { cout << "operation failed" << endl; } break; case '<': // Min value within Stack try { cout << "Min() -- " << sPtr->Min() << endl; } catch (StackEmpty) { cout << "Min() -- Failed Empty Stack" << endl; } catch (...) { cout << "operation failed" << endl; } break; case '?': // Peek(n) Stack inputs >> value; try { cout << "Peek(" << value << ") -- " << sPtr->Peek(value) << endl; } catch (StackInvalidPeek) { cout << "Peek(" << value << ") -- Failed Invalid Peek" << endl; } catch (...) { cout << "operation failed" << endl;
  • 19. } break; case 's': // Size of Stack cout << "Size() -- "; try { cout << sPtr->Size() << endl; } catch (...) { cout << "operation failed" << endl; } break; case 'z': // Capacity of Stack cout << "Capacity() -- "; try { cout << sPtr->Capacity() << endl; } catch (...) { cout << "operation failed" << endl; } break; case 'd': // Destructor cout << "~Stack() -- "; try { delete sPtr; sPtr = NULL; cout << "completed" << endl << endl; } catch (...) { cout << "operation failed" << endl << endl;
  • 20. } break; default: // Error cout << "Error - unrecognized operation '" << op << "'" << endl; cout << "Terminating now..." << endl; return 1; break; } inputs >> op; // Attempt to input next command } return 0; }