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

lab1 #1238

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

lab1 #1238

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
1 change: 1 addition & 0 deletions Anisimov_Ilya/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Анисимов Илья Азатович группа 4097ду
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ else()
add_compile_options(-Wall -Wextra -Wpedantic -Wno-gnu-empty-struct -Wno-unused-parameter)
endif()

add_subdirectory(LibraryC)
#add_subdirectory(LibraryC)
add_subdirectory(LibraryCPP)
add_subdirectory(LibraryCPPClass)
add_subdirectory(LibraryCPPTemplate)
#add_subdirectory(LibraryCPPClass)
#add_subdirectory(LibraryCPPTemplate)

add_subdirectory(Lab1C)
add_subdirectory(Lab1CPP)
5 changes: 5 additions & 0 deletions Lab1CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_executable(Lab1CPP lab1.cpp)
target_include_directories(Lab1CPP PUBLIC ../LibraryCPP)
target_link_libraries(Lab1CPP LibraryCPP)

add_test(NAME TestLab1CPP COMMAND Lab1CPP ${CMAKE_CURRENT_SOURCE_DIR}/input.txt)
1 change: 1 addition & 0 deletions Lab1CPP/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10 20 50
83 changes: 83 additions & 0 deletions Lab1CPP/lab1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <iostream>

Check notice on line 1 in Lab1CPP/lab1.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on Lab1CPP/lab1.cpp

File Lab1CPP/lab1.cpp does not conform to Custom style guidelines. (lines 1, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 26, 28, 29, 30, 31, 32, 33, 34, 36, 37, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 53, 54, 55, 57, 58, 59, 60, 61, 63, 64, 65, 66, 67, 69, 70, 72, 74, 75, 76, 77, 78, 80)
#include <fstream>
#include <cstdlib>
#include <ctime>
#include "../LibraryCPP/array.h"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут не нужно указывать путь, потому что он уже есть в CMakeLists.txt


size_t find_max_odd_segment(const Array* arr) {
size_t max_length = 0;
size_t current_length = 0;

for (size_t i = 0; i < array_size(arr); ++i) {
if (array_get(arr, i) % 2 != 0) {
current_length++;
if (current_length > max_length) {
max_length = current_length;
}
} else {
current_length = 0;
}
}

return max_length;
}

void compress_array(Array* arr, int a, int b) {
size_t write_index = 0;

for (size_t read_index = 0; read_index < array_size(arr); ++read_index) {
int value = array_get(arr, read_index);
if (value < a || value > b) {
array_set(arr, write_index, value);
write_index++;
}
}

for (size_t i = write_index; i < array_size(arr); ++i) {
array_set(arr, i, 0);
}
}

int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Èñïîëüçîâàíèå: " << argv[0] << " <input_file>" << std::endl;
return 1;
}

std::ifstream input_file(argv[1]);
if (!input_file.is_open()) {
std::cerr << "Îøèáêà: íå óäàëîñü îòêðûòü ôàéë " << argv[1] << std::endl;
return 1;
}

size_t size;
int a, b;
input_file >> size >> a >> b;

Array* arr = array_create(size);
std::srand(std::time(nullptr));
for (size_t i = 0; i < size; ++i) {
array_set(arr, i, std::rand() % 100);
}

std::cout << "Èñõîäíûé ìàññèâ: ";
for (size_t i = 0; i < array_size(arr); ++i) {
std::cout << array_get(arr, i) << " ";
}
std::cout << std::endl;

size_t max_odd_length = find_max_odd_segment(arr);
std::cout << "Ìàêñèìàëüíàÿ äëèíà îòðåçêà èç íå÷åòíûõ ÷èñåë: " << max_odd_length << std::endl;

compress_array(arr, a, b);

std::cout << "Ñæàòûé ìàññèâ: ";
for (size_t i = 0; i < array_size(arr); ++i) {
std::cout << array_get(arr, i) << " ";
}
std::cout << std::endl;

array_delete(arr);

return 0;
}
2 changes: 1 addition & 1 deletion LibraryCPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
add_library(LibraryCPP STATIC array.cpp list.cpp queue.cpp stack.cpp vector.cpp)
add_library(LibraryCPP STATIC array.cpp)

add_subdirectory(Tests)
16 changes: 8 additions & 8 deletions LibraryCPP/Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# add_executable(TestArrayCPP array.cpp)
# target_include_directories(TestArrayCPP PUBLIC ..)
# target_link_libraries(TestArrayCPP LibraryCPP)
# add_test(TestArrayCPP TestArrayCPP)
add_executable(TestArrayCPP array.cpp)
target_include_directories(TestArrayCPP PUBLIC ..)
target_link_libraries(TestArrayCPP LibraryCPP)
add_test(TestArrayCPP TestArrayCPP)

# add_executable(TestListCPP list.cpp)
# target_include_directories(TestListCPP PUBLIC ..)
# target_link_libraries(TestListCPP LibraryCPP)
# add_test(TestListCPP TestListCPP)
#add_executable(TestListCPP array.cpp)
#target_include_directories(TestListCPP PUBLIC ..)
#target_link_libraries(TestListCPP LibraryCPP)
#add_test(TestListCPP TestListCPP)

# add_executable(TestQueueCPP queue.cpp)
# target_include_directories(TestQueueCPP PUBLIC ..)
Expand Down
27 changes: 20 additions & 7 deletions LibraryCPP/array.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
#include "array.h"

Check notice on line 1 in LibraryCPP/array.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/array.cpp

File LibraryCPP/array.cpp does not conform to Custom style guidelines. (lines 3, 4, 5, 6, 7, 8, 9, 14, 15, 16, 20, 21, 22, 26, 27, 28, 29, 30, 35, 36, 37, 38, 39, 44, 45, 46)

struct Array

Check warning on line 3 in LibraryCPP/array.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/array.cpp:3:8 [cppcoreguidelines-special-member-functions]

class 'Array' defines a non-default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator
{
Data* data;
size_t size;
explicit Array(const size_t arr_size): data(new Data[arr_size]), size(arr_size) {}

Check warning on line 7 in LibraryCPP/array.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/array.cpp:7:5 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: size
~Array() {
delete[] data;
}
};

// create array
Array *array_create(size_t size)
Array *array_create(const size_t size)
{
return new Array;
return new Array(size);
}

// delete array, free memory
void array_delete(Array *arr)
void array_delete(Array* arr)
{
delete arr;
}

// returns specified array element
Data array_get(const Array *arr, size_t index)
Data array_get(const Array *arr, const size_t index)
{
return (Data)0;
if (index >= arr->size) {
return 0;
}
return arr->data[index];
}

// sets the specified array element to the value
void array_set(Array *arr, size_t index, Data value)
{
if (!arr || index >= arr->size) {
return;
}
arr->data[index] = value;
}

// returns array size
size_t array_size(const Array *arr)
{
return 0;
}
return arr->size;
}
4 changes: 2 additions & 2 deletions LibraryCPP/array.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef ARRAY_H
#define ARRAY_H

#include <cstddef>

Check failure on line 4 in LibraryCPP/array.h

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/array.h:4:10 [clang-diagnostic-error]

'cstddef' file not found

// Non-resizeable array
// Stores integer values inside
Expand All @@ -10,13 +10,13 @@
struct Array;

// create array
Array *array_create(size_t size);
Array *array_create(const size_t size);

// delete array, free memory
void array_delete(Array *arr);

// returns specified array element
Data array_get(const Array *arr, size_t index);
Data array_get(const Array *arr, const size_t index);

// sets the specified array element to the value
void array_set(Array *arr, size_t index, Data value);
Expand Down