Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5030102/20001_SokolovArtyom_2ndLab #127

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
build
8 changes: 8 additions & 0 deletions lab1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.24)

project(lab1 C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_STANDARD 99)

add_executable(lab1 main.c source/func.c)
8 changes: 8 additions & 0 deletions lab1/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 8 ВАРИАНТ

Текст задания:

Найти количество n-значных чисел в десятичной системе счисления, у каждого из
которых сумма цифр равна k. При этом в качестве n-значного числа мы допускаем и числа,
начинающиеся с одного или нескольких нулей. Например, 000102 рассматривается как
шестизначное число, сумма цифр которого равна 3
15 changes: 15 additions & 0 deletions lab1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>
#include "source/func.h"

int main() {
int n, k;

printf("Enter the bit depth of the number:\n");
scanf_s("%d", &n);
printf("Enter the required sum:\n");
scanf_s("%d", &k);

printf("%d\n", solve(n, k));

return 0;
}
56 changes: 56 additions & 0 deletions lab1/source/func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "func.h"


matrix initMatrix(int n, int k) {
if (n == 0 || k == 0)
return NULL;

matrix result = malloc(sizeof(int*) * (n + 1));

if (result != NULL) {
for (int index = 0; index < n + 1; index++) {
int* value = malloc(sizeof(int) * (k + 1));

if (value == NULL)
return NULL;

value[0] = 1;
result[index] = value;
}

for (int index = 0; index < k + 1; index++) {
result[0][index] = 0;
}
}

return result;
}

void destrMatrix(int n, int k, matrix matr) {
if (matr == NULL)
return;

for (int index = 0; index < n + 1; index++) {
free(matr[index]);
}
}

int solve(int n, int k) {
matrix matr = initMatrix(n, k);

if (matr == NULL)
return -1;

for (int row = 1; row < n + 1; row++) {
for (int col = 1; col < k + 1; col++) {
matr[row][col] = matr[row - 1][col] + matr[row][col - 1];
}
}

int result = matr[n][k];

destrMatrix(n, k, matr);
free(matr);

return result;
}
11 changes: 11 additions & 0 deletions lab1/source/func.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <malloc.h>

typedef int** matrix;

matrix initMatrix(int n, int k);

void destrMatrix(int n, int k, matrix matr);

int solve(int n, int k);
45 changes: 45 additions & 0 deletions lab1/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
cmake_minimum_required(VERSION 3.24)

project(lab1_tests C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_STANDARD 99)


include(FetchContent)

FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

set(NAME lab1_test)

set(PATH ../source)

set(FILE func_test.cpp)

set(SOURCE_FILE ${PATH}/func.c)

enable_testing()

add_executable(
${NAME}
${SOURCE_FILE}
${FILE}
)

target_include_directories(
${NAME}
PRIVATE ${PATH})

target_link_libraries(
${NAME}
GTest::gtest_main
)

include(GoogleTest)
gtest_discover_tests(${NAME})
54 changes: 54 additions & 0 deletions lab1/tests/func_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <chrono>
#include <gtest/gtest.h>

extern "C" {
#include "func.h"
}

TEST(MAIN_TEST, solve_n0k0_0Returned) {
// arrange
int n = 0, k = 0;
int expected = -1;

// act
int actual = solve(n, k);

// assert
ASSERT_EQ(expected, actual);
}

TEST(MAIN_TEST, solve_n1k1_1Returned) {
// arrange
int n = 1, k = 1;
int expected = 1;

// act
int actual = solve(n, k);

// assert
ASSERT_EQ(expected, actual);
}

TEST(MAIN_TEST, solve_n2k17_2Returned) {
// arrange
int n = 5, k = 5;
int expected = 126;

// act
int actual = solve(n, k);

// assert
ASSERT_EQ(expected, actual);
}

TEST(SUBFUNCTION_TEST, initMatrix_n0k0_NULLReturned) {
// arrange
int n = 0, k = 0;
int** expected = NULL;

// act
int** actual = initMatrix(n, k);

// assert
ASSERT_EQ(expected, actual);
}
2 changes: 2 additions & 0 deletions lab2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
build
1 change: 0 additions & 1 deletion lab2/.gitkeep

This file was deleted.

10 changes: 10 additions & 0 deletions lab2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.24)

project(lab2 C CXX)

set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT lab2)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_STANDARD 99)

add_executable(lab2 main.c source/func.c)
25 changes: 25 additions & 0 deletions lab2/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>
#include "source/func.h"


int main() {
RBNode* root = createNode(20, 20);
root = insert(root, root);
balance(&root, root);

for (int index = 0; index < 40; index++) {
RBNode* new_node = createNode(index, index);
new_node = insert(new_node, root);

balance(&root, new_node);
}

removeNode(&root, 39);

// exp
int expected = 38;

// act
int actual = get(root, 38)->value;
printf("%d", actual);
}
Loading