Question
C++ Exercise (DATA STRUCTURES). I just need a code on file Stack.cpp, input.txt, & output.txt because this question has 5 files. Description: This program demonstrates
C++ Exercise (DATA STRUCTURES). I just need a code on file Stack.cpp, input.txt, & output.txt because this question has 5 files.
Description: This program demonstrates a dynamic stack ADT. The program reads commands from a text file, outputs to the screen, and then writes results to an output file.The
input.txt file is the input file to be used for this program. It contains commands that the program will read and then execute. The cout << statements are used to show the results of commands.These statements are not necessary, because the output can be viewed in the output file.
- I already code main.cpp and Stack.h. Here is the question and code for 2 files:
//======
Stack.h
#include
#include
#include
using namespace std;
Struct Node;
Class Stack
{
private :
Node * top;
public:
Stack();
~Stack();
bool isFull();
bool isEmpty();
void push(int id);
void pop(int & id);
int returnTop();
};
Struct Node
{
int id;
Node * next;
};
//=======
main.cpp
#include
#include
#include
using namespace std;
#include "Stack.h"
int main()
{
ifstream inFile; // file containing operations
ofstream outFile; // file containing output
string inFileName; // input file external name
string outFileName; // output file external name
string outputLabel;
string command; // operation to be executed
Stack stack;
int numCommands;
// Prompt for file names, read file names, and prepare files
cout << "Enter name of input file: ";
cin >> inFileName;
inFile.open(inFileName.c_str());
cout << "Enter name of output file: ";
cin >> outFileName;
outFile.open(outFileName.c_str());
cout << "Enter name of test run #: ";
cin >> outputLabel;
outFile << outputLabel << endl;
// Read the first command from the input file
inFile >> command;
numCommands = 0;
while (command != "quit")
{
if (command == "push")
{
inFile >> item;
stack.push(item);
// Output statement shows item pushed on stack
cout << " " << item << " pushed on the stack - ";
}
else if (command == "pop")
{
stack.pop();
// Output statement shows item popped off stack
cout << " " << item << " popped off the stack - ";
}
else if (command == "top")
{
item = stack.returnTop();
outFile<< "Top element is " << item << endl;
// Output statement shows the top item on the stack
cout << " " << item << " is the top stack element - ";
}
else if (command == "isEmpty")
{
if (stack.isEmpty())
{
outFile << "Stack is empty." << endl;
// Output statements state whether the stack is empty
cout << " The stack is empty - ";
}
else
{
outFile << "Stack is not empty." << endl;
cout << " The stack is not empty - ";
}
}
else if (command == "isFull")
{
if (stack.isFull())
{
outFile << "Stack is full." << endl;
// Output statements state whether the stack is full
cout << " The stack is full - ";
}
else
{
outFile << "Stack is not full." << endl;
cout << " The stack is not full - ";
}
}
numCommands++;
cout << " Command number " << numCommands << " completed."
<< endl;
inFile >> command;
}
cout << " Received \"quit\" command. "
<< "Testing completed. ";
inFile.close();
outFile.close();
return 0;
}
// ======================================================
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started