Question

In: Computer Science

In C++ Using LinkedQueue structure solve to. (Gaddis) 10. File Filter p. 1119 Chap 18. 10.File...

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.

Solutions

Expert Solution

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


Related Solutions

Solve using PYTHON PROGRAMMING Write a script that reads a file “cars.csv”, into a pandas structure...
Solve using PYTHON PROGRAMMING Write a script that reads a file “cars.csv”, into a pandas structure and then print a. the first 3 rows and the last 3 of the dataset b. the 3 cars with the lowest average-mileage c. the 3 cars with the highest average-mileage. Solve using PYTHON PROGRAMMING
C++ (data structure using c++). please send me copyable file. Write a program and test a...
C++ (data structure using c++). please send me copyable file. Write a program and test a program that translates the following Bubble Sort algorithm to a bubblesort function. The function's prototype is, void bubblesort(int a[], int size); Bubble Sort The inner loop moves the largest element in the unsorted part of the array to the last position of the unsorted part of the array; the outer loop moves the last position of the unsorted part of the array. The Bubble...
C++ Tony Gaddis 8.11: Using Files-- String Selection Sort Modification Modify the selectionSort function presented in...
C++ Tony Gaddis 8.11: Using Files-- String Selection Sort Modification Modify the selectionSort function presented in this chapter so it sorts an array of strings instead of an array of ints. Test the function with a driver program that reads an integer, N, from standard input and then reads the first N strings from a file called href="asfunction:_root.doTutor,7,CPP">names. (Assume that N is less than or equal to 20.) Input Validation. If N read in from standard input is greater than...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Find each probability P(X; λ) using Table C in Appendix A. a. P(10; 7) b. P(9; 8) c. P(3; 4) Data from in Table C Appendix A
Find each probability P(X; λ) using Table C in Appendix A.a. P(10; 7)b. P(9; 8)c. P(3; 4) Data from in Table C Appendix A 
Code needed in C++, make changes to the file provided (18-1, has 3 files) Chapter 18...
Code needed in C++, make changes to the file provided (18-1, has 3 files) Chapter 18 Stacks and Queues ----------------------------------------------------------------------------------------------------- capacity is just 5 1. push 6 numbers on the stack 2. catch the overflow error in a catch block 3. pop one element, which means your capacity is now down to 4 4. push the element that was rejected earlier 5. verify your entire stack by popping to show the new numbers. IntStack.h #include <memory> using namespace std; class...
(C++) Create a data file and name it "input.txt". manually save 10 integers into the file....
(C++) Create a data file and name it "input.txt". manually save 10 integers into the file. Write a program to read the data and calculate the average of events and odds, separately. Print out the average values.
Using Matlab's FUNCTION FILE Solve this system of equations using Gauss Elimination in MATLAB by writing...
Using Matlab's FUNCTION FILE Solve this system of equations using Gauss Elimination in MATLAB by writing a Function file. 10y + z = 2 x + 3y – z = 6 2x + 4y + z = 5 Could you also provide me with a copiable function file of Matlab and the associated screenshot. Thank you!
For each of the following records, indicate the appropriate related file structure: master file, transaction file, reference file, or archive file. a. customer ledgers b. purchase orders c. ..
For each of the following records, indicate the appropriate related file structure: master file, transaction file, reference file, or archive file.a. customer ledgersb. purchase ordersc. list of authorized vendorsd. records related to prior pay periodse. vendor ledgersf. hours each employee has worked during the current pay periodg. tax tablesh. sales orders that have been processed and recorded 
SOLVE THE FOLLOWING USING STATISTICAL SOFTWARE R. SHOW YOUR CODE AND ANSWERS, USING AN RMD FILE...
SOLVE THE FOLLOWING USING STATISTICAL SOFTWARE R. SHOW YOUR CODE AND ANSWERS, USING AN RMD FILE (SHOW ANSWERS IN R MARKDOWN FORWAT WITH CODE AND ANSWERS) PROBLEM 1 A study of 400 glaucoma patients yields a sample mean of 140 mm and a sample standard deviation of 25 mm for the the following summaries for the systolic blood pressure readings. Construct the 95% and 99% confidence intervals for μ, the population average systolic blood pressure for glaucoma patients. PROBLEM 2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT