Chapter 18 Programming challenges 9, 10 on textbook page 1118 You have to use qu
ID: 3596448 • Letter: C
Question
Chapter 18 Programming challenges 9, 10 on textbook page 1118
You have to use queue/stack class implementation (with push/pop, enqueue/dequeue) to accept input file of any size. Make sure you use dynamic stack/queue implementation to accept input file with any size.
you can use/modify LinkedList class or DLinkedList class in stack/queue implementation
listed below. check pp.551 (chapter 10.2) for character case change,
check pp. 675 (chapter 12.5, function "get", "put") for the character by character input to/from file Book posted in the link below
LinkedList and DLinkedlist at the bottom of the question.
Problems 9,10 listed below.
9. File Reverser
Write a program that opens a text file 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. The order of the characters saved in the second file should be the reverse of
their order in the first file.
10. File Filter
Write a program that opens a text file and reads its contents into a queue of characters.
The program should then dequeue each character, convert it to uppercase, and store
it in a second file.
Link to the pdf of the book listed below.
https://drive.google.com/drive/folders/0BxOc_idmYLSzeVFSblVuWE83NXM?usp=sharing
__________ LinkedList.cpp____________________________________________________
#include "LinkedList.h"
#include
using namespace std;
Node::Node()
{
next = 0;
}
int Node::GetData()
{
return data;
}
LinkedList::LinkedList()
{
length = 0;
head = 0;
tail = 0;
currentPos = 0;
}
int LinkedList::LengthIs()
{
return length;
}
void LinkedList::MakeEmpty()
{
Node *ptr = head;
Node *ptr2;
for(int i=0;i
{
ptr2 = ptr->next;
delete ptr;
ptr = ptr2;
}
length = 0;
head = tail = currentPos = 0;
}
void LinkedList::AddToTail(int item)
{
Node *ptr=new Node();
ptr->data = item;
if(length==0)
{
tail = ptr;
head = ptr;
length++;
return;
}
tail->next = ptr;
tail = ptr;
length++;
}
void LinkedList::AddToHead(int item)
{
Node *ptr = new Node;
ptr->data = item;
ptr->next = head;
head = ptr;
if(length==0) tail = ptr;
length++;
}
int LinkedList::SearchRank(int value)
{
int rank = 0;
Node *current = head;
while(current)
{
if(current->data == value) return rank;
rank++;
current = current->next;
}
return -1;
}
void LinkedList::DeleteFromHead()
{
if(length == 0) return;
Node *ptr = head->next;
if(currentPos==head) currentPos = 0;
delete head;
head = ptr;
length--;
if(length == 0) tail = 0;
}
void LinkedList::DeleteFromTail()
{
if(length==0) return;
else if (length==1)
{
delete head;
head = tail =currentPos = 0;
length--;
return;
}
Node *current = head;
for(int i=0;inext;
tail = current;
if(currentPos == current->next) currentPos = 0;
delete current->next;
current->next = 0;
length--;
return;
}
bool LinkedList::IsLast()
{
if(currentPos==tail) return 1;
else return 0;
}
void LinkedList::Reset()
{
currentPos = 0;
}
Node LinkedList::GetNext()
{
Node temp;
if(currentPos==0)
{
if(head) temp.data = head->data;
else temp.data = -1;
currentPos = head;
}
else
{
if(currentPos->next!=0)
{
temp.data = currentPos->next->data;
currentPos=currentPos->next;
}
else temp.data = -1;
}
return temp;
}
LinkedList::~LinkedList()
{
MakeEmpty();
}
void LinkedList::PrintAll()
{
Node *current = head;
while(current)
{
cout
current = current->next;
}
}
---------------------------------------------- DLinkedList.cpp-----------------------------------------------------------
-----------------------------------------------------------------------
Explanation / Answer
Here is the C++ for the above scenario:
#include <iostream>
#include <fstream>
#include "CharStack.h"
using namespace std;
int main()
{
CharStack stack;
char ch;
ifstream inputFile;
ofstream outputFile;
inputFile.open("letters.txt");
if (!inputFile)
{
cout << "The file cannot be opened. ";
exit(1);
}
while (inputFile.get(ch))
{
stack.push(ch);
}
inputFile.close();
outputFile.open("testoutput.txt");
if (!outputFile)
{
cout << "The file cannot be opened. ";
exit(1);
}
while (!stack.isEmpty())
{
stack.pop(ch);
outputFile.put(ch);
}
outputFile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.