Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ PROGRAMMING Design a file filter package. * * 1) A file filter reads an inpu

ID: 3828922 • Letter: C

Question

C++ PROGRAMMING

Design a file filter package.

*

* 1) A file filter reads an input file, transforms it in some way, and writes the result to an output file.

* You are tasked to write an abstract Filter class that defines a pure virtual function char transform

* (char ch) for transforming a single character .

*

* 2) The Filter class should have member variables to hold the input and output streams; it also should

* have a default constructor that initializes the input/output streams to cin/cout respectively as well

* as a constructor that accepts initialized input/output file streams and uses them to initialize the

* Filter object .

*

* 3) The Filter class will also have a member function void doFilter() that will use the

* char transform(char ch) virtual function to do the filtering. Using your Filter class , create one

* derived class that performs encryption using ROT13 algorithm (explained below), another that

* transforms a file to all uppercase, and another that creates an unchanged copy of the original file.

* Write the main function that would open the file named "input.txt" and then use the three file filters

* to create 3 new files: input.txt_copy for the copy filter, input.txt_encr for the encryption filter,

* and input.txt_upper for the uppercase filter.

*

* ROT13: ------ ROT13 stands for "rotate by 13 places" and is a simple cipher based on substitution.

* To encrypt or decrypt using ROT13, each letter of the alphabet is replaced by a letter that is 13

* places further along the alphabet. For example, A becomes N and M becomes Z; similarly, N becomes

* A and Z becomes M. Only letters (both upper and lowercase) are affected by this algorithm; i.e.

* spaces, punctuation marks, digits and special characters are unchanged.

*/

Explanation / Answer

main.cpp

#include "stdafx.h"
#include <iostream>
#include "CopyFilter.h"
#include "UCFilter.h"
#include "ROT13Filter.h"

using namespace std;

int main()
{
   ifstream input;
   ofstream output;

   input.open("input.txt");
   output.open("input.txt_copy");
   CopyFilter Copy = CopyFilter(input, output);
   Copy.doFilter();
   input.close();
   output.close();

   input.open("input.txt");
   output.open("input.txt_encr");
   ROT13Filter ROT13 = ROT13Filter(input, output);
   ROT13.doFilter();
   input.close();
   output.close();

   input.open("input.txt");
   output.open("input.txt_upper");
   UCFilter UC = UCFilter(input, output);
   UC.doFilter();
   input.close();
   output.close();

   return 0;
}

CopyFilter.h

#pragma once

#ifndef CopyFilter_H
#define CopyFilter_H

#include "Filter.h"

class CopyFilter : public Filter
{
private:
   char transform(char ch)
   {
       return ch;
   }
public:
   CopyFilter() : Filter() {};
   CopyFilter(ifstream& i, ofstream& o) : Filter(i, o) {};
};

#endif

Filter.h

#pragma once
#ifndef FILTER_H
#define FILTER_H
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Filter {
private:
   //initialize the variables as fstreams, since it's easier to upconvert to fstream than downconvert to iostream
   ifstream& in = static_cast<ifstream&>(cin);
   ofstream& out = static_cast<ofstream&>(cout);
public:
   //default constructor: initialize to cin/cout
   Filter() {
       //istream& in = cin;
       //ostream& out = cout;
   }

   //initialize to ifstream//ofstream
   Filter(ifstream& i, ofstream& o)
   {
       cin.rdbuf(i.rdbuf());
       cout.rdbuf(o.rdbuf());
       ifstream& in = i;
       ofstream& out = o;
   }

   //performs the designated cipher (see base class)
   void doFilter() {
       char c;
       while (in.get(c)) { //if there's a character to read
           out.put(transform(c));
       }
       out.put(' ');
   };

   //cipher that produces a copy
   virtual char transform(char ch)
   {
       return ch; //dummy function, returns a copy
   };
};

#endif // !Filter

ROT13Filter.h

//ROT13.h for Programming Fundamentals II project.
//Authors (of this file):
// Khalid Hourani

#pragma once

#ifndef ROT13FILTER_H
#define ROT13FILTER_H

#include "Filter.h"

class ROT13Filter : public Filter
{
private:
   char transform(char ch)
   {
       if (('a' <= ch) && (ch <= 'z'))
       {
           ch -= 'a';
           ch += 13;
           ch = ch % 26;
           ch += 'a';
           return ch;
       }
       else if (('A' <= ch) && (ch <= 'Z'))
       {
           ch -= 'A';
           ch += 13;
           ch = ch % 26;
           ch += 'A';
           return ch;
       }
       else
       {
           return ch;
       }
   }
public:
   ROT13Filter() : Filter(){};
   ROT13Filter(ifstream& i, ofstream& o) : Filter(i, o) {};
};

#endif

UCFilter.h

#pragma once

#ifndef UC_H
#define UC_H

#include "Filter.h"

class UCFilter : public Filter
{
private:
   char transform(char ch)
   {
       return toupper(ch);
   }
public:
   UCFilter() : Filter() {};
   UCFilter(ifstream& i, ofstream& o) : Filter(i, o) {};
};

#endif


input.txt

The Project Gutenberg EBook of A Tale of Two Cities, by Charles Dickens

This eBook is for the use of anyone anywhere at no cost and with
almost no restrictions whatsoever. You may copy it, give it away or
re-use it under the terms of the Project Gutenberg License included
with this eBook or online at www.gutenberg.org


Title: A Tale of Two Cities
       A Story of the French Revolution

Author: Charles Dickens

Release Date: January, 1994 [EBook #98]
Posting Date: November 28, 2009
Last Updated: September 25, 2016

Language: English

Character set encoding: UTF-8

*** START OF THIS PROJECT GUTENBERG EBOOK A TALE OF TWO CITIES ***


Produced by Judith Boss


A TALE OF TWO CITIES

A STORY OF THE FRENCH REVOLUTION

By Charles Dickens


CONTENTS


     Book the First--Recalled to Life

     Chapter I      The Period
     Chapter II     The Mail
     Chapter III    The Night Shadows
     Chapter IV     The Preparation
     Chapter V      The Wine-shop
     Chapter VI     The Shoemaker


     Book the Second--the Golden Thread

     Chapter I      Five Years Later
     Chapter II     A Sight
     Chapter III    A Disappointment
     Chapter IV     Congratulatory
     Chapter V      The Jackal
     Chapter VI     Hundreds of People
     Chapter VII    Monseigneur in Town
     Chapter VIII   Monseigneur in the Country
     Chapter IX     The Gorgons Head
     Chapter X      Two Promises
     Chapter XI     A Companion Picture
     Chapter XII    The Fellow of Delicacy
     Chapter XIII   The Fellow of no Delicacy
     Chapter XIV    The Honest Tradesman
     Chapter XV     Knitting
     Chapter XVI    Still Knitting
     Chapter XVII   One Night
     Chapter XVIII Nine Days
     Chapter XIX    An Opinion
     Chapter XX     A Plea
     Chapter XXI    Echoing Footsteps
     Chapter XXII   The Sea Still Rises
     Chapter XXIII Fire Rises
     Chapter XXIV   Drawn to the Loadstone Rock


     Book the Third--the Track of a Storm

     Chapter I      In Secret
     Chapter II     The Grindstone
     Chapter III    The Shadow
     Chapter IV     Calm in Storm
     Chapter V      The Wood-sawyer
     Chapter VI     Triumph
     Chapter VII    A Knock at the Door
     Chapter VIII   A Hand at Cards
     Chapter IX     The Game Made
     Chapter X      The Substance of the Shadow
     Chapter XI     Dusk
     Chapter XII    Darkness
     Chapter XIII   Fifty-two
     Chapter XIV    The Knitting Done
     Chapter XV     The Footsteps Die Out For Ever

Book the First--Recalled to Life


I. The Period


It was the best of times,
it was the worst of times,
it was the age of wisdom,
it was the age of foolishness,
it was the epoch of belief,
it was the epoch of incredulity,
it was the season of Light,
it was the season of Darkness,
it was the spring of hope,
it was the winter of despair,
we had everything before us,
we had nothing before us,
we were all going direct to Heaven,
we were all going direct the other way--
in short, the period was so far like the present period, that some of
its noisiest authorities insisted on its being received, for good or for
evil, in the superlative degree of comparison only.

There were a king with a large jaw and a queen with a plain face, on the
throne of England; there were a king with a large jaw and a queen with
a fair face, on the throne of France. In both countries it was clearer
than crystal to the lords of the State preserves of loaves and fishes,
that things in general were settled for ever.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote