Advertisement
a53

Playfair

a53
Jun 27th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. using namespace std;
  4. int F[26];
  5. struct LC
  6. {
  7. int x,y;
  8. };
  9. LC lc[26];
  10.  
  11. int main()
  12. {
  13. int c;
  14. ifstream f("playfair.in");
  15. f>>c;
  16. char s[5][5],t[10000];
  17. for(int i=0;i<5;++i)
  18. for(int j=0;j<5;++j)
  19. f>>s[i][j],F[s[i][j]-'a']=1,lc[s[i][j]-'a'].x=i,lc[s[i][j]-'a'].y=j;
  20. f>>t;
  21. char lit;
  22. for(int i=0;i<26;++i) /// Stabilim litera care nu exista in matrice
  23. if(F[i]==0)
  24. lit=i+'a',i=27;
  25. int L=(int)strlen(t);
  26. if(c==1)
  27. for(int i=0;i<L;i+=2)
  28. if(t[i]==t[i+1])
  29. t[i+1]=lit;
  30. else
  31. {
  32. int a=lc[t[i]-'a'].x,b=lc[t[i]-'a'].y,c=lc[t[i+1]-'a'].x,d=lc[t[i+1]-'a'].y;
  33. if(a!=c&&b!=d)
  34. t[i]=s[a][d],t[i+1]=s[c][b];
  35. if(a==c)
  36. {
  37. if(b==4)
  38. b=-1;
  39. if(d==4)
  40. d=-1;
  41. t[i]=s[a][b+1],t[i+1]=s[c][d+1];
  42. }
  43. if(b==d)
  44. {
  45. if(a==4)
  46. a=-1;
  47. if(c==4)
  48. c=-1;
  49. t[i]=s[a+1][b],t[i+1]=s[c+1][d];
  50. }
  51. }
  52. else
  53. for(int i=0;i<L;i+=2)
  54. if(t[i+1]==lit)
  55. t[i+1]=t[i];
  56. else
  57. {
  58. int a=lc[t[i]-'a'].x,b=lc[t[i]-'a'].y,c=lc[t[i+1]-'a'].x,d=lc[t[i+1]-'a'].y;
  59. if(a!=c&&b!=d)
  60. t[i]=s[a][d],t[i+1]=s[c][b];
  61. if(a==c)
  62. {
  63. if(b==0)
  64. b=5;
  65. if(d==0)
  66. d=5;
  67. t[i]=s[a][b-1],t[i+1]=s[c][d-1];
  68. }
  69. if(b==d)
  70. {
  71. if(a==0)
  72. a=5;
  73. if(c==0)
  74. c=5;
  75. t[i]=s[a-1][b],t[i+1]=s[c-1][d];
  76. }
  77. }
  78. ofstream g("playfair.out");
  79. g<<t<<'\n';
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement