Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include "stdafx.h"
- #include <fstream>
- #include <iostream>
- #include <cstdlib>
- #include <string>
- using namespace std;
- const int x = 30; int alf[x];
- void analslov(string str)
- {
- for (int i = 0; i < str.length(); i++)
- {
- if (str[i] > 'z' || str[i] < 'a') continue; //fitcha
- alf[str[i] - 'a']++;
- }
- }
- void obnul()
- {
- for (int i = 0; i < x; i++)
- alf[i] = 0;
- }
- int dcode()
- {
- int max=0, itog=0;
- for (int i = 0; i < x; i++)
- {
- if (alf[i] > max) { max = alf[i]; itog = i; }
- }
- return itog;
- }
- char perev(char a, int x)
- {
- char itog;
- if (a > 'z' || a < 'a') return a; //fitcha
- if (a - x < 'a') itog = 'z' - (x- (a-'a')-1);
- else
- {
- itog = a - x;
- }
- return itog;
- }
- string decoding(string str, int x)
- {
- string itog="";
- for (int i = 0; i < str.length(); i++)
- {
- itog += perev(str[i], x);
- }
- return itog;
- }
- int main()
- {
- setlocale(LC_ALL, "rus");
- string a1, s, a2; ifstream code; ofstream decode;
- cout << "Введите путь к файлу\n";
- cin >> a1;
- cout<< "Введите путь для файла вывода\n";
- cin >> a2;
- string subA1=a2.substr(a2.length()-4);
- if (subA1!=".txt") {cerr<<"Пожалуйста, убедитесь, что файл вывода имеет тип .txt и попробуйте снова\n"; return 1;} // Обработка попытки вывести не .txt
- decode.open(a2.c_str());
- code.open(a1.c_str());
- if (!code.is_open()) {
- cout<< "Файл шифра не открыт. Проверьте указанный путь и повторите.\n"; // Обработка некорректного файла ВВОДА
- return 0;
- }
- if (!decode.is_open()) {
- cout << "Невозможно создать файл вывода. Проверьте указанный путь и повторите.\n"; // Обработка некорректного файла ВЫВОДА
- return 0;
- }
- obnul();
- while (!code.eof())
- {
- code >> s;
- analslov(s);
- }
- int e = dcode();
- //cout « e;
- int sdvig = e - 4; if (sdvig < 0) sdvig += 26;
- // code.clear();
- code.seekg(0);
- while (!code.eof())
- {
- code >> s;
- s=decoding(s,sdvig);
- decode <<s;
- }
- decode.close();
- code.close();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement