-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugboard.cpp
89 lines (80 loc) · 2.37 KB
/
Plugboard.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
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include "Plugboard.hpp"
bool Plugboard::plugboardDuplicateCheck()
{
for (size_t firstCharacter = 0; firstCharacter < plugboardCopy.length() - 1; ++firstCharacter)
{
for (size_t secondCharacter = firstCharacter + 1; secondCharacter < plugboardCopy.length(); ++secondCharacter)
{
if (plugboardCopy[firstCharacter] == plugboardCopy[secondCharacter])
{
std::cout << "Error: duplicates found" << std::endl;
return true;
}
}
}
return false;
}
bool Plugboard::lengthCheck()
{
if (plugboardCopy.length() / 2 > 13)
{
std::cout << "Error: too much pairs. There should no more than 13 pairs" << std::endl;
return true;
}
else
{
return false;
}
}
bool Plugboard::correctInputCheck()
{
for (auto &iterator : plugboardCopy)
{
if (!(iterator > 64 && iterator < 91))
{
std::cout << "Error: plugboard should contain only alphabetical characters (A-Z)" << std::endl;
return true;
}
}
return false;
}
bool Plugboard::setPlugboard()
{
std::cout << "Type characters in pairs (i.e. \"KL ON ...\"). No more than 13 pairs" << std::endl;
std::cin.ignore();
std::getline(std::cin, plugboardCopy);
std::transform(plugboardCopy.begin(), plugboardCopy.end(), plugboardCopy.begin(), ::toupper);
plugboardCopy.erase(std::remove(plugboardCopy.begin(), plugboardCopy.end(), ' '), plugboardCopy.end());
if (plugboardDuplicateCheck() || lengthCheck() || correctInputCheck())
{
return true;
}
for (size_t firstCharacter = 0, secondCharacter = firstCharacter + 1;
secondCharacter < plugboardCopy.length();
firstCharacter += 2, secondCharacter += 2)
{
auto pair = std::make_pair(plugboardCopy[firstCharacter], plugboardCopy[secondCharacter]);
plugboard.emplace(pair);
}
return false;
}
void Plugboard::substitute(char &eachCharacter)
{
for (auto const &mapIterator : plugboard)
{
if (mapIterator.first == eachCharacter)
{
eachCharacter = mapIterator.second;
break;
}
if (mapIterator.second == eachCharacter)
{
eachCharacter = mapIterator.first;
break;
}
}
}