Write a program that opens a text file called input.txt and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file called output.txt. The order of the characters saved in the second file should be the reverse of their order in the first file. We don’t know the text file length.
Input.txt
This is a file is for test
output.txt
tset rof si elif a si sihT
Directions:
Finish
Stack & Stack::operator=(const Stack & original)
{
// write code here, refer copy construct
}
// File name: stack.h
#include <iostream>
using namespace std;
typedef char StackElement;
class Stack
{
public:
Stack();
Stack(const Stack & original);
~Stack();
bool empty() const;
void push(const StackElement & value);
void display(ostream & out) const;
StackElement top() const;
void pop();
Stack & operator=(const Stack & original);
private:
class Node
{
public:
StackElement data;
Node *next;
Node (StackElement value, Node *link = 0)
{
data = value;
next = link;
}
};
typedef Node *NodePointer;
NodePointer myTop;
};
Stack::Stack()
{
myTop = 0;
myTop = NULL;
}
Stack::Stack(const Stack & original)
{
myTop = 0;
if(!original.empty())
{
myTop = new Stack::Node(original.top());
Stack::NodePointer lastPtr = myTop,
origPtr = original.myTop->next;
while(origPtr != 0)
{
lastPtr->next = new Stack::Node(origPtr->data);
lastPtr = lastPtr->next;
origPtr = origPtr->next;
}
}
}
Stack::~Stack()
{
Stack::NodePointer currPtr = myTop, nextPtr;
while (currPtr != 0)
{
nextPtr = currPtr->next;
delete currPtr;
currPtr = nextPtr;
}
}
bool Stack::empty() const
{
return (myTop == 0);
}
void Stack::push(const StackElement & value)
{
myTop = new Stack::Node(value, myTop);
}
void Stack::display(ostream & out) const
{
Stack::NodePointer ptr;
for (ptr = myTop; ptr != 0; ptr = ptr->next)
out << ptr->data << endl;
}
StackElement Stack::top() const
{
if (!empty())
return (myTop->data);
else
{
cerr << "*** Stack is empty "
"--- returning garbage value ***\n";
return *(new StackElement);
}
}
void Stack::pop()
{
if (!empty())
{
Stack::NodePointer ptr = myTop;
myTop = myTop->next;
delete ptr;
}
else
cerr << "*** Stack is empty -- can't remove a value ***\n";
}
Stack & Stack::operator=(const Stack & original)
{
// write code here, refer copy construct
}
#endif







Editable code:
CharStack.h
#include <iostream>
using namespace std;
typedef char StackElement;
class Stack
{
public:
Stack();
Stack(const Stack & original);
~Stack();
bool empty() const;
void push(const StackElement & value);
void display(ostream & out) const;
StackElement top() const;
char pop();
Stack & operator=(const Stack & original);
private:
class Node
{
public:
StackElement data;
Node *next;
Node(StackElement value, Node *link = 0)
{
data = value;
next = link;
}
};
typedef Node *NodePointer;
NodePointer myTop;
};
Stack::Stack()
{
myTop = 0;
myTop = NULL;
}
Stack::Stack(const Stack & original)
{
myTop = 0;
if (!original.empty())
{
myTop = new Stack::Node(original.top());
Stack::NodePointer lastPtr = myTop,
origPtr = original.myTop->next;
while (origPtr != 0)
{
lastPtr->next = new Stack::Node(origPtr->data);
lastPtr = lastPtr->next;
origPtr = origPtr->next;
}
}
}
Stack::~Stack()
{
Stack::NodePointer currPtr = myTop, nextPtr;
while (currPtr != 0)
{
nextPtr = currPtr->next;
delete currPtr;
currPtr = nextPtr;
}
}
bool Stack::empty() const
{
return (myTop == 0);
}
void Stack::push(const StackElement & value)
{
myTop = new Stack::Node(value, myTop);
}
void Stack::display(ostream & out) const
{
Stack::NodePointer ptr;
for (ptr = myTop; ptr != 0; ptr = ptr->next)
out << ptr->data << endl;
}
StackElement Stack::top() const
{
if (!empty())
return (myTop->data);
else
{
cerr << "*** Stack is empty "
"--- returning garbage value ***\n";
return *(new StackElement);
}
}
//*** as changed into char
char Stack::pop()
{
char c;
if (!empty())
{
Stack::NodePointer ptr = myTop;
myTop = myTop->next;
c = ptr->data;
delete ptr;
}
else
cerr << "*** Stack is empty -- can't remove a value ***\n";
return c;
}
// Gets error here.. so , make as comment line
/*Stack & Stack::operator=(const Stack & original)
{
// write code here, refer copy construct
}*/
Main.cpp
#include <iostream>
#include <fstream>
#include "CharStack.h"
using namespace std;
int main()
{
Stack stack;
char ch;
ifstream inputFile;
ofstream outputFile;
inputFile.open("in.txt");
if (!inputFile)
{
cout << "The file cannot be opened.\n";
exit(1);
}
while (inputFile.get(ch))
{
stack.push(ch);
}
inputFile.close();
outputFile.open("out.txt");
if (!outputFile)
{
cout << "The file cannot be opened.\n";
exit(1);
}
while (!stack.empty())
{
ch=stack.pop();
outputFile.put(ch);
}
outputFile.close();
system("pause");
return 0;
}
Write a program that opens a text file called input.txt and reads its contents into a...
// thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...
My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is not empty s1 (size 4): 4 6 2 17 s1 is not empty Testing copy constructor s1 (size 4): 4 6 2 17 s2 (size 4): 4 6 2 17 Testing clear() s1 (size 0): s2 (size 4): 0 1477251200 1477251168 1477251136 s3 (size 4): 28 75 41 36 Testing assignment operator s3 (size 4): 28 75 41 36 s4 (size 4): 28 75...
My question is pretty simple, I just want to know how to call my operator== function in Stack.cpp using a list function. Here is what I meant. This is my Stack.h file: class Stack { public: /**constructors and destructors*/ Stack(); Stack(const Stack &S); ~Stack(); void pop(); void push(int data); bool operator==(const Stack &S); [ .......] private: List<int> stack; }; Here is my List.h file: template<class listitem> class List { private: struct...
// Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...
1. Here are codes to define a stack class based on dynamic array, please complete the copy constructor //--- Definition of Stack copy constructor Stack::Stack(const Stack & original) : myCapacity(original.myCapacity), myTop(original.myTop) { //--- Get new array for copy myArray = new(nothrow) StackElement[myCapacity]; if (myArray != 0) // check if memory available // copy original's array member into this new array { // Please complete the function here } else { cerr << "*Inadequate memory to allocate...
I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...
Using the below files, Write a program that reads a document containing endnotes indicated in this manner, collects them in a queue, and prints them on the screen. For this lab, you will create a text file called sample.txt and put the following paragraph in it. This part is the beginning. {This part is the footnote.} This part is the end. /* Queue.h contains the declaration of class Queue. Basic operations: Constructor: Constructs an empty queue empty: Checks if a...
In this assignment, you will implement a sort method on
singly-linked and doubly-linked lists.
Implement the following sort member function on a singly-linked
list:
void
sort(bool(*comp)(const
T &, const
T &) = defaultCompare);
Implement the following sort member function on a doubly-linked
list:
void
sort(bool(*comp)(const
T &, const
T &) = defaultCompare);
The sort(…) methods take as a parameter a comparator function,
having a default assignment of defaultCompare, a
static function defined as follows:
template <typename T>
static bool defaultCompare(const...
in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...