Guest User

Untitled

a guest
Jul 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <ctime>
  4. #include <stdlib.h>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. void wypelnij_alfabet(char tablica[]) {
  10.  
  11. for (int i = 0; i < 26; i++) {
  12. tablica[i] = 97 + i;
  13. }
  14. tablica[26] = 0;
  15. }
  16.  
  17. void wypelnij_kodem(char kod[]) {
  18.  
  19. char tmp;
  20. int i, j;
  21. bool ok = false;
  22.  
  23. i = j = 0;
  24.  
  25. srand((unsigned)time(0));
  26.  
  27. while (i < 26) {
  28. ok = false;
  29.  
  30. while (ok == false) {
  31. tmp = rand()%26 + 97;
  32. for (j = 0; j < i && (tmp != kod[j]) && (tmp != i + 97); j++) {}
  33. if (j == i) ok = true;
  34. else ok = false;
  35. }
  36. kod[i] = tmp;
  37. i++;
  38. }
  39. kod[26] = 0;
  40. }
  41.  
  42. void wyswietl(char tablica[], char kod[]) {
  43.  
  44. cout<<" Kodowanie: "<<endl<<endl;
  45.  
  46. for (int j = 0; j < 26; j++) {
  47. cout<<" "<<tablica[j]<<" -> "<<kod[j]<<endl;
  48. }
  49. cout<<endl;
  50. }
  51.  
  52. int main (int argc, char * argv[]) {
  53.  
  54. char tablica[27];
  55. char kod[27];
  56. int x = 1;
  57. int j = 0;
  58.  
  59. ofstream file("slownik.txt");
  60.  
  61. string tekst;
  62.  
  63. while (x < argc) {
  64.  
  65. tekst += argv[x];
  66. if (x != argc - 1) tekst += " ";
  67. x++;
  68. }
  69.  
  70. cout<<endl<<"Tekst, ktory podales: "<<tekst<<endl;
  71.  
  72. wypelnij_alfabet(tablica);
  73. wypelnij_kodem(kod);
  74. wyswietl(tablica, kod);
  75.  
  76. file<<kod;
  77. file.close();
  78.  
  79. map<char, char> mapa;
  80. map <char, char> mapa2;
  81.  
  82. for (int i = 0; i < 26; i++) {
  83. mapa.insert(pair<char, char>(tablica[i],kod[i]));
  84. }
  85.  
  86. map<char, char>::iterator it = mapa.begin();
  87.  
  88. j = 0;
  89. tekst.c_str();
  90.  
  91. while (tekst[j] != 0) {
  92.  
  93. it = mapa.find(tekst[j]);
  94.  
  95. if (tekst[j] == 32) {
  96. j++; continue;
  97. }
  98. else {
  99. tekst[j] = it->second;
  100. j++;
  101. }
  102. }
  103.  
  104. ifstream plik("slownik.txt");
  105.  
  106. j = 0;
  107.  
  108. cout<<"Zakodowany tekst: "<<tekst<<endl;
  109. cout<<"Odkodowany: ";
  110.  
  111. char znak;
  112.  
  113. map<char, char>slownik;
  114. while (j < 26) {
  115. slownik.insert(pair<char,char>(tablica[j],plik.get()));
  116. j++;
  117. }
  118. j = 0;
  119.  
  120. while (tekst[j] != 0) {
  121.  
  122. it = slownik.begin();
  123.  
  124. if (tekst[j] == 32) {
  125. j++; continue;
  126. }
  127. else {
  128. while ((it->second) != (tekst[j])) {
  129. it++;
  130. }
  131. tekst[j] = it->first;
  132. j++;
  133. }
  134. }
  135. cout<<tekst<<endl<<endl;
  136. plik.close();
  137. }
Add Comment
Please, Sign In to add comment