fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype> // dla islower
  4. using namespace std;
  5.  
  6. /**
  7.  */
  8. string szyfruj2(const string &tekst, int klucz1, int klucz2) {
  9. string wynik = tekst;
  10.  
  11. // Redukujemy klucze do zakresu 0-25
  12. klucz1 = ((klucz1 % 26) + 26) % 26;
  13. klucz2 = ((klucz2 % 26) + 26) % 26;
  14.  
  15. for (size_t i = 0; i < tekst.size(); i++) {
  16. char c = tekst[i];
  17. if (!islower(c)) {
  18. throw invalid_argument("Tekst może zawierać tylko małe litery alfabetu łacińskiego.");
  19. }
  20. int przesuniecie = (i % 2 == 0) ? klucz1 : klucz2;
  21. // Szyfr Cezara
  22. wynik[i] = char('a' + ( (c - 'a' + przesuniecie) % 26 ));
  23. }
  24. return wynik;
  25. }
  26. int main() {
  27. try {
  28. cout << szyfruj2("poufnytekst", 3, 10) << endl; // oczekiwane: zreixbdhuvd
  29. cout << szyfruj2("poczekajnamnie", 123, 5) << endl; // oczekiwane: uhhsjdfcstrgnx
  30. }
  31. catch (const exception &e) {
  32. cerr << "Błąd: " << e.what() << endl;
  33. }
  34. return 0;
  35. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
syxpqiwoncw
itvexptogffsbj