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

Lab3 #983

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
11 changes: 7 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ 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(Lab1CPP)
add_subdirectory(Lab2CPP)
Dovgalyuk marked this conversation as resolved.
Show resolved Hide resolved
add_subdirectory(Lab3CPP)
# add_subdirectory(LibraryCPPClass)
# add_subdirectory(LibraryCPPTemplate)

add_subdirectory(Lab1C)
# add_subdirectory(Lab1C)
5 changes: 0 additions & 5 deletions Lab1C/CMakeLists.txt

This file was deleted.

4 changes: 0 additions & 4 deletions Lab1C/input.txt

This file was deleted.

40 changes: 0 additions & 40 deletions Lab1C/lab1.c

This file was deleted.

11 changes: 11 additions & 0 deletions Lab1CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
add_executable(Lab1CPP main.cpp lab1.cpp lab1.h)
target_include_directories(Lab1CPP PUBLIC ../LibraryCPP)
target_link_libraries(Lab1CPP LibraryCPP)
add_test(NAME TestLab1CPP COMMAND Lab1CPP ${CMAKE_CURRENT_SOURCE_DIR}/input.txt)


add_executable(MyTest MyArray.cpp lab1.cpp lab1.h)
target_include_directories(MyTest PUBLIC .)
target_include_directories(MyTest PUBLIC ../LibraryCPP)
target_link_libraries(MyTest LibraryCPP)
add_test(NAME Mytest COMMAND MyTest ${CMAKE_CURRENT_SOURCE_DIR}/TestInput.txt)
65 changes: 65 additions & 0 deletions Lab1CPP/MyArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "lab1.h"

Check notice on line 1 in Lab1CPP/MyArray.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on Lab1CPP/MyArray.cpp

File Lab1CPP/MyArray.cpp does not conform to Custom style guidelines. (lines 3, 4, 5, 6, 9, 11, 12, 14, 16, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 33, 34, 36, 37, 38, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 61, 63)

int main(int argc, char **argv)
{
if (argc < 2)
return 1;


FILE *input = fopen(argv[1], "r");

Array *arr = array_create_and_read(input);
arr = task1(arr, input);

Array *correctAns = array_create_and_read(input);

bool isIncorrect = false;

for(size_t i = 0; i < array_size(arr); i++)
{
if(array_get(arr,i) != array_get(correctAns,i))
{
std::cout << "Incorrect answer in " << i << "element" << std::endl;
isIncorrect = true;
}
}

if(isIncorrect)
{
std::cout << "TEST 1 FAIL: wrong answer. Current answer is: -1 4 -12 -10 -1 -19 -12- 2 -11" << std::endl;
return 1;
}

array_delete(arr);
array_delete(correctAns);

arr = array_create_and_read(input);
arr = task2(arr);
correctAns = array_create_and_read(input);

if(array_size(arr) != array_size(correctAns))
{
std::cout << "TEST 2 FAIL: Incorrect array size: " << array_size(arr) << " current size is: " << array_size(correctAns) << std::endl;
return 1;
}

for(size_t i = 0; i < array_size(arr); i++)
{
if(array_get(arr, i) != array_get(correctAns, i))
{
std::cout << "Incorrect element: " << i << std::endl;
isIncorrect = true;
}
}
if(isIncorrect)
{
std::cout << "TEST 2 FAIL: wrong answer. Current answer is: 7 5 3 2";
return 1;
}

array_delete(arr);
array_delete(correctAns);

fclose(input);
return 0;
}
9 changes: 9 additions & 0 deletions Lab1CPP/TestInput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
9
4 9 -2 0 4 -9 -2 3 -1
5 10
9
-1 4 -12 -10 -1 -19 -12 -2 -11
5
7 5 3 2 6
4
7 5 3 2
5 changes: 5 additions & 0 deletions Lab1CPP/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
9
4 9 -2 0 4 -9 -2 3 -1
5 10
5
7 5 3 2 6
85 changes: 85 additions & 0 deletions Lab1CPP/lab1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "lab1.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 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 24, 25, 26, 27, 28, 29, 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 42, 46, 47, 48, 49, 50, 52, 53, 54, 55, 56, 57, 58, 59, 61, 62, 63, 65, 66, 68, 69, 70, 71, 72, 73, 74, 75, 77, 78, 79, 80, 81, 82, 83)

Array *array_create_and_read(FILE *input)
{
int n;
if(fscanf(input, "%d", &n) != 1)
return nullptr;
/* Create array */
Array *arr = array_create(n);
/* Read array data */
for (int i = 0 ; i < n ; ++i)
{
int x;
if(fscanf(input, "%d", &x) != 1)
{
array_delete(arr);
return nullptr;
}
array_set(arr, i, x);
}
return arr;
}

// Ввести целое число, создать массив такой размерности и заполнить его из файла.
// Из всех положительных элементов вычесть элемент с номером k1, из остальных — элемент с номером k2. k1 и k2 ввести из файла.
Array* task1(Array *arr, FILE *input)
{
int k1, k2;
if(fscanf(input, "%d %d", &k1, &k2) != 2)
return nullptr;

Array* ans = array_create(array_size(arr));
for(size_t i = 0; i < array_size(ans); i++)
{
int x = array_get(arr,i);
if(x > 0)
array_set(ans,i,(x - k1));
else
array_set(ans,i,(x - k2));
}

array_delete(arr);
return ans;
}

// Ввести целое число, создать массив такой размерности и заполнить его из файла.
// В массиве найти элементы, которые не делятся ни на какой другой элемент массива.
Array* task2(Array *arr)
{
size_t ansSize = 0;

for(size_t i = 0; i < array_size(arr); i++)
{
size_t j = 0;
for(; j < array_size(arr); j++)
{
if(i != j && array_get(arr,i) % array_get(arr, j) == 0)
break;
}

if(j == array_size(arr))
ansSize++;
}

Array* ans = array_create(ansSize);
size_t index = 0;

for(size_t i = 0; i < array_size(arr); i++)
{
size_t j = 0;
for(; j < array_size(arr); j++)
{
if(i != j && array_get(arr,i) % array_get(arr, j) == 0)
break;
}

if(j == array_size(arr))
{
array_set(ans, index, array_get(arr, i));
index++;
}
}
array_delete(arr);
return ans;
}
12 changes: 12 additions & 0 deletions Lab1CPP/lab1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef LAB1_H

Check notice on line 1 in Lab1CPP/lab1.h

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on Lab1CPP/lab1.h

File Lab1CPP/lab1.h does not conform to Custom style guidelines. (lines 7, 8, 9, 12)
#define LAB1_H

#include "array.h"

Check failure on line 4 in Lab1CPP/lab1.h

View workflow job for this annotation

GitHub Actions / cpp-linter

Lab1CPP/lab1.h:4:10 [clang-diagnostic-error]

'array.h' file not found
#include <iostream>

Array* array_create_and_read(FILE *input);
Array* task1(Array *arr, FILE* input);
Array* task2(Array *arr);


#endif
30 changes: 30 additions & 0 deletions Lab1CPP/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "lab1.h"

Check notice on line 1 in Lab1CPP/main.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on Lab1CPP/main.cpp

File Lab1CPP/main.cpp does not conform to Custom style guidelines. (lines 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28)

int main(int argc, char **argv)
{
if (argc < 2)
return 1;

Array *arr = NULL;
FILE *input = fopen(argv[1], "r");
arr = array_create_and_read(input);
arr = task1(arr, input);
for(size_t i = 0; i < array_size(arr); i++)
{
std::cout << array_get(arr, i) << " ";
}
std::cout << std::endl;
array_delete(arr);
/* Create another array here */
arr = array_create_and_read(input);
arr = task2(arr);
std::cout << array_size(arr) << std::endl;
for(size_t i = 0; i < array_size(arr); i++)
{
std::cout << array_get(arr, i) << " ";
}
std::cout << std::endl;
array_delete(arr);
fclose(input);
return 0;
}
4 changes: 4 additions & 0 deletions Lab2CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_executable(Lab2TaskCPP lab2.cpp lab2.h TestTask.cpp)
target_include_directories(Lab2TaskCPP PUBLIC ../LibraryCPP)
target_link_libraries(Lab2TaskCPP LibraryCPP)
add_test(NAME Lab2TaskCPP COMMAND Lab2TaskCPP ${CMAKE_CURRENT_SOURCE_DIR}/input.txt)
43 changes: 43 additions & 0 deletions Lab2CPP/TestTask.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "lab2.h"

Check notice on line 1 in Lab2CPP/TestTask.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on Lab2CPP/TestTask.cpp

File Lab2CPP/TestTask.cpp does not conform to Custom style guidelines. (lines 3, 4, 5, 6, 8, 9, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39, 41)

int main(int argc, char **argv)
{
if (argc < 2)
return 1;

std::ifstream input;
input.open(argv[1]);

if(task(input) != "NO")
{
std::cout << "Wrong answer: 1 line of input.txt";
return 1;
}

if(task(input) != "NO")
{
std::cout << "Wrong answer: 2 line of input.txt";
return 1;
}

if(task(input) != "YES")
{
std::cout << "Wrong answer: 3 line of input.txt";
return 1;
}

if(task(input) != "NO")
{
std::cout << "Wrong answer: 4 line of input.txt";
return 1;
}

if(task(input) != "NO")
{
std::cout << "Wrong answer: 5 line of input.txt";
return 1;
}

input.close();
return 0;
}
5 changes: 5 additions & 0 deletions Lab2CPP/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
([]][]]]
({)}{([])}
([{}])<><<>>
(((((([{
}[((<>))]{
Loading
Loading