Advertisement
Guest User

fdg

a guest
Feb 20th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. // ConsoleApplication1.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <fstream>
  5. #include <string>
  6. #include <algorithm>
  7. #include <iostream>
  8. #include <ctype.h>
  9.  
  10.  
  11.  
  12. using namespace std;
  13. //Объявляем массив символов русского и английского алфавита
  14. string alpha = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
  15.  
  16. int main() {
  17. system("chcp 1251");
  18. system("cls");
  19. setlocale(LC_ALL, "Russian");
  20. ifstream keys("key.txt");
  21. ifstream start("start.txt");
  22. ofstream output("out.txt");
  23. string message;
  24. string key;
  25. string keybuf;
  26. string crypt;
  27. while (!keys.eof())
  28. {
  29. char buf;
  30. bool check = 0;
  31. keys >> buf;
  32. buf = tolower(buf);
  33. if ((buf >= 'а') && (buf <= 'я'))
  34. {
  35. for (int i = 0; i <= keybuf.length(); ++i)
  36. {
  37. if (keybuf[i] == buf)
  38. check = 1;
  39. }
  40. if (check == 0)
  41. {
  42. keybuf += buf;
  43. check = 0;
  44. }
  45. }
  46. }
  47. cout << keybuf;
  48. for (int i = 0; i < alpha.length(); ++i)
  49. {
  50. bool check = 0;
  51. for (int j = 0; j < keybuf.length(); j++)
  52. {
  53. if (alpha[i] == keybuf[j]) check = 1;
  54. }
  55. if (check == 0) keybuf += alpha[i];
  56. }
  57.  
  58. cout << endl << alpha << endl << keybuf << endl;
  59. bool mode;
  60. cout << "Введите 0 для шифровки сообщения или 1 для дешифровки сообщения" << endl;
  61. cin >> mode;
  62. if (mode == 0) {
  63. while (!start.eof()) {
  64. char buf;
  65. start >> buf;
  66. buf = tolower(buf);
  67. bool symb = 0;
  68. for (int i = 0; i < alpha.length(); ++i)
  69. {
  70. if (buf == alpha[i])
  71. {
  72. crypt += keybuf[i];
  73. }
  74. }
  75. }
  76. crypt.erase(crypt.length() - 1, 1);
  77. cout << "Криптограмма: " << crypt;
  78. output << crypt;
  79. }
  80. else
  81. {
  82. while (!start.eof())
  83. {
  84. char buf;
  85. bool symb = 0;
  86. start >> buf;
  87. buf = tolower(buf);
  88. for (int i = 0; i < alpha.length(); ++i)
  89. {
  90. if (buf == keybuf[i])
  91. {
  92. message += alpha[i];
  93. }
  94. }
  95. }
  96. message.erase(message.length() - 1, 1);
  97. cout << endl << "Изначальное сообщение: " << message << endl;
  98. output << message;
  99. }
  100. system("pause");
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement