Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int transferLetter(const char*, int const, char const);
- int* encoding(char const*, char const*);
- char* decoding(int const*, int, char const*);
- char* createRule(const char*);
- void createRuleTest();
- void encodingTests();
- void decodingTests();
- bool equals(const char*, const char*);
- bool equals(const int*, const int*, int);
- int main()
- {
- createRuleTest();
- encodingTests();
- decodingTests();
- system("pause");
- }
- int getlength(const char* source)
- {
- if (source == nullptr)
- {
- throw std::invalid_argument("String cannot be null!");
- }
- int length = 0;
- while (source[length])
- {
- length++;
- }
- return length;
- }
- bool isAlphabet(char symbol)
- {
- return 'A' <= symbol && symbol <= 'Z' || 'a' <= symbol && symbol <= 'z';
- }
- bool isLower(char symbol)
- {
- return 'a' <= symbol && symbol <= 'z';
- }
- char toUpper(char symbol)
- {
- if (isAlphabet(symbol) && isLower(symbol))
- {
- return symbol - 32;
- }
- return symbol;
- }
- char* createRule(const char* source)
- {
- for (int i = 0; i < getlength(source); i++)
- for (int j = getlength(source) - 1; j >= 0; j--)
- {
- swap(source[j], source[i]);
- }
- return nullptr;
- }
- int* encoding(const char* source, const char* rule)
- {
- int* code = new int[getlength(source)+1];
- int transfer = 1;
- for(int i = 0; i < (getlength(source) + 1); i++)
- for (int j = 0; j < (getlength(rule) + 1); j++)
- {
- if (isAlphabet(source[i]))
- {
- code[i] = transferLetter(rule, getlength(rule), toUpper(source[i])) + 1;
- }
- else
- {
- code[i] = source[i];
- }
- }
- return nullptr;
- }
- char* decoding(const int* source, int size, const char* rule)
- {
- // TODO decoding logic here
- return nullptr;
- }
- int transferLetter(const char* array, int const size, char const value)
- {
- for (int i = 0; i < size; i++)
- {
- if (array[i] == value)
- {
- return i;
- }
- }
- }
- bool equals(const char* lhs, const char* rhs)
- {
- int i = 0;
- while (lhs[i])
- {
- if (lhs[i] != rhs[i])
- {
- return false;
- }
- i++;
- }
- return true;
- }
- bool equals(const int* lhs, const int* rhs, int size)
- {
- for (int i = 0; i < size; i++)
- {
- if (lhs[i] != rhs[i])
- {
- return false;
- }
- }
- return true;
- }
- void createRuleTest()
- {
- const char* source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- const char* actual = createRule(source);
- const char* expected = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
- cout << "Test for creation rule " << (equals(actual, expected) ? "Passed." : "Failed.") << endl;
- }
- void encodingTests()
- {
- const char* source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- const char* rule = createRule(source);
- int* encode = encoding("Learn C++!", rule);
- cout << "Test for encoding " << (equals(encode, new int[10]{ 15, 22, 26, 9, 13, 32, 24, 43, 43, 33 }, 10) ? "Passed." : "Failed.") << endl;
- encode = encoding("Cogito, ergo sum! (Descartes)", rule);
- 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;
- }
- void decodingTests()
- {
- const char* source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- const char* rule = createRule(source);
- char* decode = decoding(new int[10]{ 15, 22, 26, 9, 13, 32, 24, 43, 43, 33 }, 10, rule);
- cout << "Test for decoding " << (equals(decode, "Learn C++!") ? "Passed." : "Failed.") << endl;
- 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);
- cout << "Test for decoding " << (equals(decode, "Cogito, ergo sum! (Descartes)") ? "Passed." : "Failed.") << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement