Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions AvtomatikaTestTask1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <stdio.h>
#include <stdbool.h>
#include <Windows.h>


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");
}