File Filter C++
File Filter
Write a C++ 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.
Answer:
// File Filter #include <iostream> #include<fstream> #include<cstdlib> #include "DynQueue.h" using namespace std; int main() { // Create a file input stream object. fstream file1("file1.txt", ios::in); // Create a file output stream object. fstream file2("file2.txt", ios::out); // Creatre a queue to hold characters. Dynque<char> queue; char catchChar, ch; // Read all the characters from file #1 // and enqueue them. file1.get(catchChar); while (!file1.eof()) { queue.enqueue(toupper(catchChar)); file1.get(catchChar); } // Close file #1. file1.close(); // Dequeue each character and write it // to file #2. while (!queue.isEmpty()) { queue.dequeue(ch); file2.put(ch); } // Close file #2. file2.close(); // Re-open file #2. file2.open("file2.txt", ios::in); // Read each character from the file // and display it. file2.get(ch); while (!file2.eof()) { cout << ch; file2.get(ch); } // Close file #2. file2.close(); return 0; }
// Dynqueue class template template <class T> class Dynque { private: struct QueueNode { T value; QueueNode *next; }; QueueNode *front; QueueNode *rear; int numItems; public: Dynque(); ~Dynque(); void enqueue(T); void dequeue(T &); bool isEmpty(); bool isFull(); void clear(); }; //************************************************ // Constructor * //************************************************ template <class T> Dynque<T>::Dynque() { front = NULL; rear = NULL; numItems = 0; } //************************************************ // Destructor * //************************************************ template <class T> Dynque<T>::~Dynque() { clear(); } //************************************************ // Function enqueue inserts the value in num * // at the rear of the queue. * //************************************************ template <class T> void Dynque<T>::enqueue(T num) { QueueNode *newNode; newNode = new QueueNode; newNode->value = num; newNode->next = NULL; if (isEmpty()) front = rear = newNode; else { rear->next = newNode; rear = newNode; } numItems++; } //************************************************ // Function dequeue removes the value at the * // front of the queue, and copies it into num. * //************************************************ template <class T> void Dynque<T>::dequeue(T &num) { QueueNode *temp; if (isEmpty()) cout << "The queue is empty.\n"; else { num = front->value; temp = front->next; delete front; front = temp; numItems--; } } //************************************************ // Function isEmpty returns true if the queue * // is empty, and false otherwise. * //************************************************ template <class T> bool Dynque<T>::isEmpty() { if (numItems) return false; else return true; } //************************************************ // Function clear dequeues all the elements * // in the queue. * //************************************************ template <class T> void Dynque<T>::clear() { T value; // Dummy variable for dequeue while(!isEmpty()) dequeue(value); }
Leave a reply