-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvtomatikaTestTask1.cpp
More file actions
108 lines (79 loc) · 2.52 KB
/
Copy pathAvtomatikaTestTask1.cpp
File metadata and controls
108 lines (79 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#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");
}