Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. //============================================================================
  2. // Name : P.cpp
  3. // Author : Oliver Mattos
  4. // Version :
  5. // Copyright :
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include <string>
  11. #include <algorithm>
  12. #include <cctype>
  13. using namespace std;
  14.  
  15. int mod5(int x) {
  16. return (x+5)%5;
  17. }
  18.  
  19. int mod25(int x) {
  20. return (x+25)%25;
  21. }
  22.  
  23. string digraph(string keyword, string di) {
  24.  
  25. int posa = keyword.find_first_of(di[0], 0);
  26. int posb = keyword.find_first_of(di[1], 0);
  27.  
  28. if (posa%5 == posb%5) {
  29. // same col
  30. return string(1, keyword[mod25(posa+5)]) + string(1,keyword[mod25(posb+5)]);
  31. }
  32.  
  33. if (posa/5 == posb/5) {
  34. // same row
  35. return string(1, keyword[(posa/5)*5 + mod5(posa+1)]) + string(1, keyword[(posb/5)*5 + mod5(posb+1)]);
  36. }
  37.  
  38. return string(1, keyword[(posa/5)*5+posb%5]) + string(1, keyword[(posb/5)*5+posa%5]);
  39.  
  40. }
  41.  
  42. int main() {
  43. string tmp;
  44. string keyword;
  45.  
  46. cin >> tmp >> keyword; // prints !!!Hello World!!!
  47.  
  48. std::transform(keyword.begin(), keyword.end(),keyword.begin(), ::toupper);
  49.  
  50. keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  51.  
  52. for (int i=0; i<(int)keyword.length(); i++)
  53. if (keyword[i] == 'J') keyword[i] = 'I';
  54.  
  55. for (int i=0; i<(int)keyword.length(); i++) {
  56. while (keyword.find_first_of(keyword[i], i+1)<100000)
  57. keyword.erase(keyword.begin()+keyword.find_first_of(keyword[i], i+1));
  58. }
  59.  
  60.  
  61. cin >> tmp; // prints !!!Hello World!!!
  62. cout << "Message: ";
  63. cin.get();
  64.  
  65.  
  66. while (!cin.fail()) {
  67.  
  68. char a, b;
  69. bool acase, bcase, end=false;
  70.  
  71. while (!isalpha(a = cin.get())) { if (cin.fail() || a != ' ') { end = true; break;} cout << a; };
  72.  
  73. if (end) break;
  74.  
  75. acase = isupper(a);
  76. a = toupper(a);
  77.  
  78. int gotA;
  79.  
  80. do {
  81. gotA = false;
  82. string whitespace = "";
  83. string encoded;
  84.  
  85. while (!isalpha(b = cin.get())) { if (cin.fail() || b != ' ') { end=true; break;} whitespace +=b; };
  86.  
  87. if (end) {
  88. b = 'X';
  89. bcase=false;
  90. } else {
  91. bcase = isupper(b);
  92. b = toupper(b);
  93. }
  94.  
  95. if (a == 'J') a = 'I';
  96. if (b == 'J') b = 'I';
  97.  
  98. if (a==b) {
  99. // bah, fail case...
  100. encoded = digraph(keyword, string(1, a) + "X" );
  101. acase = bcase;
  102. bcase = false;
  103. a =b;
  104. gotA = true;
  105.  
  106. } else {
  107. encoded = digraph(keyword, string(1, a) + string(1, b) );
  108. }
  109.  
  110. cout << string(1, acase?encoded[0]:(encoded[0]-'A'+'a'));
  111. cout << whitespace;
  112. cout << string(1, bcase?encoded[1]:(encoded[1]-'A'+'a'));
  113.  
  114.  
  115. } while (gotA && !end);
  116.  
  117.  
  118.  
  119.  
  120. }
  121.  
  122. cout << endl;
  123.  
  124.  
  125.  
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement