diff --git a/src/hw2_compilation/CMakeLists.txt b/src/hw2_compilation/CMakeLists.txt new file mode 100644 index 0000000..c57b134 --- /dev/null +++ b/src/hw2_compilation/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 4.0) +project(hw2_compilation C) + +set(CMAKE_C_STANDARD 17) + +add_executable(parenthese parenthese.c) +add_executable(substr substr.c) +add_executable(zero_counter zero_counter.c) diff --git a/src/hw2_compilation/parenthese.c b/src/hw2_compilation/parenthese.c new file mode 100644 index 0000000..7c839ef --- /dev/null +++ b/src/hw2_compilation/parenthese.c @@ -0,0 +1,52 @@ +#include + +// Функция проверки скобок +int checkBrackets(char str[]) +{ + int depth = 0; // Переменная "глубины", которая будет увеличиваться от открывающихся скобок и уменьшаться от закрывающихся + + for (int i = 0; str[i] != '\0'; i++) { + if (str[i] == '(') { + depth++; + } else if (str[i] == ')') { + depth--; + } + if (depth < 0) { + // Если глубина отрицательна, то строка автоматически несбалансирована. + // Например, если строка начинается с закрывающей скобки + return -1; + } + } + + if (depth == 0) { + return 1; // Строка сбалансирована + } else { + return 0; // Строка не сбалансирована + } +} + +int main(void) +{ + int lineLen = 0; + printf("Введите длину строки, которую собираетесь ввести\n"); + scanf("%d", &lineLen); + if (lineLen <= 0) { + printf("Строка должна быть больше чем 0\n"); + return 1; + } + getchar(); // удаляем \n в конце строки + + printf("Введите строку со скобками (скобками считаются только круглые): "); + char str[lineLen + 1]; // Добавляем 1, так как fgets в конце требует \n и это считается как еще один символ + fgets(str, sizeof(str), stdin); + + int result = checkBrackets(str); + + if (result == 1) { + printf("Строка сбалансирована\n"); + } else { + printf("Строка несбалансирована\n"); + } + + return 0; +} diff --git a/src/hw2_compilation/substr.c b/src/hw2_compilation/substr.c new file mode 100644 index 0000000..3e01e9f --- /dev/null +++ b/src/hw2_compilation/substr.c @@ -0,0 +1,28 @@ +#include +#include + +int main() +{ + char str[512]; + char substr[512]; + + printf("Введите строку: \n"); + fgets(str, sizeof(str), stdin); + str[strcspn(str, "\n")] = '\0'; + // Удаляем символ новой строки + // Потому что fgets() включает символ новой строки в считанную строку + + printf("Введите подстроку: \n"); + fgets(substr, sizeof(substr), stdin); + substr[strcspn(substr, "\n")] = '\0'; + + int count = 0; + int pos = 0; + while (strstr(str + pos, substr)) { + count++; + pos++; + } + + printf("%d\n", count); + return 0; +} diff --git a/src/hw2_compilation/zero_counter.c b/src/hw2_compilation/zero_counter.c new file mode 100644 index 0000000..4b369b0 --- /dev/null +++ b/src/hw2_compilation/zero_counter.c @@ -0,0 +1,29 @@ +#include + +int main() +{ + int n; + int count = 0; + + // Ввод размера массива + printf("Введите размер массива: "); + scanf("%d", &n); + int array[n]; + + // Ввод элементов массива + printf("Введите элементов массива:\n"); + for (int i = 0; i < n; i++) { + scanf("%d", &array[i]); + } + + // Подсчет нулевых элементов + for (int i = 0; i < n; i++) { + if (array[i] == 0) { + count++; + } + } + + // Вывод результата + printf("Количество нулевых элементов: %d\n", count); + return 0; +}