Advertisement
Guest User

Untitled

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