diff --git a/AvtomatikaTestTask1.cpp b/AvtomatikaTestTask1.cpp new file mode 100644 index 0000000..8caf925 --- /dev/null +++ b/AvtomatikaTestTask1.cpp @@ -0,0 +1,108 @@ +#include +#include +#include + + +bool match(const char* str, const char* pattern); + +void testMatch(const char* str, const char* pattern, bool expectedMatchResult); + +const char* bool_to_str(bool b); + +void printStringForSpecifiedNumberOfTimes(const char* symbolToPrint, int count); + + +int main() +{ + SetConsoleCP(1251); + SetConsoleOutputCP(1251); + + printf("Тестирование функции match:\n"); + + const char* symbolToPrint = "="; + const int count = 64; + printStringForSpecifiedNumberOfTimes(symbolToPrint, count); + + testMatch("ab1c", "ab%c", true); + testMatch("ab2c", "ab%c", true); + testMatch("abc", "ab%c", false); + testMatch("ab12c", "ab%c", false); + + testMatch("ab1c", "ab*c", true); + testMatch("ab2c", "ab*c", true); + testMatch("abc", "ab*c", true); + testMatch("ab12c", "ab*c", true); + testMatch("acb", "ab*c", false); + testMatch("ab", "ab*c", false); + + testMatch("", "*", true); + testMatch("", "%", false); + testMatch("a", "*", true); + testMatch("a", "%", true); + testMatch("ab", "a%", true); + testMatch("ab", "%b", true); + + testMatch("", "", true); + testMatch("a", "", false); + testMatch("a", "*a", true); + testMatch("a", "*a*", true); + + return 0; +} + + +bool match(const char* str, const char* pattern) { + + if (*str == '\0' && *pattern == '\0') { + return true; + } + + if (*pattern == '*') { + while (*(pattern + 1) == '*') { + pattern++; + } + + if (match(str, pattern + 1)) { + return true; + } + + if (*str != '\0' && match(str + 1, pattern)) { + return true; + } + + return false; + } + + if (*str == '\0' || *pattern == '\0') { + return false; + } + + if (*pattern == '%' || *pattern == *str) { + return match(str + 1, pattern + 1); + } + + return false; +} + +void testMatch(const char* str, const char* pattern, bool expectedMatchResult) { + bool matchResult = match(str, pattern); + const char* resultStatus = (matchResult == expectedMatchResult) ? "Successed" : "Failed"; + + printf("%s: match(\"%s\", \"%s\") вернул %s (ожидалось: %s)\n", + resultStatus, str, pattern, + bool_to_str(matchResult), + bool_to_str(expectedMatchResult) + ); +} + +const char* bool_to_str(bool b) { + return b ? "true" : "false"; +} + +void printStringForSpecifiedNumberOfTimes(const char* symbolToPrint, int count) { + for (int num = 0; num < count; num++) { + printf("%s", symbolToPrint); + } + printf("\n"); +} +