Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Online C Compiler.
  4. Code, Compile, Run and Debug C program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <iostream>
  10. #include <string>
  11.  
  12. using namespace std;
  13.  
  14. class playfair
  15. {
  16. public:
  17. void doIt( string k, string t, bool ij, bool e )
  18. {
  19. createGrid( k, ij ); getTextReady( t, ij, e );
  20. if( e ) doIt( 1 ); else doIt( -1 );
  21. display();
  22. }
  23.  
  24. private:
  25. void doIt( int dir )
  26. {
  27. int a, b, c, d; string ntxt;
  28. for( string::const_iterator ti = _txt.begin(); ti != _txt.end(); ti++ )
  29. {
  30. if( getCharPos( *ti++, a, b ) )
  31. if( getCharPos( *ti, c, d ) )
  32. {
  33. if( a == c ) { ntxt += getChar( a, b + dir ); ntxt += getChar( c, d + dir ); }
  34. else if( b == d ){ ntxt += getChar( a + dir, b ); ntxt += getChar( c + dir, d ); }
  35. else { ntxt += getChar( c, b ); ntxt += getChar( a, d ); }
  36. }
  37. }
  38. _txt = ntxt;
  39. }
  40.  
  41. void display()
  42. {
  43. cout << "\n\n OUTPUT:\n=========" << endl;
  44. string::iterator si = _txt.begin(); int cnt = 0;
  45. while( si != _txt.end() )
  46. {
  47. cout << *si; si++; cout << *si << " "; si++;
  48. if( ++cnt >= 26 ) cout << endl, cnt = 0;
  49. }
  50. cout << endl << endl;
  51. }
  52.  
  53. char getChar( int a, int b )
  54. {
  55. return _m[ (b + 5) % 5 ][ (a + 5) % 5 ];
  56. }
  57.  
  58. bool getCharPos( char l, int &a, int &b )
  59. {
  60. for( int y = 0; y < 5; y++ )
  61. for( int x = 0; x < 5; x++ )
  62. if( _m[y][x] == l )
  63. { a = x; b = y; return true; }
  64.  
  65. return false;
  66. }
  67.  
  68. void getTextReady( string t, bool ij, bool e )
  69. {
  70. for( string::iterator si = t.begin(); si != t.end(); si++ )
  71. {
  72. *si = toupper( *si ); if( *si < 65 || *si > 90 ) continue;
  73. if( *si == 'J' && ij ) *si = 'I';
  74. else if( *si == 'Q' && !ij ) continue;
  75. _txt += *si;
  76. }
  77. if( e )
  78. {
  79. string ntxt = ""; size_t len = _txt.length();
  80. for( size_t x = 0; x < len; x += 2 )
  81. {
  82. ntxt += _txt[x];
  83. if( x + 1 < len )
  84. {
  85. if( _txt[x] == _txt[x + 1] ) ntxt += 'X';
  86. ntxt += _txt[x + 1];
  87. }
  88. }
  89. _txt = ntxt;
  90. }
  91. if( _txt.length() & 1 ) _txt += 'X';
  92. }
  93.  
  94. void createGrid( string k, bool ij )
  95. {
  96. if( k.length() < 1 ) k = "KEYWORD";
  97. k += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string nk = "";
  98. for( string::iterator si = k.begin(); si != k.end(); si++ )
  99. {
  100. *si = toupper( *si ); if( *si < 65 || *si > 90 ) continue;
  101. if( ( *si == 'J' && ij ) || ( *si == 'Q' && !ij ) )continue;
  102. if( nk.find( *si ) == -1 ) nk += *si;
  103. }
  104. copy( nk.begin(), nk.end(), &_m[0][0] );
  105. }
  106.  
  107. string _txt; char _m[5][5];
  108. };
  109.  
  110. int main( int argc, char* argv[] )
  111. {
  112. string key, i, txt; bool ij, e;
  113. cout << "(E)ncode ili (D)ecode? "; getline( cin, i ); e = ( i[0] == 'e' || i[0] == 'E' );
  114. cout << "Eneti kljuc: "; getline( cin, key );
  115. cout << "I <-> J (Y/N): "; getline( cin, i ); ij = ( i[0] == 'y' || i[0] == 'Y' );
  116. cout << "Uneti text: "; getline( cin, txt );
  117. playfair pf; pf.doIt( key, txt, ij, e ); return system( "pause" );
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement