In: Computer Science
In C++ Using LinkedQueue structure solve to. (Gaddis) 10. File Filter p. 1119 Chap 18.
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.
#include "QueueInterface.h"
#include "Node.h"
#include "PrecondViolatedExcep.h"
template<class ItemType>
class LinkedQueue : public QueueInterface<ItemType> {
private:
// The queue is implemented as a chain of linked nodes that has
// two external pointers, a head pointer for the front of the queue // and a tail pointer for the back of the queue.
Node<ItemType>* backPtr;
Node<ItemType>* frontPtr;
public:
LinkedQueue();
LinkedQueue(const LinkedQueue& aQueue); ~LinkedQueue();
bool isEmpty() const;
bool enqueue(const ItemType& newEntry); bool dequeue();
/** @throw PrecondViolatedExcep if the queue is empty */
ItemType peekFront() const throw(PrecondViolatedExcep); }; // end LinkedQueue
=======================
#pragma once
QueueInterface.cpp
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.
/** Listing 13-1.
@file QueueInterface.h */
#ifndef _QUEUE_INTERFACE
#define _QUEUE_INTERFACE
template<class ItemType>
class QueueInterface
{ public:
/** Sees whether this queue is empty.
@return True if the queue is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to the back of this queue.
@post If the operation was successful, newEntry is at the
back of the queue.
@param newEntry The object to be added as a new entry. @return True if the addition is successful or false if not. */
virtual bool enqueue(const ItemType& newEntry) = 0;
/** Removes the front of this queue.
@post If the operation was successful, the front of the queue
has been removed.
@return True if the removal is successful or false if not. */
virtual bool dequeue() = 0;
/** Returns the front of this queue.
@pre The queue is not empty.
@post The front of the queue has been returned, and the
queue is unchanged.
@return The front of the queue. */
virtual ItemType peekFront() const = 0; }; // end QueueInterface
#endif
======================
#ifndef _NODE
#define _NODE
template<class ItemType>
class Node
{
private:
ItemType item; // A data item
Node<ItemType>* next; // Pointer to next node
public:
Node();
Node(const ItemType& anItem);
Node(const ItemType& anItem, Node<ItemType>* nextNodePtr);
void setItem(const ItemType& anItem);
void setNext(Node<ItemType>* nextNodePtr);
ItemType getItem() const ;
Node<ItemType>* getNext() const ;
}; // end Node
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.
/** @file Node.cpp
Listing 4-2 */
#include <cstddef>
template<class ItemType>
Node<ItemType>::Node() : next(nullptr)
{
} // end default constructor
template<class ItemType>
Node<ItemType>::Node(const ItemType& anItem) : item(anItem), next(nullptr)
{
} // end constructor
template<class ItemType>
Node<ItemType>::Node(const ItemType& anItem, Node<ItemType>* nextNodePtr) :
item(anItem), next(nextNodePtr)
{
} // end constructor
template<class ItemType>
void Node<ItemType>::setItem(const ItemType& anItem)
{
item = anItem;
} // end setItem
template<class ItemType>
void Node<ItemType>::setNext(Node<ItemType>* nextNodePtr)
{
next = nextNodePtr;
} // end setNext
template<class ItemType>
ItemType Node<ItemType>::getItem() const
{
return item;
} // end getItem
template<class ItemType>
Node<ItemType>* Node<ItemType>::getNext() const
{
return next;
} // end getNext
#endif
=========================
PrecondViolatedExcep.h
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.
/** Listing 7-5.
@file PrecondViolatedExcep.h */
#ifndef _PRECOND_VIOLATED_EXCEP
#define _PRECOND_VIOLATED_EXCEP
#include <stdexcept>
#include <string>
using namespace std;
class PrecondViolatedExcep : public logic_error
{
public:
PrecondViolatedExcep(const string& message = "");
}; // end PrecondViolatedExcep
#endif
PrecondViolatedExcep.cpp
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.
/** Listing 7-6.
@file PrecondViolatedExcep.cpp */
#include "PrecondViolatedExcep.h"
PrecondViolatedExcep::PrecondViolatedExcep(const string& message): logic_error("Precondition Violated Exception: " + message)
{
} // end constructor
// End of implementation file.
CODE :
# Content of DynamicQueue.h
#ifndef DYNAMICQUEUE_H
#define DYNAMICQUEUE_H
#include <iostream>
#include <new> //for bad_alloc
using namespace std;
template<class T>
class DynamicQueue
{
private:
    struct Node
        {
        T value;
        Node *next;
    };
    Node *frontNode;
    Node *rearNode;
public:
    //constructor
    DynamicQueue()
        {
        //marking empty queue
        frontNode = NULL;
    }
    //enqueue function
    void enqueue(T val)
        {
        try{
            //create new node
            Node *newNode = new Node;
            
            newNode->value = val;
            //make newNode to be last node in queue
            newNode->next = NULL;
            //first check if queue is empty
            if(isEmpty())
                        {
                //make front and rear to point
                //to newNode thus making it
                // first and last
                frontNode = newNode;
                rearNode = newNode;
            }
            //otherwise if queue not empty
            else
                        {
                //link current rear to newNode
                //and update rear to point to new Node
                rearNode->next = newNode;
                rearNode = newNode;
            }
        }
        catch(bad_alloc)
                {
            cout << "Exception caught! Bad allocation!\n";
        }
    }
    //dequeue function
    void dequeue(T &var){
        //first check if queue is empty
        if(isEmpty())
                {
            cout << "Queue is empty! Cannot dequeue!\n";
        }
        else
                {
            //save value of current front
            //to argument variable
            var = frontNode->value;
            //create temp Node to point to current front
            Node *temp = frontNode;
            //make front point to next node in line
            frontNode = temp->next;
            //delete previous front, which is now
            //pointed by temp
            delete temp;
        }
    }
    //determines whether queue is empty or not
    bool isEmpty()
        {
        //check if front points to null
        if(frontNode == NULL)
            return true;
        //otherwise return false
        return false;
    }
    //destructor
    ~DynamicQueue()
        {
        //dummy variable
        T dummy;
        //dequeue until queue is empty
        while(!isEmpty()){
            dequeue(dummy);
        }
    }
};
#endif
# Content of Main.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
#include "DynamicQueue.h"
using namespace std;
int main()
{
    //create fstream object and open input file
    fstream inputFile;
    inputFile.open("inputFile.txt", ios::in);
    //create Dynamic stack object of chars
    DynamicQueue<char> charQueue;
    char ch;
    //read input file char by char
    cout << "Now reading:\n";
    while(inputFile.get(ch)){
        cout << ch;
        //for each iteration, enqueue char
        charQueue.enqueue(ch);
    }
    //create output file object
    fstream outputFile;
    outputFile.open("outputFile.txt", ios::out);
    //dequeue chars one by one from queue
    //until it is empty, and write to outputFile
    cout << "\nNow writing:\n";
    while(!charQueue.isEmpty())
        {
        //pop front char from Queue
        charQueue.dequeue(ch);
        //write it into file
        cout << (char)toupper(ch);
        outputFile << (char)toupper(ch);
    }
    return 0;
}
OUTPUT :
# InputFile.txt

#OutputFile.txt

# Console Output

Hope this helps!
Good Luck :)