-
Notifications
You must be signed in to change notification settings - Fork 279
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
Lab1 #1106
Changes from all commits
7793a27
a1a2084
3c1dd50
40fd722
3a3834c
102c123
7b08675
7a64894
a968702
db7a462
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) |
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
30 2 5 | ||
50 -10 10 | ||
-10 0 |
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 GitHub Actions / cpp-linterRun clang-format on Lab1CPP/lab1.cpp
|
||
#include <iostream> | ||
#include <fstream> | ||
#include <ctime> | ||
#include "array.h" | ||
|
||
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) << " "; | ||
} | ||
} | ||
|
||
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; | ||
} |
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) |
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) |
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 GitHub Actions / cpp-linterRun clang-format on LibraryCPP/Tests/array.cpp
|
||
#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; | ||
} | ||
} | ||
std::cout << "\nTest end\n"; | ||
|
||
array_delete(arr); | ||
} |
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 GitHub Actions / cpp-linterRun clang-format on LibraryCPP/Tests/list.cpp
|
||
#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); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А где заполнение хвоста массива нулями?