Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4.  
  5. using namespace std;
  6.  
  7. int transferLetter(const char*, int const, char const);
  8.  
  9. int* encoding(char const*, char const*);
  10.  
  11. char* decoding(int const*, int, char const*);
  12.  
  13. char* createRule(const char*);
  14.  
  15. void createRuleTest();
  16.  
  17. void encodingTests();
  18.  
  19. void decodingTests();
  20.  
  21. bool equals(const char*, const char*);
  22.  
  23. bool equals(const int*, const int*, int);
  24.  
  25.  
  26.  
  27. int main()
  28.  
  29. {
  30.     createRuleTest();
  31.  
  32.     encodingTests();
  33.  
  34.     decodingTests();
  35.  
  36.     system("pause");
  37.  
  38. }
  39. int getlength(const char* source)
  40. {
  41.     if (source == nullptr)
  42.     {
  43.         throw std::invalid_argument("String cannot be null!");
  44.     }
  45.  
  46.     int length = 0;
  47.     while (source[length])
  48.     {
  49.         length++;
  50.     }
  51.     return length;
  52. }
  53. bool isAlphabet(char symbol)
  54. {
  55.     return 'A' <= symbol && symbol <= 'Z' || 'a' <= symbol && symbol <= 'z';
  56. }
  57. bool isLower(char symbol)
  58. {
  59.     return 'a' <= symbol && symbol <= 'z';
  60. }
  61. char toUpper(char symbol)
  62. {
  63.     if (isAlphabet(symbol) && isLower(symbol))
  64.     {
  65.         return symbol - 32;
  66.     }
  67.     return symbol;
  68. }
  69. char* createRule(const char* source)
  70. {
  71.     for (int i = 0; i < getlength(source); i++)
  72.         for (int j = getlength(source) - 1; j >= 0; j--)
  73.         {
  74.             swap(source[j], source[i]);
  75.         }
  76.    
  77.     return nullptr;
  78.  
  79. }
  80. int* encoding(const char* source, const char* rule)
  81.  
  82. {
  83.     int* code = new int[getlength(source)+1];
  84.  
  85.     int transfer = 1;
  86.    
  87.     for(int i = 0; i < (getlength(source) + 1); i++)
  88.         for (int j = 0; j < (getlength(rule) + 1); j++)
  89.         {
  90.              if (isAlphabet(source[i]))
  91.              {
  92.                 code[i] = transferLetter(rule, getlength(rule), toUpper(source[i])) + 1;
  93.              }
  94.              else
  95.              {
  96.                  code[i] = source[i];
  97.              }
  98.         }
  99.     return nullptr;
  100. }
  101. char* decoding(const int* source, int size, const char* rule)
  102. {
  103.  
  104.     // TODO decoding logic here
  105.  
  106.     return nullptr;
  107.  
  108. }
  109. int transferLetter(const char* array, int const size, char const value)
  110. {
  111.     for (int i = 0; i < size; i++)
  112.     {
  113.         if (array[i] == value)
  114.         {
  115.             return i;
  116.         }
  117.     }
  118. }
  119.  
  120.  
  121. bool equals(const char* lhs, const char* rhs)
  122. {
  123.     int i = 0;
  124.  
  125.     while (lhs[i])
  126.     {
  127.         if (lhs[i] != rhs[i])
  128.         {
  129.             return false;
  130.         }
  131.         i++;
  132.     }
  133.     return true;
  134. }
  135.  
  136. bool equals(const int* lhs, const int* rhs, int size)
  137.  
  138. {
  139.     for (int i = 0; i < size; i++)
  140.     {
  141.         if (lhs[i] != rhs[i])
  142.         {
  143.             return false;
  144.         }
  145.     }
  146.     return true;
  147. }
  148.  
  149.  
  150.  
  151. void createRuleTest()
  152.  
  153. {
  154.  
  155.     const char* source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  156.  
  157.     const char* actual = createRule(source);
  158.  
  159.     const char* expected = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
  160.  
  161.     cout << "Test for creation rule " << (equals(actual, expected) ? "Passed." : "Failed.") << endl;
  162.  
  163. }
  164.  
  165.  
  166.  
  167. void encodingTests()
  168.  
  169. {
  170.  
  171.     const char* source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  172.  
  173.  
  174.  
  175.     const char* rule = createRule(source);
  176.  
  177.  
  178.  
  179.     int* encode = encoding("Learn C++!", rule);
  180.  
  181.  
  182.  
  183.     cout << "Test for encoding " << (equals(encode, new int[10]{ 15, 22, 26, 9, 13, 32, 24, 43, 43, 33 }, 10) ? "Passed." : "Failed.") << endl;
  184.  
  185.  
  186.  
  187.     encode = encoding("Cogito, ergo sum! (Descartes)", rule);
  188.  
  189.  
  190.  
  191.     cout << "Test for encoding " << (equals(encode, new int[29]{ 24, 12, 20, 18, 7, 12, 44, 32, 22, 9, 20, 12, 32, 8, 6, 14, 33, 32, 40, 23, 22, 8, 24, 26, 9, 7, 22, 8, 41 }, 29) ? "Passed." : "Failed.") << endl;
  192.  
  193.  
  194.  
  195. }
  196.  
  197.  
  198.  
  199. void decodingTests()
  200.  
  201. {
  202.  
  203.     const char* source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  204.  
  205.  
  206.  
  207.     const char* rule = createRule(source);
  208.  
  209.  
  210.  
  211.     char* decode = decoding(new int[10]{ 15, 22, 26, 9, 13, 32, 24, 43, 43, 33 }, 10, rule);
  212.  
  213.  
  214.  
  215.     cout << "Test for decoding " << (equals(decode, "Learn C++!") ? "Passed." : "Failed.") << endl;
  216.  
  217.  
  218.  
  219.     decode = decoding(new int[29]{ 24, 12, 20, 18, 7, 12, 44, 32, 22, 9, 20, 12, 32, 8, 6, 14, 33, 32, 40, 23, 22, 8, 24, 26, 9, 7, 22, 8, 41 },29, rule);
  220.  
  221.  
  222.  
  223.     cout << "Test for decoding " << (equals(decode, "Cogito, ergo sum! (Descartes)") ? "Passed." : "Failed.") << endl;
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement