Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im stumped on how to fix this partial code. Please fill in the \"TO DO\" comment

ID: 3814539 • Letter: I

Question

Im stumped on how to fix this partial code. Please fill in the "TO DO" comments at the bottom of the partial program.

/**
* Stack.cpp - This file uses the C++ STL stack API and implements a
* helper function called reverseStack that will reverse the
* provided stack.
*
* TODO: Include your name and course number here.
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <stack>

using namespace std;

template <class Object>
void printStack(stack<Object> inStack);

template <class Object>
stack<Object> reverseStack(stack<Object> inStack);

int main()
{
stack<string> inStack;
stack<string> resultStack;

inStack.push("844");
inStack.push("-");
inStack.push("14");
inStack.push("*");
inStack.push("-9");
inStack.push("/");
inStack.push("30");

resultStack = reverseStack(inStack);

cout << "The contents of the stack:" << endl;
printStack(inStack);

cout << "The reverseStack of the stack:" << endl;
printStack(resultStack);

cout << "** Press any key to continue **";
getchar();

return 0;
}

template <class Object>
void printStack(stack<Object> inStack)
{
while (!inStack.empty())
{
// Print the top item that is on the stack
cout << " " << inStack.top() << endl;

// Pop the item off of the stack
inStack.pop();
}

cout << endl;

return;
}

template <class Object>
stack<Object> reverseStack(stack<Object> inStack)
{
// TODO: Implement the details for the reverse function.

// TODO: Return the reverse of inStack instead of inStack.
return inStack;
}

Explanation / Answer

#include <iostream>
#include <cstdio>
#include <string>
#include <stack>
using namespace std;
template <class Object>
void printStack(stack<Object> inStack);
template <class Object>
stack<Object> reverseStack(stack<Object> inStack);
int main()
{
stack<string> inStack;
stack<string> resultStack;
inStack.push("844");
inStack.push("-");
inStack.push("14");
inStack.push("*");
inStack.push("-9");
inStack.push("/");
inStack.push("30");
resultStack = reverseStack(inStack);
cout << "The contents of the stack:" << endl;
printStack(inStack);
cout << "The reverseStack of the stack:" << endl;
printStack(resultStack);
cout << "** Press any key to continue **";
getchar();
return 0;
}
template <class Object>
void printStack(stack<Object> inStack)
{
while (!inStack.empty())
{
// Print the top item that is on the stack
cout << " " << inStack.top() << endl;
// Pop the item off of the stack
inStack.pop();
}
cout << endl;
return;
}
template <class Object>
stack<Object> reverseStack(stack<Object> inStack)
{
// TODO: Implement the details for the reverse function.
// TODO: Return the reverse of inStack instead of inStack.
stack<string> revStack;
while (!inStack.empty())
{
revStack.push(inStack.top());
inStack.pop();
}
return revStack;
}

Output:

sh-4.2$ main                                                                                                                                                                                                                                                             

The contents of the stack:                                                                                                                                                                                                                                               

30                                                                                                                                                                                                                                                                      

/                                                                                                                                                                                                                                                                       

-9                                                                                                                                                                                                                                                                      

*                                                                                                                                                                                                                                                                       

14                                                                                                                                                                                                                                                                      

-                                                                                                                                                                                                                                                                       

844                                                                                                                                                                                                                                                                     

                                                                                                                                                                                                                                                                         

The reverseStack of the stack:                                                                                                                                                                                                                                           

844                                                                                                                                                                                                                                                                     

-                                                                                                                                                                                                                                                                       

14                                                                                                                                                                                                                                                                      

*                                                                                                                                                                                                                                                                       

-9                                                                                                                                                                                                                                                                      

/                                                                                                                                                                                                                                                                       

30