diff --git a/homework3/CMakeLists.txt b/homework3/CMakeLists.txt new file mode 100644 index 0000000..273f811 --- /dev/null +++ b/homework3/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.10) +project(CProgrammingTasks homework3) + +# Включаем предупреждения +if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wpedantic") +endif() + +# Создаем исполняемые файлы для каждой задачи +add_executable(task1 src/task1Balance.c) +add_executable(task2 src/task2Strings.c) +add_executable(task3 src/task3ZeroCount.c ) diff --git a/homework3/src/task1Balance.c b/homework3/src/task1Balance.c new file mode 100644 index 0000000..ef6fbab --- /dev/null +++ b/homework3/src/task1Balance.c @@ -0,0 +1,33 @@ +#include +#include + +int main() +{ + int balance = 0; + char origString[100]; + printf("введите строку: "); + scanf("%99s", origString); + + int l = strlen(origString); + for (int i = 0; i < l; i++) { + char s = origString[i]; + + if (s == '(') + balance++; + if (s == ')') + balance--; + + if (balance < 0) { + printf("не выполняется правило вложенности скобок\n"); + return 0; + } + } + + if (balance != 0) { + printf("не выполняется правило вложенности скобок\n"); + return 0; + } + + printf("баланс скобок соблюдается\n"); + return 0; +} diff --git a/homework3/src/task2Strings.c b/homework3/src/task2Strings.c new file mode 100644 index 0000000..8baa28f --- /dev/null +++ b/homework3/src/task2Strings.c @@ -0,0 +1,41 @@ +#include +#include + +int main() +{ + char s[100]; + char s1[100]; + + printf("input string s: "); + scanf("%99s", s); + + printf("input substring s1: "); + scanf("%99s", s1); + + int count = 0; + int sLength = strlen(s); + int s1Length = strlen(s1); + + if (s1Length == 0 || s1Length > sLength) { + printf("number of occurrences: %d\n", count); + return 0; + } + + for (int i = 0; i <= sLength - s1Length; i++) { + int found = 1; + + for (int j = 0; j < s1Length; j++) { + if (s[i + j] != s1[j]) { + found = 0; + break; + } + } + + if (found) { + count++; + } + } + + printf("number of occurrences '%s' in '%s': %d\n", s1, s, count); + return 0; +} diff --git a/homework3/src/task3ZeroCount.c b/homework3/src/task3ZeroCount.c new file mode 100644 index 0000000..1efbbfb --- /dev/null +++ b/homework3/src/task3ZeroCount.c @@ -0,0 +1,31 @@ +#include + +#define MAXSIZE 100 + +int main() +{ + int arr[MAXSIZE]; + int n; + printf("input the size of the array (no more than %d): ", MAXSIZE); + scanf("%d", &n); + + if (n <= 0 || n > MAXSIZE) { + printf("incorrect array size!"); + return 1; + } + + printf("input %d elemens\n", n); + + for (int i = 0; i < n; i++) { + printf("element %d: ", i + 1); + scanf("%d", &arr[i]); + } + + int zeroCount = 0; + for (int i = 0; i < n; i++) { + if (arr[i] == 0) + zeroCount++; + } + printf("the number of zero elements in the array is %d\n", zeroCount); + return 0; +}