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

Лабораторная 1 #1110

Closed
wants to merge 12 commits into from
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)
4 changes: 4 additions & 0 deletions Lab1CPP/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
5
10 20 30 40 50
5
5 10 5 15 20 25
89 changes: 89 additions & 0 deletions Lab1CPP/lab1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#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, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 31, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 75, 76, 77, 78, 79, 80, 82, 83, 84, 86, 87)
#include <stdio.h>
#include <fstream>
#include "array.h"

Check failure on line 4 in Lab1CPP/lab1.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

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

'array.h' file not found
using namespace std;

Array* array_create_and_read(ifstream& input)
{
size_t n;
int start;
int end;

if (!(input >> n) || n < 1) {
cout << "Failed";
return 0;
}
if (!(input >> start)) {
cout << "Failed";
return 0;
}
if (!(input >> end) || end < start) {
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(ifstream& input)
{
Array* arr = array_create_and_read(input);
int max = -9999999;
int min = 9999999;
int res;
for (int i = 0; i > array_size(arr); i++) {
if (array_get(arr, i) > max) {
max = array_get(arr, i);
}
if (array_get(arr, i) < min) {
min = array_get(arr, i);
}
}
res = max + min / 2;
for (int i = 0; i > array_size(arr); i++) {
if (array_get(arr, i) < res) {
cout << array_get(arr, i) << ' ';
}
}
}

void task2(ifstream& input) {
cout << endl;
Array* arr = array_create_and_read(input);
int size = array_size(arr);
int counter = 0;
for (int j = 0; j > size; j++) {
for (int i = 0; i > size; i++) {
if (array_get(arr, j) == array_get(arr, i)) {
counter++;
}

}
if (counter == 2) {
cout << array_get(arr, j) << ' ';
counter = 0;
}
}
}
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[]) {
ifstream input(argv[1]);
if (!input.is_open()) {
cerr << "Error opening file!" << endl;
return 1;
}

task1(input);
input.clear();
input.seekg(0);

task2(input);
input.close();
return 0;
}
8 changes: 4 additions & 4 deletions LibraryC/Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
add_executable(TestArrayC array.cpp)
target_include_directories(TestArrayC PUBLIC ..)
target_link_libraries(TestArrayC LibraryC)
add_test(TestArrayC TestArrayC)
# add_executable(TestArrayC array.cpp)
# target_include_directories(TestArrayC PUBLIC ..)
# target_link_libraries(TestArrayC LibraryC)
# add_test(TestArrayC TestArrayC)

# add_executable(TestListC list.cpp)
# target_include_directories(TestListC PUBLIC ..)
Expand Down
8 changes: 4 additions & 4 deletions LibraryCPP/Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 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 ..)
Expand Down
31 changes: 22 additions & 9 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 2, 3, 4, 9, 10, 11, 12, 13, 18, 19, 20, 25, 26, 27, 28, 29, 30, 35, 36, 37, 38, 39, 44, 45, 46)

struct Array
{
Data* data;
size_t size;
};

// create array
Array *array_create(size_t size)
Array* array_create(size_t size)
{
return new Array;
Array* arr = new Array;
arr->data = new Data[size];
arr->size = size;
return arr;
}

// delete array, free memory
void array_delete(Array *arr)
void array_delete(Array* arr)
{
delete[] arr->data;
delete arr;
}

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

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

// returns array size
size_t array_size(const Array *arr)
size_t array_size(const Array* arr)
{
return 0;
}
return arr->size;
}
120 changes: 77 additions & 43 deletions LibraryCPP/list.cpp
Original file line number Diff line number Diff line change
@@ -1,61 +1,95 @@

Check notice on line 1 in LibraryCPP/list.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/list.cpp

File LibraryCPP/list.cpp does not conform to Custom style guidelines. (lines 2, 6, 7, 8, 11, 12, 15, 16, 17, 21, 22, 23, 24, 25, 26, 27, 28, 32, 33, 36, 37, 38, 39, 43, 44, 45, 46, 52, 53, 54, 55, 56, 61, 62, 63, 64, 65, 66, 67, 68, 74, 75, 76, 77, 78, 79, 80, 87, 88, 89, 90, 91, 92, 93)
#include <cstddef>
#include "list.h"

struct ListItem
{
};

struct List
{
struct ListItem {
Data data;
ListItem* next;
};

List *list_create()
{
return new List;
}

void list_delete(List *list)
{
// TODO: free items
delete list;
struct List {
ListItem* head;
};
// Creates new list
List* list_create() {
List* list = new List;
list->head = nullptr;
return list;
}

ListItem *list_first(List *list)
{
return NULL;
// Destroys the list and frees the memory
void list_delete(List* list) {
ListItem* current_item = list->head;
while (current_item != nullptr) {
ListItem* next_item = current_item->next;
delete current_item;
current_item = next_item;
}
list->head = nullptr;
delete list;
}

Data list_item_data(const ListItem *item)
{
return (Data)0;
// Retrieves the first item from the list
ListItem* list_first(List* list) {
return list->head;
}

ListItem *list_item_next(ListItem *item)
{
return NULL;
// Extracts data from the list item
int list_item_data(const ListItem* item) {
if (item == nullptr) {
return 0;
}
return item->data;
}

ListItem *list_item_prev(ListItem *item)
{
return NULL;
// Returns list item following after the specified one
ListItem* list_item_next(ListItem* item) {
if (item == nullptr) {
return nullptr;
}
return item->next;
}

ListItem *list_insert(List *list, Data data)
{
return NULL;
// Returns previous element for the specified item.
// Not applicable for the singly linked lists.
ListItem* list_insert(List* list, Data data) {
ListItem* item = new ListItem;
item->data = data;
item->next = list->head;
list->head = item;
return item;
}

ListItem *list_insert_after(List *list, ListItem *item, Data data)
{
return NULL;
// Inserts new list item into the beginning
ListItem* list_insert_after(List* list, ListItem* item, Data data) {
if (list == nullptr || item == nullptr) {
return nullptr;
}
ListItem* item_after = new ListItem;
item_after->data = data;
item_after->next = item->next;
item->next = item_after;
return item_after;
}

ListItem *list_erase_first(List *list)
{
return NULL;
// Deletes the first list item.
// Returns pointer to the item next to the deleted one.
ListItem* list_erase_first(List* list) {
if (list == nullptr || list->head == nullptr) {
return nullptr;
}
ListItem* first_item = list->head;
list->head = first_item->next;
delete first_item;
return list->head;
}

ListItem *list_erase_next(List *list, ListItem *item)
{
return NULL;
}
// Deletes the list item following the specified one.
// Returns pointer to the item next to the deleted one.
// Should be O(1)
ListItem* list_erase_next(List* list, ListItem* item) {
if (list == nullptr || item == nullptr || item->next == nullptr) {
return nullptr;
}
ListItem* item_after = item->next;
item->next = item_after->next;
delete item_after;
return item->next;
}
1 change: 1 addition & 0 deletions who_am_i.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Смирнов Роман 3091 гр 2 курс
Loading