Advertisement
shek_shek

algebra_1

Mar 2nd, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stack>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <string>
  8. #include <set>
  9. #include <iomanip>
  10. #include <vector>
  11. #include <map>
  12. #include <cassert>
  13. #include <queue>
  14. #include <fstream>
  15.  
  16. using namespace std;
  17.  
  18. typedef long long li;
  19. typedef long double ld;
  20.  
  21. #define forn(i, n) for (int i = 0; i < int(n); ++i)
  22. #define pb push_back
  23. #define mp make_pair
  24. #define all(v) v.begin(),v.end()
  25. #define EPS 1e-9
  26. #define PI 3.1415926535897932384626433832795
  27.  
  28.  
  29. int main () {
  30.     ifstream key_stream("key.txt");
  31.     ifstream text_stream("text.txt");
  32.     freopen("output.txt", "w", stdout);
  33.     string alph = "абвгдезжиклмнопрстуфцчхшщьъэюя0123456789!,.?", key = "";
  34.     bool contain_in_alph[44];
  35.     forn (i, alph.size())
  36.         contain_in_alph[i] = false;
  37.     while (!key_stream.eof()) {
  38.         string temp;
  39.         key_stream >> temp;
  40.         forn (i, temp.size()) {
  41.             temp[i] += (temp[i] >= 'A' && temp[i] <= 'Я') ? ('а' - 'А') : 0;
  42.             int idx = alph.find(temp[i]);
  43.             if (!contain_in_alph[idx])
  44.                 contain_in_alph[idx] = true, key += temp[i];
  45.         }
  46.     }
  47.     if (key.size() < alph.size() / 2) {
  48.         cout << "Ключ некорректен" << endl;
  49.         return 0;
  50.     }
  51.     forn (i, alph.size())
  52.         if (!contain_in_alph[i])
  53.             key += alph[i], contain_in_alph[i] = true;
  54.  
  55.     string result = "";
  56.     while (!text_stream.eof()) {
  57.         string temp;
  58.         text_stream >> temp;
  59.         forn (i, temp.size()) {
  60.             bool fl = false;
  61.             if (temp[i] >= 'А' && temp[i] <= 'Я') {
  62.                 temp[i] += 'а' - 'А';
  63.                 fl = !fl;
  64.             }
  65.             int idx = key.find(temp[i]);
  66.             result += (key[(idx + (key.size() / 2)) % key.size()] - ((fl) ? ('а' - 'А') : 0));
  67.         }
  68.         result += " ";
  69.     }
  70.     cout << result << endl;
  71.  
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement