-
Notifications
You must be signed in to change notification settings - Fork 291
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
Лабораторная 1 #1110
Changes from all commits
97794fe
14116b9
c579cfd
94aa16b
a380910
dd15395
410b619
e84ee9f
456ce33
9e292d4
e96d9c5
fcc5fce
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 |
---|---|---|
@@ -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) |
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 |
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
|
||
#include <stdio.h> | ||
#include <fstream> | ||
#include "array.h" | ||
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; | ||
} | ||
} | ||
} | ||
|
||
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; | ||
} |
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
|
||
|
||
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,95 @@ | ||
|
||
Check notice on line 1 in LibraryCPP/list.cpp
|
||
#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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Смирнов Роман 3091 гр 2 курс |
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.
Будет неправильно работать даже для теста, который есть в репозитории.