Advertisement
Guest User

chujnia

a guest
Jan 18th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. string text;
  7. int shift;
  8.  
  9. void save(string text)
  10. {
  11.     fstream saveFile;
  12.     string fileName = "a.txt";
  13.     saveFile.open(fileName.c_str(),ios::out);
  14.     saveFile << text;
  15.     saveFile.close();
  16. }
  17.  
  18. string load(string name)
  19. {
  20.     fstream loadFile;
  21.     string line;
  22.     loadFile.open(name);
  23.     getline(loadFile,line);
  24.     loadFile.close();
  25.     return line;
  26. }
  27.  
  28.  
  29. string cezar_encrypt( string inp , int shift)
  30. {
  31.     for(int i = 0; i <= inp.length() ; i++)
  32.     {
  33.         if( inp[i] >= 65 && inp[i] <= 90 - shift || inp[i] >= 97 && inp[i] <= 122 - shift) inp[i] = int(text[i]) + shift;
  34.         else if ( inp[i] >= 91 - shift && inp[i] <= 90 || inp[i] >= 123 - shift && inp[i] <= 122) inp[i] = int(text[i]) - 26 + shift;
  35.         //else cout << int(inp[i]) << endl;
  36.     }
  37.  
  38.     return inp;
  39. }
  40.  
  41. string cezar_decrypt( string inp , int shift)
  42. {
  43.     for(int i = 0; i <= inp.length() ; i++)
  44.     {
  45.         if( inp[i] >= 65 && inp[i] <= 90 - shift || inp[i] >= 97 && inp[i] <= 122 - shift) inp[i] = int(text[i]) - shift;
  46.         else if ( inp[i] >= 91 - shift && inp[i] <= 90 || inp[i] >= 123 - shift && inp[i] <= 122) inp[i] = int(text[i]) + 26 - shift;
  47.         //else cout << int(inp[i]) << endl;
  48.     }
  49.  
  50.     return inp;
  51. }
  52.  
  53. int main()
  54. {
  55.     string text2 , encrypted_text;
  56.     cout << "Podaj tekst do zaszyfrowania" << endl;
  57.     cin >> text;
  58.     cout << "Podaj przesunięcie" << endl;
  59.     cin >> shift;
  60.     cout << "Wynik:" << endl;
  61.     encrypted_text = cezar_encrypt(text, shift);
  62.     cout << encrypted_text << endl;
  63.     save(encrypted_text);
  64.     //cout << "Podaj klucz do deszyfrowania" << endl;
  65.     //cin >> shift;
  66.     text2 = load("a.txt");
  67.     text = cezar_decrypt(text2 , shift);
  68.     cout << "Wiadomosc po deszyfrowaniu: " << text << endl;
  69.  
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement