Question: 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

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 dont 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  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 *** "; 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 *** "; } Stack & Stack::operator=(const Stack & original) { // write code here, refer copy construct } #endif 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!