-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugboard.cpp
More file actions
65 lines (56 loc) · 1.68 KB
/
Copy pathplugboard.cpp
File metadata and controls
65 lines (56 loc) · 1.68 KB
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
#include "plugboard.h"
#include "errors.h"
using namespace std;
Plugboard::Plugboard(std::string const& call_string){
int count = 0;
string pb_digit_string;
ifstream is_plugboard;
is_plugboard.open(call_string, ios::in);
if (!is_plugboard){
exit_code = ERROR_OPENING_CONFIGURATION_FILE;
}
is_plugboard >> pb_digit_string;
while (!exit_code && !is_plugboard.eof()){
if (!exit_code)
is_numeric(pb_digit_string, exit_code);
if (!exit_code)
input_in_range(pb_digit_string, exit_code);
if (!exit_code)
pb_configuration[count] = stoi(pb_digit_string);
if (!exit_code){
if (!is_repetitive(count, pb_configuration))
exit_code = IMPOSSIBLE_PLUGBOARD_CONFIGURATION;
}
if (!exit_code)
is_plugboard >> pb_digit_string;
count++;
if (!exit_code && count >= ALPHABET && !is_plugboard.eof())
exit_code = INCORRECT_NUMBER_OF_PLUGBOARD_PARAMETERS;
}
//SETTING A FLAG TO SIGNALISE END OF PLUGBOARD
if (!exit_code && count < ALPHABET)
pb_configuration[count] = -1;
if (!exit_code && count % 2 == 1)
exit_code = INCORRECT_NUMBER_OF_PLUGBOARD_PARAMETERS;
is_plugboard.close();
}
/*End of Function*/
int Plugboard::encrypt(int digit){
int blocker = 0, remainder, i = 0;
//Calculating how many plugboard connections were specified
while (i >= 0 && blocker < ALPHABET){
i = this->pb_configuration[blocker];
blocker++;
}
for (int j = 0 ; j < blocker; j++){
if (this->pb_configuration[j] == digit){
remainder = j % 2;
if (!remainder)
return (this->pb_configuration[j+1]);
if (remainder)
return (this->pb_configuration[j-1]);
}
}
return digit;
}
/*End of Function*/