Advertisement
Guest User

chechikov - kruto :/

a guest
Nov 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. string normalize(string str, int mode) {
  8.     //modes:
  9.     //0 - To lower
  10.     //1 - To upper
  11.     for (int i = 0; i < str.size(); i++) {
  12.         switch (mode) {
  13.         case 0: {
  14.             str[i] = tolower(str[i]);
  15.             break;
  16.         }
  17.         case 1: {
  18.             str[i] = toupper(str[i]);
  19.             break;
  20.         }
  21.         default: {
  22.             i = str.size();
  23.         }
  24.         }
  25.     }
  26.     return str;
  27. }
  28.  
  29. int main() {
  30.     const string ENstrAlphabet = "abcdefghijklmnopqrstuvwxyz";
  31.  
  32.     string message, key, subAlphabet;
  33.     subAlphabet = ENstrAlphabet;
  34.     map<char, char> Alphabet;
  35.    
  36.     cout << "ENTER YOUR MESSAGE:\n";
  37.     getline(cin, message);
  38.  
  39.     cout << "ENTER THE KEY:\n";
  40.     cin >> key;
  41.    
  42.     //creating subAlphabet
  43.     for (char i : key) {
  44.         subAlphabet.erase(subAlphabet.begin() + subAlphabet.find(i));
  45.     }
  46.     subAlphabet = key + subAlphabet;
  47.  
  48.     for (int i = 0; i < ENstrAlphabet.size(); i++) {
  49.        
  50.     }
  51.     //
  52.  
  53.     //normalizing
  54.     message = normalize(message, 0);
  55.     subAlphabet = normalize(subAlphabet, 1);
  56.     //
  57.  
  58.     //creating Alphabet map
  59.     for (int i = 0; i < subAlphabet.size(); i++) {
  60.         Alphabet.insert(pair<char, char>(ENstrAlphabet[i], subAlphabet[i]));
  61.     }
  62.  
  63.     //!adding space to the alphabet
  64.     Alphabet.insert(pair<char, char>(' ', ' '));
  65.     //
  66.  
  67.  
  68.     //encode
  69.     for (int i = 0; i < message.size(); i++) {
  70.         message[i] = Alphabet[message[i]];
  71.     }
  72.     //
  73.  
  74.     cout << " " << endl;
  75.     cout << "ENCODE\n\n" << "Alphabet:\n"<< endl;
  76.     cout << '-' << ENstrAlphabet << endl;
  77.     cout << '+' << subAlphabet << endl;
  78.     cout << " " << endl;
  79.     cout << message << endl;
  80.  
  81.     //decode
  82.     for (int i = 0; i < message.size(); i++) {
  83.         for (char k : ENstrAlphabet) {
  84.             if (Alphabet[k] == message[i]) {
  85.                 message[i] = k;
  86.                 break;
  87.             }
  88.         }
  89.     }
  90.     //O(n^2)
  91.  
  92.     cout << " " << endl;
  93.     cout << "DECODE\n\n" << "Alphabet:\n" << endl;
  94.     cout << '-' << subAlphabet << endl;
  95.     cout << '+' << ENstrAlphabet << endl;
  96.     cout << " " << endl;
  97.     cout << message << endl;
  98.  
  99.     system("pause");
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement