Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions homework3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 )
33 changes: 33 additions & 0 deletions homework3/src/task1Balance.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <string.h>

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;
}
41 changes: 41 additions & 0 deletions homework3/src/task2Strings.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <string.h>

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;
}
31 changes: 31 additions & 0 deletions homework3/src/task3ZeroCount.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdio.h>

#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;
}