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 #1106

Closed
wants to merge 10 commits into from
Closed

Lab1 #1106

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
36 changes: 18 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
cmake_minimum_required(VERSION 3.15)
project(Algorithms)

enable_testing()
set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
if (MSVC)
# needs at least cmake 3.15
add_compile_options(/W4 /WX)
else()
add_compile_options(-Wall -Wextra -Wpedantic -Wno-gnu-empty-struct -Wno-unused-parameter)
endif()

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

add_subdirectory(Lab1C)
cmake_minimum_required(VERSION 3.15)
project(Algorithms-1)
enable_testing()
set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
if (MSVC)
# needs at least cmake 3.15
add_compile_options(/W4 /WX)
else()
add_compile_options(-Wall -Wextra -Wpedantic -Wno-gnu-empty-struct -Wno-unused-parameter)
endif()
#add_subdirectory(LibraryC)
add_subdirectory(LibraryCPP)
#add_subdirectory(LibraryCPPClass)
#add_subdirectory(LibraryCPPTemplate)
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 input.txt)
3 changes: 3 additions & 0 deletions Lab1CPP/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
30 2 5
50 -10 10
-10 0
96 changes: 96 additions & 0 deletions Lab1CPP/lab1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <stdio.h>

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, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 34, 35, 36, 37, 38, 40, 41, 42, 43, 44, 45, 46, 48, 51, 52, 53, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 81, 82, 83, 84, 85, 86, 88, 89, 91, 92, 93, 94)
#include <iostream>
#include <fstream>
#include <ctime>
#include "array.h"

Check failure on line 5 in Lab1CPP/lab1.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Lab1CPP/lab1.cpp:5:10 [clang-diagnostic-error]

'array.h' file not found

Array *array_create_and_read(std::ifstream& input)
{
//std::cout << "acar"; //для дебага
size_t n;
int start;
int end;

if (!(input >> n) || n < 1) {
std::cout << "Failed";
return 0;
}
if (!(input >> start)) {
std::cout << "Failed";
return 0;
}
if (!(input >> end) || end < start) {
std::cout << "Failed";
return 0;
}
Array* arr = array_create(n);
for (size_t i = 0; i < n; ++i) {
int x = rand() % (end - start + 1) + start;
array_set(arr, i, x);
}
return arr;
}

void task1(std::ifstream& input)
{
//std::cout << "t1\n"; //для дебага
Array* arr = array_create_and_read(input);
size_t c5 = 0, c4 = 0, c3= 0, c2 = 0;

for(size_t i =0 ; i < array_size(arr); i++) {
std::cout << array_get(arr, i) << " ";
if(array_get(arr, i) == 5) {c5++;}
else if(array_get(arr, i) == 4) {c4++;}
else if(array_get(arr, i) == 3) {c3++;}
else if(array_get(arr, i) == 2) {c2++;}
}

std::cout << "\nколичество 5: " << c5 << "\nколичество 4: " << c4 << "\nколичество 3: " << c3 <<"\nколичество 2: " << c2;
}

void task2(std::ifstream& input)
{
//std::cout << "t2\n"; //для дебага

Array* arr = array_create_and_read(input);
int start, end, counter = 0;
if (!(input >> start)) {
std::cout << "Failed";
return;
}
if (!(input >> end || end < start)) {
std::cout << "Failed";
return;
}

for(size_t i =0 ; i < array_size(arr); i++) {
std::cout << array_get(arr, i) << " ";
int temp = array_get(arr, i); //запоминаю значение
array_set(arr, i, 0); //а в массиве ячейке присаиваю значение 0. Так все ячейки которые не будут изменены дальше будут 0 как раз таки заполняя нулями хвост
if( temp > end || temp < start) {
array_set(arr, counter, temp);
counter++;
}
}
std::cout << "\n\n";
for(size_t i =0 ; i < array_size(arr); i++) { //а тут я уже не меняю массив просто вывожу
std::cout << array_get(arr, i) << " ";
}
Copy link
Owner

Choose a reason for hiding this comment

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

А где заполнение хвоста массива нулями?

}

int main(int argc, char **argv)
{
srand((unsigned int)time(NULL));
//std::ifstream input("input.txt");
std::ifstream input(argv[1]);
if (input.is_open()) {

std::cout << " task 1:\n";
task1(input);

std::cout << "\n---------\n\n task 2:\n";
task2(input);
}
input.close();
return 0;
}
6 changes: 3 additions & 3 deletions 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_subdirectory(Tests)
add_library(LibraryCPP STATIC array.cpp)
add_subdirectory(Tests)
52 changes: 26 additions & 26 deletions LibraryCPP/Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# 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(TestQueueCPP queue.cpp)
# target_include_directories(TestQueueCPP PUBLIC ..)
# target_link_libraries(TestQueueCPP LibraryCPP)
# add_test(TestQueueCPP TestQueueCPP)
# set_tests_properties(TestQueueCPP PROPERTIES TIMEOUT 10)

# add_executable(TestStackCPP stack.cpp)
# target_include_directories(TestStackCPP PUBLIC ..)
# target_link_libraries(TestStackCPP LibraryCPP)
# add_test(TestStackCPP TestStackCPP)

# add_executable(TestVectorCPP vector.cpp)
# target_include_directories(TestVectorCPP PUBLIC ..)
# target_link_libraries(TestVectorCPP LibraryCPP)
# add_test(TestVectorCPP TestVectorCPP)
# set_tests_properties(TestVectorCPP PROPERTIES TIMEOUT 10)
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(TestQueueCPP queue.cpp)
# target_include_directories(TestQueueCPP PUBLIC ..)
# target_link_libraries(TestQueueCPP LibraryCPP)
# add_test(TestQueueCPP TestQueueCPP)
# set_tests_properties(TestQueueCPP PROPERTIES TIMEOUT 10)
# add_executable(TestStackCPP stack.cpp)
# target_include_directories(TestStackCPP PUBLIC ..)
# target_link_libraries(TestStackCPP LibraryCPP)
# add_test(TestStackCPP TestStackCPP)
# add_executable(TestVectorCPP vector.cpp)
# target_include_directories(TestVectorCPP PUBLIC ..)
# target_link_libraries(TestVectorCPP LibraryCPP)
# add_test(TestVectorCPP TestVectorCPP)
# set_tests_properties(TestVectorCPP PROPERTIES TIMEOUT 10)
59 changes: 30 additions & 29 deletions LibraryCPP/Tests/array.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
#include <iostream>
#include "array.h"

int main()
{
Array *arr = array_create(10);

if (array_size(arr) != 10)
{
std::cout << "Invalid array size\n";
array_delete(arr);
return 1;
}

for (int i = 0 ; i < 10 ; ++i)
array_set(arr, i, i * 2);

for (int i = 0 ; i < 10 ; ++i)
{
if (array_get(arr, i) != i * 2)
{
std::cout << "Invalid array element " << i << "\n";
array_delete(arr);
return 1;
}
}

array_delete(arr);
}
#include <iostream>

Check notice on line 1 in LibraryCPP/Tests/array.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/Tests/array.cpp

File LibraryCPP/Tests/array.cpp does not conform to Custom style guidelines. (lines 1, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27)
#include "array.h"

Check failure on line 2 in LibraryCPP/Tests/array.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/Tests/array.cpp:2:10 [clang-diagnostic-error]

'array.h' file not found

int main()
{
Array *arr = array_create(10);

if (array_size(arr) != 10)
{
std::cout << "Invalid array size\n";
array_delete(arr);
return 1;
}

for (int i = 0 ; i < 10 ; ++i)
array_set(arr, i, i * 2);

for (int i = 0 ; i < 10 ; ++i)
{
if (array_get(arr, i) != i * 2)
{
std::cout << "Invalid array element " << i << "\n";
array_delete(arr);
return 1;
}
}
std::cout << "\nTest end\n";

array_delete(arr);
}
96 changes: 48 additions & 48 deletions LibraryCPP/Tests/list.cpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
#include <iostream>
#include "list.h"

int main()
{
List *list = list_create();

if (!list)
{
std::cout << "List creation error\n";
return 1;
}

list_insert(list, 1);
list_insert(list, 2);
list_insert(list, 3);

if (list_item_data(list_first(list)) != 3)
{
std::cout << "list_insert error\n";
return 1;
}

list_insert_after(list, list_first(list), 4);

if (list_item_data(list_item_next(list_first(list))) != 4)
{
std::cout << "list_insert_after error\n";
return 1;
}

list_erase_first(list);

if (list_item_data(list_first(list)) != 4)
{
std::cout << "list_erase error\n";
return 1;
}

std::cout << "List: ";
for (ListItem *item = list_first(list) ; item ; item = list_item_next(item))
{
std::cout << list_item_data(item) << " ";
}
std::cout << "\n";

list_delete(list);
}
#include <iostream>

Check notice on line 1 in LibraryCPP/Tests/list.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/Tests/list.cpp

File LibraryCPP/Tests/list.cpp does not conform to Custom style guidelines. (lines 1, 4, 5, 6, 8, 9, 10, 11, 12, 14, 15, 16, 18, 19, 20, 21, 22, 24, 26, 27, 28, 29, 30, 32, 34, 35, 36, 37, 38, 40, 41, 42, 43, 44, 45)
#include "list.h"

Check failure on line 2 in LibraryCPP/Tests/list.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/Tests/list.cpp:2:10 [clang-diagnostic-error]

'list.h' file not found
int main()
{
List *list = list_create();
if (!list)
{
std::cout << "List creation error\n";
return 1;
}
list_insert(list, 1);
list_insert(list, 2);
list_insert(list, 3);
if (list_item_data(list_first(list)) != 3)
{
std::cout << "list_insert error\n";
return 1;
}
list_insert_after(list, list_first(list), 4);
if (list_item_data(list_item_next(list_first(list))) != 4)
{
std::cout << "list_insert_after error\n";
return 1;
}
list_erase_first(list);
if (list_item_data(list_first(list)) != 4)
{
std::cout << "list_erase error\n";
return 1;
}
std::cout << "List: ";
for (ListItem *item = list_first(list) ; item ; item = list_item_next(item))
{
std::cout << list_item_data(item) << " ";
}
std::cout << "\n";
list_delete(list);
}
Loading
Loading