-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotor.cpp
96 lines (71 loc) · 2.66 KB
/
Rotor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
-----------------------------------------------------------------------------------
Laboratoire : 02
Fichier : Rotor.cpp
Auteur(s) : Doran Kayoumi, Jérémie Melly, Pierre-Olivier Sandoz
Date : 07.03.2019
But : Implémentation ce la class Rotor
Remarque(S) : -
Compilateur : MinGW-g++ 6.3.0
-----------------------------------------------------------------------------------
*/
#include "Rotor.h"
using namespace std;
const vector<vector<string>> Rotor::DEFAULT_CONFIG = {
{"I", "EKMFLGDQVZNTOWYHXUSPAIBRCJ", "R"},
{"II", "AJDKSIRUXBLHWTMCQGZNPYFVOE", "F"},
{"III", "BDFHJLCPRTXVZNYEIWGAKMUSQO", "W"},
{"IV", "ESOVPZJAYQUIRHXLNFTGKDCMWB", "K"},
{"V", "VZBRGITYUPSDNHLXAWMJQOFECK", "A"}
};
const string Rotor::ENTRY = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vector<string> Rotor::getConfig(const string &ID) {
for (vector<string> config : Rotor::DEFAULT_CONFIG) {
if (config.at(0) == ID) return config;
}
// If this point is reached, the wanted config doesn't exist
// an exception should be thrown if this point is reached
}
Rotor::Rotor(const string &ID, char position) :
ID(ID), WIRING(Rotor::getConfig(ID)[1]), NOTCH(Rotor::getConfig(ID)[2].at(0)), position(toupper(position)) {}
bool Rotor::reachedNotch() {
return this->position == this->NOTCH;
}
void Rotor::turn() {
if (this->position == 'Z')
this->position = 'A';
else
++this->position;
}
char Rotor::convert(char toConvert) const {
size_t toConvertPos = Rotor::ENTRY.find(toConvert);
size_t rotorPos = Rotor::ENTRY.find(this->position);
return this->WIRING.at((rotorPos + toConvertPos) % 26);
}
char Rotor::decode(char toDecode) const {
size_t toDecodePos = this->WIRING.find(toDecode);
size_t rotorPos = Rotor::ENTRY.find(this->position);
return Rotor::ENTRY.at((toDecodePos - rotorPos + 26) % 26);
}
void Rotor::setPosition(char position) {
this->position = position;
}
ostream &operator<<(ostream &os, const Rotor &r) {
string pos_wiring;
size_t index = Rotor::ENTRY.find(r.position);
pos_wiring = r.WIRING.substr(index) + r.WIRING.substr(0, index);
os << "rotor ID : " << r.ID << endl
<< "entry : " << Rotor::ENTRY << endl
<< "def wiring : " << r.WIRING << endl
<< "position : " << r.position << endl
<< "pos wiring : " << pos_wiring << endl
<< "notch : " << r.NOTCH << endl;
return os;
}
Rotor &Rotor::operator=(const Rotor &ROTOR) {
(string &) this->ID = ROTOR.ID;
(string &) this->WIRING = ROTOR.WIRING;
(string &) this->NOTCH = ROTOR.NOTCH;
this->position = ROTOR.position;
return *this;
}