Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. // ConsoleApplication1.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <fstream>
  8. #include <cctype>
  9.  
  10. using namespace std;
  11.  
  12. const int NumberOfA = 26; //kol-vo bukv
  13.  
  14. bool decode(string Buf, int *Mas)
  15. {
  16.     if (Buf.length() == 0) return true;
  17.     if (Mas == 0) return false;
  18.     size_t i;
  19.     for (i = 0; i<Buf.length(); i++)
  20.     {
  21.         if (!isalpha(Buf[i]) || Buf[i]<'a' || Buf[i]>'z') continue;
  22.         Mas[(int)Buf[i] - 97]++;
  23.     }
  24.     return true;
  25. }
  26.  
  27. int Analyt(int *Mas)
  28. {
  29.     if (Mas == 0) return -500;
  30.     size_t i;
  31.     int x;
  32.     int Max = 0;
  33.     for (i = 0; i<NumberOfA; i++)
  34.     {
  35.         if (Mas[i]>Max)
  36.         {
  37.             x = i;
  38.             Max = Mas[i];
  39.         }
  40.     }
  41.     x -= 4;
  42.     if (x<0) x += 26;
  43.     return x;
  44. }
  45.  
  46. string decodedLine(string Buf, int Sdv)       //tutu nepravilno perevodit!!!!!!!!!!
  47. {
  48.     if (Buf.length() == 0) return "";
  49.     if (Sdv == 0) return Buf;
  50.     string itog = "";
  51.     size_t i;
  52.     for (i = 0; i<Buf.length(); i++)
  53.     {
  54.         if (!isalpha(Buf[i])) {
  55.             itog += Buf[i];
  56.             continue;
  57.         }
  58.         Buf[i] = tolower((int)Buf[i]);
  59.         // if((int)Buf[i]-Sdv<97) itog+=122+(97-(int)Buf[i]-Sdv);             //rabotaуt .. удали комментарий
  60.         if ((int)Buf[i] - Sdv<97) itog += 122 - (97 - Buf[i] + Sdv) - 1;
  61.         else itog += (char)((int)Buf[i] - Sdv);
  62.     }
  63.     itog += " ";
  64.     return itog;
  65. }
  66.  
  67. int main()
  68. {
  69.     int Albet[NumberOfA] = { 0 };
  70.     int Sdvig;
  71.     setlocale(LC_ALL, "rus");
  72.     ifstream Out;
  73.     ofstream In;
  74.     string OutWay, InWay, Buffer, BufOut;
  75.     cout << "Введи путь до зашифрованного файла";
  76.     cin >> OutWay;
  77.     Out.open(OutWay.c_str());
  78.     if (!Out.is_open())
  79.     {
  80.         cerr << "Файл ввода может быть открыт! Проверьте путь и попробуйте снова!" << endl;
  81.         return 1;
  82.     }
  83.     cout << "Введи путь до файла вывода";
  84.     cin >> InWay;
  85.     In.open(InWay.c_str());
  86.     if (!In.is_open())
  87.     {
  88.         cerr << "Файл вывода может быть открыт! Проверьте путь и попробуйте снова!" << endl;
  89.         return 1;
  90.     }
  91.     try {
  92.         while (!Out.eof())
  93.         {
  94.             Out >> Buffer;
  95.             if (Out.fail())
  96.             {
  97.                 cerr << "Ошибка при вводе из файла";
  98.                 return 2;
  99.             }
  100.             if (!decode(Buffer, Albet))
  101.             {
  102.                 cerr << "Проблема с передачей массива";
  103.                 return 3;
  104.             }
  105.         }
  106.  
  107.         Out.close(); // тут throw exeptiot failbit
  108.     }
  109.     catch (...)
  110.     {
  111.         cerr << "There is a problem with the file";
  112.         return 5;
  113.     }
  114.     Sdvig = Analyt(Albet);
  115.     if (Sdvig == -500)
  116.     {
  117.         cerr << "Проблема с передачей массива";
  118.         return 3;
  119.     }
  120.     Out.open(OutWay.c_str());           //dlya decode
  121.     if (!Out.is_open())
  122.     {
  123.         cerr << "Файл ввода может быть открыт! Проверьте путь и попробуйте снова!" << endl;
  124.         return 1;
  125.     }
  126.     try {
  127.         while (!Out.eof())
  128.         {
  129.             Out >> Buffer;
  130.             if (Out.fail())
  131.             {
  132.                 cerr << "Ошибка при вводе из файла";
  133.                 return 2;
  134.             }
  135.             BufOut = decodedLine(Buffer, Sdvig);   //zachem?
  136.                                                    // if (BufOut!=)
  137.             In << BufOut;
  138.         }
  139.         Out.close();
  140.         In.close();
  141.     }
  142.     catch (...)
  143.     {
  144.         cerr << "There is a problem with the file";
  145.         return 2;
  146.     }
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement