Assume the implementation of class iVigenereStream is correct
Ask Expert

Be Prepared For The Toughest Questions

Practice Problems

Assume the implementation of class iVigenereStream is correct

Problem 4

Assume the implementation of class iVigenereStream is correct. We can define a forward iterator that encapsulates an iVigenereStream stream and use this iterator to encode and decode characters in a for-each range loop, respectively. For example, we can read an encoded stream via a forward iterator and send each character to a target stream like below:

for ( char c : VigenereForwardIterator( lInput ) )

{

cout << c;

}

Here, lInput is an iVigenereStream stream that performs on-the-fly encoding while reading a text input file. In the for-each-loop we only see the encoded characters.

A suggested specification of class VigenereForwardIterator is shown below:

#pragma once

#include "iVigenereStream.h"

class VigenereForwardIterator

{

private:

iVigenereStream& fIStream;

char fCurrentChar;

bool fEOF;

public:

VigenereForwardIterator( iVigenereStream& aIStream ); // [10]

// forward iterator interface

char operator*() const; // [2]

VigenereForwardIterator& operator++(); // prefix increment [10] VigenereForwardIterator operator++( int ); // postfix increment [10]

bool operator==( const VigenereForwardIterator& aOther ) const; // [8]

bool operator!=( const VigenereForwardIterator& aOther) const; // [4]

VigenereForwardIterator begin() const; // [16]

VigenereForwardIterator end() const; // [10]

};

Class VigenereForwardIterator defines a standard forward iterator. However, streams in C++ are generally not copiable. Hence, we need to maintain access to the input stream fIStream via a reference. This requires fIStream to be initialized via a member initializer.

The iterator has to always read one character. This can be achieved via the increment operators. When we construct a VigenereForwardIterator object, then its fCurrentCharacter has to be set to the first character in the stream. If the underlying stream runs in encode mode, then fCurrentCharacter is set to its encoded equivalent. Otherwise, it is set to the “clear value”.

The iterator does not maintain a dedicated index. Instead, it uses a fEOF flag, which is true if and only if the underlying stream has reached end-of-file. Two iterators of type VigenereForwardIterator are equal (i.e., they are positioned on the same character), when their respective addresses are the same and if their fEOF flags are the same. The latter is used to define an end iterator.

The dereference operator returns the value of fCurrentChar. The increment operators advance the iterator. That is, increment means we input the next character (with on-the-fly scrambling) and test whether the iterator has reached end-of-file.

 The auxiliary methods begin() and end() are required to compile the for-each range loop. The method begin() returns a copy of the current iterator whose stream has been reset and which is positioned on the first character, if such a character is available. The method end() also returns a copy of the current iterator, but with its fEOF flag set to true. An iterator positioned at the end has logically reached end-of-file.

You can test your implementation of VigenereForwardIterator using the following test driver (enable #define P4 in main.cpp):

#include "VigenereForwardIterator.h"

int runP4( string argv[2] )

{

iVigenereStream lInput( []( Vigenere& aCipherProvider, char aCharacter )

{

return aCipherProvider.encode( aCharacter );

} , argv[0], argv[1].c_str() );

if ( !lInput.good() )

{

cerr << "Cannot open input file: " << argv[1] << endl;

return 2;

}

cout << "Forward Iterator Decoding \"" << argv[1]

<< "' using \"" << argv[0] << "\"." << endl;

for ( char c : VigenereForwardIterator( lInput ) )

{

cout << c;

}

lInput.close();

cout << "Completed." << endl;

return 0;

}

Running the test driver should produce the following output (Shakespeare’s Richard III). The file sample_4.txt must be in the “Working Directory”.

Forward Iterator Encoding "sample_4.txt' using "Relations".

SHF J MLTBX J

Oiwwrs. O Xcdtsx.

Tbuxl Yqtohsmywy.

Sajxhxmywy. Zdr lx mbj owcqnk in tra rcxudtizfx Vtgt uftfcbvw xbybwa qt madf fzf qe Swad;

Tir prk izp dzsvpe npfw xdpv'w oxph ajm zspit Wc ozm szxu kcmwr sk yxg dvtnc hozntg.

Spk cmw xzv pglxk gdpff oxqa xxxhsorico fuyplqh; Jno twvcaxs vjok cdgl yq xbk ujbbhutik;

Bjf ngjfh tavjngx uvbzhwy gh pmsff riyivsyk; Nhw ilnokyjg eejhiiy oa qfdlommzjp rqjzclkn.

Senn-odkfz'k oje djmi zqlplp'e bql jgxhspmm yogwh; Lzi ruo, - xbmuser dc vcnhyjrv hngwsx bhlgek,

Vt jkclmx mbt ygdsa iz njtgage pjbjskgmufw, - Dj utqwax gqremo nf t zjqa'e bqonnfv

Sh wpf ycxuckcwze qehjorcb hv m qvmn. 

Pbi O, utvn fo iic mpbj'e scg miwsjmbt lkyrcm, Wkw gsmh eh qdmeu es ubrgjmm mcpxxfv-bemhh;

T, cvha mn knmytz munek'h, fzc puog bsws'w jbxyeiu Yh fubzm vdzdky s rupyuc srcijba cmncj;

U, cvhh zz scanijf'e bi ozct ommg vlwyhxurgd, Uwuplym dt imfuowi qe jntmzegnhrp gtyhtq, Msmcsg'y, msjntxku'h, nstc pnywvx rm nkrk

Xfyb sbrf gasuibqwz ygwmx, aloyzt zmpy pbvh zx, Bzj giey ne moqytz ugs gosfegjcrvprf

Mpji rpie guzl ux bl, tu J zlmn od uanu; Xts, K, hh bmvp edjt dcxrgd yjxu xv yshwn, Ufkk dt ehqqhdy hd tfeb heus ixf mbnb, Vmfmxm cb xzs yd laushb na sam tyc

Xwr xmxvvbu cr qngh pkh sssxfrxqm:

Esj izrayepfi, mqsuj O rssgxh swxyf o zdpuj, Ik joftnyscc yvyxw zbrf qmqe-xvptwk imkl,

Y px isxfqfrorh yi ujbes f zcabsxj,

Foz qmff cvf whtf jqyixdvqx er yixnw itny. Jttmx lbux Y xpcw, qozzlfjxrb reijyadil, Fz ryzfzzg rqshcsnnsi, dxrmqv, jsw pagfyl, Uc wil rz okiiajl Pkcjthkj sqp uzj ylsh

Wr xmfoum ojhi yii amm bafcvxi hmf vuqsk:

Uhl nu Eqsy Frajxo ks hx xovw epi knml Vx J oq cpunmx, obylz, chp yxfmvmffsok,

Yila isn naicuw Gmzkmcxq goatwqm ej pql'w zb, Zdthy x tmeqjtxs, xxarx afbr, qpjw O

Hg Dwqisx'z wknvp uzi fcwmwkyz xuvdp gw.

Iaom, utawllco, iiec ov us lsji: vygr Bepmqvhw hryfk.

Gfrykte, lhqv sug: bzhi bieor qpjm nwnsw acjke Lufx dvjlw oxph vpdl Zmqrs?

Completed.

The output should match the contents of sample_3.txt. It is the encoded text of Shakespeare’s Richard III, Act I, Scene I.

vigenere.cpp

main.cpp

Hint
ComputerBinary encoding: It is a procedure mainly to convert the data to a form which is easily used by difference computer operating systems. Also, it is achieved by converting the binary data to a ASCII string format, by converting 8-bit data into a 7-bit format specifically, which is used as the standard set of ASCII printable characters....

Know the process

Students succeed in their courses by connecting and communicating with
an expert until they receive help on their questions

1
img

Submit Question

Post project within your desired price and deadline.

2
img

Tutor Is Assigned

A quality expert with the ability to solve your project will be assigned.

3
img

Receive Help

Check order history for updates. An email as a notification will be sent.

img
Unable to find what you’re looking for?

Consult our trusted tutors.

Developed by Versioning Solutions.