Problem 2
Assume the implementation of KeyProvider is correct.
Using KeyProvider, define class Vigenere that satisfies the suggested specification as shown below:
#pragma once
#include "KeyProvider.h" #define CHARACTERS 26 class Vigenere
{
private:
char fMappingTable[CHARACTERS][CHARACTERS];
const std::string fKeyword; KeyProvider fKeywordProvider;
// Initialize the mapping table
// Row 1: B - A
// Row 26: A - Z
void initializeTable();
public:
// Initialize Vigenere scrambler [8] Vigenere( const std::string& aKeyword );
// Return the current keyword. [22]
// This method scans the keyword provider and copies the keyword characters
// into a result string. std::string getCurrentKeyword();
// Reset Vigenere scrambler. [6]
// This method has to initialize the keyword provider.
void reset();
// Encode a character using the current keyword character and update keyword. [36]
char encode( char aCharacter );
// Decode a character using the current keyword character and update keyword. [46]
char decode( char aCharacter );
};
Class Vigenere maintains a mapping table, the initial keyword string, and a keyword provider. The method initializeTable() sets up the mapping table. Somebody has already implemented this method:
void Vigenere::initializeTable()
{
for ( char row = 0; row < CHARACTERS; row++ )
{
char lChar = 'B' + row;
for ( char column = 0; column < CHARACTERS; column++ )
{
if ( lChar > 'Z' ) lChar = 'A';
fMappingTable[row][column] = lChar++;
}
}
}
The constructor has to initialize all member variables with sensible variables. Ideally, all member variables except fMappingTable should be initialized using member initializers. Please note that neither KeyProvider nor Vigenere define default constructors. We do not support empty keywords. Hence, you must use a member initializer for fKeywordProvider in order for the Vigenere constructor to work.
The method getCurrentKeyword() returns the current keyword stored in fKeywordProvider. There is no direct access to it. Instead, getCurrentKeyword() has to scan the keyword provider and assemble a result string that represents the current keyword. If the method works properly, then the keyword in fKeywordProvider is invariant.
The method reset() initializes the keyword provider with the initial keyword string. This means, we can use reset() to restart the Vigenère scrambler.
The methods encode() and decode() provide the scrambling operations. They implement the encoding and decoding process, respectively, as described above. When processing a character, these methods record whether the character is upper case or lower case, and each time a letter is being processed the current keyword character is updated as part of the autokey cipher process.
You can test your implementation of Vigenere using the following test driver (enable
#define P2 in main.cpp):
#include "Vigenere.h"
int runP2( string argv[2] )
{
Vigenere lSrambler( argv[0] ); string lMessage = argv[1];
// Test encoding
cout << "Encoding \"" << lMessage
<< "\" using \"" << lSrambler.getCurrentKeyword() << "\"" << endl;
for ( char c : lMessage )
{
cout << (isalpha( c ) ? static_cast<char>(toupper( c )) : c);
}
cout << "\n";
string lEncodedMessage;
for ( char c : lMessage )
{
lEncodedMessage += lSrambler.encode( c );
}
cout << lEncodedMessage << "\nCompleted" << endl;
// Test decoding lSrambler.reset();
cout << "Decoding \"" << lEncodedMessage
<< "\" using \"" << lSrambler.getCurrentKeyword() << "\"" << endl;
for ( char c : lEncodedMessage ){
cout << (isalpha( c ) ? static_cast<char>(toupper( c )) : c);
}
cout << "\n";
string lDecodedMessage;
for ( char c : lEncodedMessage )
{
lDecodedMessage += lSrambler.decode( c );
}
cout << lDecodedMessage << "\nCompleted" << endl;
return 0;
}
Running the test driver should produce the following output:
Encoding "To be, or not to be: that is the question:" using "RELATIONS" TO BE, OR NOT TO BE: THAT IS THE QUESTION:
Lt nf, ia ccm nd dj: izoi cm ijj kcfmcbiv:
Completed
Decoding "Lt nf, ia ccm nd dj: izoi cm ijj kcfmcbiv:" using "RELATIONS" LT NF, IA CCM ND DJ: IZOI CM IJJ KCFMCBIV:
To be, or not to be: that is the question: Completed
Students succeed in their courses by connecting and communicating with an expert until they receive help on their questions
Consult our trusted tutors.