Загрузка...

Оцените шифратор цезаря

Тема в разделе C/C++ создана пользователем quozzzz 9 мар 2023. 445 просмотров

  1. quozzzz
    quozzzz Автор темы 9 мар 2023 Заблокирован(а) 137 2 фев 2022
    Код
    #include <iostream>
    #include <string>

    using namespace std;

    string encrypt(string text, int shift)
    {
    string result = "";
    for (int i = 0; i < text.length(); i++) {
    if (isalpha(text[i])) {
    char c = text[i];
    if (isupper(c)) {
    result += char(int(c + shift - 65) % 26 + 65);
    }
    else {
    result += char(int(c + shift - 97) % 26 + 97);
    }
    }
    else {
    result += text[i];
    }
    }
    return result;
    }

    string decrypt(string text, int shift)
    {
    string result = "";
    for (int i = 0; i < text.length(); i++) {
    if (isalpha(text[i])) {
    char c = text[i];
    if (isupper(c)) {
    result += char(int(c - shift - 65 + 26) % 26 + 65);
    }
    else {
    result += char(int(c - shift - 97 + 26) % 26 + 97);
    }
    }
    else {
    result += text[i];
    }
    }
    return result;
    }

    int main()
    {
    int action, shift, num_letters;
    string text;

    cout << "Choose an action (1 for encryption, 2 for decryption): ";
    cin >> action;

    if (action == 1) {
    cout << "Enter the number of letters to shift: ";
    cin >> shift;
    cin.ignore();

    cout << "Enter the text to encrypt: ";
    getline(cin, text);

    string encrypted_text = encrypt(text, shift);
    cout << "Encrypted text: " << encrypted_text << endl;
    }
    else if (action == 2) {
    cout << "Enter the number of letters before the words: ";
    cin >> num_letters;
    cin.ignore();

    cout << "Enter the text to decrypt: ";
    getline(cin, text);

    string decrypted_text = decrypt(text, num_letters);
    cout << "Decrypted text: " << decrypted_text << endl;
    }
    else {
    cout << "Invalid action chosen. Please choose 1 for encryption or 2 for decryption." << endl;
    }

    return 0;
    }
     
    9 мар 2023 Изменено
    1. re1von
      quozzzz, вставь код нормально
    2. quozzzz Автор темы
  2. Pussy_Destroyer
    нормальный такой салат получился
     
  3. quozzzz
    quozzzz Автор темы 9 мар 2023 Заблокирован(а) 137 2 фев 2022
    Сделал
     
  4. gpt
    статичное шифрование
     
    1. quozzzz Автор темы
      gpt, норм получилось? Не говно код?
  5. Daemon
    Daemon 9 мар 2023 1680 8 янв 2021
    Начнем с того, зачем вы в условии в main скопипастили половину тела, если там 3 строки отличаются только? Сами не понимаете, что принцип DRY должен работать?
     
    9 мар 2023 Изменено
    1. Посмотреть предыдущие комментарии (1)
    2. quozzzz Автор темы
      Daemon, окей, ща подумаю что убрать
    3. Daemon
      quozzzz, Про инструкцию switch можете еще почитать, хотя это не обязательно, на любителя.
    4. quozzzz Автор темы
      Daemon, Switch я люблю) Просто на тот момент я знал только про if else, а switch я сейчас активно пользуюсь) Кейсы делать кайф в switch
    5. Посмотреть следующие комментарии (3)
Top
Загрузка...