Skip to content

Commit

Permalink
added comments in enigma.h
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhan Chan committed Nov 21, 2020
1 parent 70c5356 commit bbbb684
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 21 deletions.
4 changes: 3 additions & 1 deletion enigma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Enigma::Enigma(int argc, char** argv)
}

// this function brings together all the necessary parts of the enigma machine to encode/decode input to output
void Enigma::decoder_encoder( int input, const Plugboard plugboard, const Reflector reflector, Rotor* set_of_rotors, const int number_of_rotors){
void Enigma::decoder_encoder(const int input, const Plugboard plugboard, const Reflector reflector, Rotor* const set_of_rotors, const int number_of_rotors){
// initialise ouput as 0
int output = 0;

Expand All @@ -64,6 +64,8 @@ void Enigma::decoder_encoder( int input, const Plugboard plugboard, const Reflec
// if not the rightmost rotor, check notch of right rotor and turn if at 12 o'clock
(*(set_of_rotors + i)).rotor_rotation(set_of_rotors, number_of_rotors, i);
output = (set_of_rotors + i) -> right_to_left(output);


}
}

Expand Down
15 changes: 7 additions & 8 deletions enigma.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ Please refer to the .h file for information on the other components.
*/

class Enigma{
int argc;
char** argv;
Plugboard plugboard;
Rotor* set_of_rotors;
Reflector reflector;
int number_of_rotors;
int argc; // number of command line arguments
char** argv; // pointer to an array of pointers
Plugboard plugboard; // plugboard object. only 1 plugboard is allowed per machine
Rotor* set_of_rotors = nullptr; // pointer to rotor 0, in a set of rotors, if number_of_rotors > 0. NULL otherwise.
Reflector reflector; // reflector object. only 1 reflector is allowed per machine
int number_of_rotors; // number of rotors in the machine

// this is an internal helper function used in enncrypt() that encodes the message and output it to the standard output stream.
void decoder_encoder(int input, const Plugboard plugboard, const Reflector reflector, Rotor* set_of_rotors, const int number_of_rotors);
void decoder_encoder(const int input, const Plugboard plugboard, const Reflector reflector, Rotor* const set_of_rotors, const int number_of_rotors);

public:
// Default constructor which initialises the various components of the machine
Expand All @@ -28,7 +28,6 @@ class Enigma{
// this reads input from the standard input stream and calls decoder_encoder().
void encrypt();


};

// ========= Non-member functions =========
Expand Down
3 changes: 1 addition & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

int main (int argc, char** argv){

// For global constants, please see "constants.h"
// For constants defined, please see "constants.h"

try{
// =========== CHECK COMMAND LINE ARGUMENTS ===========
Expand All @@ -13,7 +13,6 @@ int main (int argc, char** argv){
// =========== ENCODING/DECODING INPUT ===========
// Note that rotor position starts from 0 from the left.
// Example: For three rotors, their position will be 0, 1 and 2 from left to right
//int number_of_rotors = argc - FIXED_ARGV;

Enigma enigma = Enigma(argc, argv);
enigma.encrypt();
Expand Down
5 changes: 2 additions & 3 deletions reflector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


// this function scrambles input through the reflector
int Reflector::reflector_output(const int input) const {
int Reflector::reflector_output(int input) const {
int output;
return output = rf_mapping[input];
}
Expand Down Expand Up @@ -76,7 +76,6 @@ void Reflector::load_rf_setting(const char* filename, int rf_mapping[NUM_OF_ALPH

}


// check if there is an even number of settings
if (count % 2 != 0) {
std::cerr << "Incorrect (odd) number of parameters in reflector file reflector.rf" << std::endl;
Expand All @@ -90,7 +89,7 @@ void Reflector::load_rf_setting(const char* filename, int rf_mapping[NUM_OF_ALPH
}

// map the entire reflector using the settings
for ( int x = 0; x< NUM_OF_ALPHABETS; x++) rf_mapping[x] = x;
for ( int x = 0; x < NUM_OF_ALPHABETS; x++) rf_mapping[x] = x;

for ( int i = 0; i < NUM_OF_ALPHABETS; i+=2 ) {
rf_mapping[rf_setting[i]] = rf_setting[i+1];
Expand Down
2 changes: 1 addition & 1 deletion reflector.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Reflector{
}

// this function scrambles input through the reflector
int reflector_output(const int input) const;
int reflector_output(int input) const;
};

#endif
4 changes: 2 additions & 2 deletions rotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void load_rotor_pos(const char* filename, int pos_array[], const int number_of_r
}

// this function initialises enigma_rotors based on input parameters
void initialise_enigma_rotors(Rotor* set_of_rotors, const int number_of_rotors, char* argv[]){
void initialise_enigma_rotors(Rotor* const set_of_rotors, const int number_of_rotors, char* argv[]){

// Initialise array containing each rotor's starting position
int pos_array[number_of_rotors];
Expand All @@ -81,7 +81,7 @@ void initialise_enigma_rotors(Rotor* set_of_rotors, const int number_of_rotors,
// ========== rotor class member functions ==========

// this function rotates the rotor based on the notch
void Rotor::rotor_rotation(Rotor* set_of_rotors, const int number_of_rotors, const int rotor_num){
void Rotor::rotor_rotation(Rotor* const set_of_rotors, const int number_of_rotors, const int rotor_num){

// if it is rightmost rotor, rotate immediately
if ( rotor_num == number_of_rotors - 1) {
Expand Down
8 changes: 4 additions & 4 deletions rotor.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef ROTOR_H
#define ROTOR_H

# include "constants.h"
#include "constants.h"

/* This is a user defined class for Rotor, one of the key components in the engima machine.
There can be multiple Rotors defined as required by the user.
Expand Down Expand Up @@ -31,13 +31,13 @@ class Rotor{

// default constructor. Note that rotor_pos starts from 0 for the left most rotor.
Rotor(){};
Rotor(const char*filename, const int pos_array[], const int rotor_pos){
Rotor(const char* filename, const int pos_array[], const int rotor_pos){
load_rotor_setting(filename, pos_array, rotor_pos);
this -> rotor_pos = rotor_pos;
}

// controls the rotation of each rotor based on rotor and notch position
void rotor_rotation(Rotor enigma_rotors[], const int number_of_rotors, const int rotor_num);
void rotor_rotation(Rotor* const enigma_rotors, const int number_of_rotors, const int rotor_num);

// to decode/encode char entering from right of rotor and output to left
int right_to_left(const int right_input);
Expand All @@ -52,7 +52,7 @@ class Rotor{
void load_rotor_pos(const char* filename, int pos_array[], const int number_of_rotors);

// initialises a *group* of rotors when the objects are first created
void initialise_enigma_rotors(Rotor enigma_rotors[], const int number_of_rotors, char* argv[]);
void initialise_enigma_rotors(Rotor* const set_of_rotors, const int number_of_rotors, char* argv[]);


#endif

0 comments on commit bbbb684

Please sign in to comment.