Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Bioinformatics Databases And Systems

Authors: Stanley I. Letovsky

1st Edition

1475784058, 978-1475784053

More Books

Students also viewed these Databases questions