Advertisement
Nakumas

[C++] Caesar's cipfer

Apr 10th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. bool encryptChar(char _c)
  9. {
  10.     return !(_c == ' ' || _c == ',' || _c == '.' || _c == '?' || _c == '!' || _c == ':' || _c == ';' || _c == '(' || _c == ')');
  11. }
  12.  
  13. char cipher(char _c, int _shift)
  14. {
  15.     if(encryptChar(_c)) return (char)(_c + _shift);
  16.     else return _c;
  17. }
  18.  
  19. string properFile(string _fileName, string _content, int _shift)
  20. {
  21.     fstream file;
  22.     file.open(_fileName + ".txt", ios::in);
  23.     if(file.good())
  24.     {
  25.         vector <string> lines;
  26.         string temp;
  27.         cout << "Poprawnie otworzono plik" << endl;
  28.  
  29.         cout << "Trwa odczytywanie danych z pliku..." << endl;
  30.         while(!file.eof())
  31.         {
  32.             getline(file, temp);
  33.             lines.push_back(temp);
  34.         }
  35.         cout << "Trwa kodowanie..." << endl;
  36.         for (int k = 0; k < lines.size(); ++k)
  37.         {
  38.             for (int j = 0; j < lines[k].length(); ++j)
  39.             {
  40.                 lines[k][j] = cipher(lines[k][j], _shift);
  41.                 //cout << lines[k][j] << ">" << cipher(lines[k][j], _shift) << "\t"; //
  42.             }
  43.         }
  44.         for (const string line : lines)
  45.         {
  46.             _content += line + "\n";
  47.         }
  48.         cout << "Kodowanie zakonczone!" << endl;
  49.     }
  50.     else cout << "Blad podczas otwierania pliku" << endl;
  51.     file.flush();
  52.     file.close();
  53.     return _content;
  54. }
  55.  
  56. void saveEncryptedToFile(string _endFile, string _content)
  57. {
  58.     fstream file;
  59.     file.open(_endFile + ".txt", ios::out);
  60.     file << _content;
  61.     file.close();
  62. }
  63.  
  64. int main()
  65. {
  66.     const int shift = 3;
  67.     string content, fileName, endFile;
  68.         cout << "Podaj nazwe pliku(bez rozszerzenia), ktore zawartosc chcesz zakodowac: ";
  69.         cin >> fileName;
  70.         content = properFile(fileName, content, shift);
  71.  
  72.         //cout << content <<endl;
  73.         endFile = fileName+"_Encrypted";
  74.         saveEncryptedToFile(endFile, content);
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement