Advertisement
cmiN

decriptare3

Jun 30th, 2011
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. string mystr, vowels("AEIOU");
  7.  
  8. void normalize()
  9. {
  10.     string::iterator it;
  11.     for (it = mystr.begin(); it != mystr.end(); ++it) {
  12.         *it = toupper(*it);
  13.     }
  14. }
  15.  
  16. bool isvow(char chr)
  17. {
  18.     if (vowels.find(chr) == string::npos) {
  19.         return 0;
  20.     } else {
  21.         return 1;
  22.     }
  23. }
  24.  
  25. char rotate(char chr, short len)
  26. {
  27.     if (len > 0) {
  28.         do {
  29.             if (chr == 'Z') {
  30.                 chr = 'A';
  31.             } else {
  32.                 ++chr;
  33.             }
  34.         } while (--len);
  35.     } else if (len < 0) {
  36.         do {
  37.             if (chr == 'A') {
  38.                 chr = 'Z';
  39.             } else {
  40.                 --chr;
  41.             }
  42.         } while (++len);
  43.     }
  44.     return chr;
  45. }
  46.  
  47. void dec()
  48. {
  49.     bool tmp;
  50.     short it, torot = 0;
  51.     for (it = 0; it < mystr.length(); ++it) {
  52.         mystr[it] = rotate(mystr[it], -4);
  53.         tmp = isvow(mystr[it]);
  54.         mystr[it] = rotate(mystr[it], torot);
  55.         if (tmp) {
  56.             mystr.erase(it + 1, 1);
  57.             --torot;
  58.         }
  59.     }
  60. }
  61.  
  62. void enc()
  63. {
  64.     bool tmp;
  65.     short it, tt;
  66.     for (it = 0; it < mystr.length(); ++it) {
  67.         tmp = isvow(mystr[it]);
  68.         mystr[it] = rotate(mystr[it], 4);
  69.         if (tmp) {
  70.             mystr.insert(it + 1, 1, rotate(mystr[it], 10));
  71.             for (tt = ++it + 1; tt < mystr.length(); ++tt) {
  72.                 mystr[tt] = rotate(mystr[tt], 1);
  73.             }
  74.         }
  75.     }
  76. }
  77.  
  78. int main()
  79. {
  80.     short ans;
  81.     cout << "1. Encode\n2. Decode\n3. Exit\n";
  82.     cin >> ans;
  83.     if (ans == 1 || ans == 2) {
  84.         cout << "Input: ";
  85.         cin >> mystr;
  86.         normalize();
  87.         if (ans == 1) {
  88.             enc();
  89.         } else {
  90.             dec();
  91.         }
  92.         cout << "Output: " << mystr << endl;
  93.         main();
  94.     }
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement