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...
The program I wrote has a few memory leaks. My assignment is to just fix the memory leaks. Here's the code: bool RemoveTail() { if (tail == nullptr) return false; Node *ptr = tail; if(tail->prev != nullptr) { tail = tail->prev; tail->next = nullptr; } else { tail = nullptr; head = nullptr; } delete ptr; ptr = nullptr; length--; return true; } bool RemoveAt(const unsigned int index) { if(index >= length || index < 0) return false; unsigned int...
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...
What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...