Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. //#include "stdafx.h"
  2. #include <fstream>
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. const int x = 30; int alf[x];
  10.  
  11. void analslov(string str)
  12. {
  13. for (int i = 0; i < str.length(); i++)
  14. {
  15. if (str[i] > 'z' || str[i] < 'a') continue; //fitcha
  16. alf[str[i] - 'a']++;
  17. }
  18.  
  19. }
  20.  
  21. void obnul()
  22. {
  23. for (int i = 0; i < x; i++)
  24. alf[i] = 0;
  25. }
  26. int dcode()
  27. {
  28. int max=0, itog=0;
  29. for (int i = 0; i < x; i++)
  30. {
  31. if (alf[i] > max) { max = alf[i]; itog = i; }
  32. }
  33. return itog;
  34. }
  35. char perev(char a, int x)
  36. {
  37. char itog;
  38. if (a > 'z' || a < 'a') return a; //fitcha
  39. if (a - x < 'a') itog = 'z' - (x- (a-'a')-1);
  40. else
  41. {
  42. itog = a - x;
  43. }
  44. return itog;
  45. }
  46. string decoding(string str, int x)
  47. {
  48. string itog="";
  49. for (int i = 0; i < str.length(); i++)
  50. {
  51. itog += perev(str[i], x);
  52. }
  53. return itog;
  54. }
  55.  
  56. int main()
  57. {
  58. setlocale(LC_ALL, "rus");
  59. string a1, s, a2; ifstream code; ofstream decode;
  60. cout << "Введите путь к файлу\n";
  61. cin >> a1;
  62. cout<< "Введите путь для файла вывода\n";
  63. cin >> a2;
  64. string subA1=a2.substr(a2.length()-4);
  65. if (subA1!=".txt") {cerr<<"Пожалуйста, убедитесь, что файл вывода имеет тип .txt и попробуйте снова\n"; return 1;} // Обработка попытки вывести не .txt
  66. decode.open(a2.c_str());
  67. code.open(a1.c_str());
  68. if (!code.is_open()) {
  69. cout<< "Файл шифра не открыт. Проверьте указанный путь и повторите.\n"; // Обработка некорректного файла ВВОДА
  70. return 0;
  71. }
  72. if (!decode.is_open()) {
  73. cout << "Невозможно создать файл вывода. Проверьте указанный путь и повторите.\n"; // Обработка некорректного файла ВЫВОДА
  74. return 0;
  75. }
  76. obnul();
  77. while (!code.eof())
  78. {
  79. code >> s;
  80. analslov(s);
  81. }
  82. int e = dcode();
  83. //cout « e;
  84. int sdvig = e - 4; if (sdvig < 0) sdvig += 26;
  85.  
  86. // code.clear();
  87. code.seekg(0);
  88. while (!code.eof())
  89. {
  90. code >> s;
  91. s=decoding(s,sdvig);
  92. decode <<s;
  93. }
  94. decode.close();
  95. code.close();
  96. system("pause");
  97. return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement