Skip to content
Closed

lab3 #1616

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
1 change: 1 addition & 0 deletions 123123
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123123
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ 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(LibraryCPPClass)
#add_subdirectory(LibraryCPPTemplate)

add_subdirectory(Lab1C)
add_subdirectory(Lab3CPP)
13 changes: 13 additions & 0 deletions Lab1CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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}/input1.txt)
set_tests_properties(TestLab1CPP PROPERTIES
PASS_REGULAR_EXPRESSION "7.*9 4 12 11 13"
)

add_test(NAME TestLab1CPP2 COMMAND Lab1CPP ${CMAKE_CURRENT_SOURCE_DIR}/input2.txt)
set_tests_properties(TestLab1CPP2 PROPERTIES
PASS_REGULAR_EXPRESSION "0.*25 26 27 28 29"
)
4 changes: 4 additions & 0 deletions Lab1CPP/input1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
10
2 3 4 10 11 13 16 17 21 25
9
7 6 5 8 9 4 12 11 13
3 changes: 3 additions & 0 deletions Lab1CPP/input2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0
9
21 22 23 24 25 26 27 28 29
81 changes: 81 additions & 0 deletions Lab1CPP/lab1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <iostream>
#include <fstream>
#include <string>
#include "array.h"

using namespace std;

Array* array_create_and_write(ifstream& input)
{
int n;
input >> n;
/* Create array */
Array* arr = array_create(n);
/* Read array data */
for (int i = 0; i < n; ++i)
{
int x;
input >> x;
array_set(arr, i, x);
}
return arr;
}

void task1(Array* arr)
{
if (!arr) return;
size_t size = array_size(arr);
int count = 0;
for (size_t i = 0; i < size; ++i) {
int value = array_get(arr, i);
for (int d = 2; d <= 9; ++d) {
if (value % d == 0) {
count++;
break;
}
}
}
cout << count << endl;
}

void task2(Array* arr)
{
size_t size = array_size(arr);
if (size < 5) return;
int max_sum = 0;
size_t max_index = 0;
for (size_t i = 0; i < 5; ++i) max_sum += array_get(arr, i);

int cur_sum = max_sum;
for (size_t i = 1; i <= size - 5; ++i) {
cur_sum = cur_sum - array_get(arr, i - 1) + array_get(arr, i + 4);
if (cur_sum > max_sum) {
max_sum = cur_sum;
max_index = i;
}
}
for (size_t i = 0; i < 5; ++i) {
cout << array_get(arr, max_index + i) << (i < 4 ? " " : "\n");
}
}

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

ifstream input(argv[1]);;

Array* arr = array_create_and_write(input);

task1(arr);
array_delete(arr);

/* Create another array here */
arr = array_create_and_write(input);

task2(arr);
array_delete(arr);
input.close();
}
20 changes: 20 additions & 0 deletions Lab2CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
add_executable(Lab2CPP lab2.cpp)
target_include_directories(Lab2CPP PUBLIC ../LibraryCPP)
target_link_libraries(Lab2CPP LibraryCPP)

add_test(NAME TestLab2CPP COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/input1.txt)
set_property(TEST TestLab2CPP PROPERTY PASS_REGULAR_EXPRESSION "5 12 34")

add_test(NAME TestLab2CPP2 COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/input2.txt)
set_property(TEST TestLab2CPP2 PROPERTY PASS_REGULAR_EXPRESSION "8 11 32 54 65")

add_test(NAME TestLab2CPP3 COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/input3.txt)
set_property(TEST TestLab2CPP3 PROPERTY PASS_REGULAR_EXPRESSION "2 11 26 33 56 78 84 96")

add_test(NAME TestLab2CPP4 COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/input4.txt)
set_property(TEST TestLab2CPP4 PROPERTY PASS_REGULAR_EXPRESSION "3 13 15 21 38 54 67 99")

add_test(NAME TestLab2CPP5 COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/input5.txt)
set_property(TEST TestLab2CPP5 PROPERTY PASS_REGULAR_EXPRESSION "5 11 23 29 30 41 49 64 81")

add_test(NAME TestLab2CPP6 COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/input_big.txt)
1 change: 1 addition & 0 deletions Lab2CPP/input1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12 34 5
1 change: 1 addition & 0 deletions Lab2CPP/input2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
65 32 8 54 11
1 change: 1 addition & 0 deletions Lab2CPP/input3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
78 26 33 2 56 84 96 11
1 change: 1 addition & 0 deletions Lab2CPP/input4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
99 3 67 21 54 13 15 38
1 change: 1 addition & 0 deletions Lab2CPP/input5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5 64 23 11 29 30 81 49 41
1 change: 1 addition & 0 deletions Lab2CPP/input_big.txt

Large diffs are not rendered by default.

102 changes: 102 additions & 0 deletions Lab2CPP/lab2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <iostream>
#include <fstream>
#include "vector.h"
#include "stack.h"

using namespace std;

void merge(Vector* v, size_t left, size_t mid, size_t right, Vector* temp) {
size_t i = left, j = mid, k = left;
while (i < mid && j < right) {
if (vector_get(v, i) <= vector_get(v, j))
vector_set(temp, k++, vector_get(v, i++));
else
vector_set(temp, k++, vector_get(v, j++));
}
while (i < mid) vector_set(temp, k++, vector_get(v, i++));
while (j < right) vector_set(temp, k++, vector_get(v, j++));
for (size_t t = left; t < right; ++t)
vector_set(v, t, vector_get(temp, t));
}

void merge_sort(Vector* v) {
size_t n = vector_size(v);
if (n <= 1) return;

Vector* temp = vector_create();
vector_resize(temp, n);

Stack* st = stack_create();

stack_push(st, (Data)0);
stack_push(st, (Data)n);
stack_push(st, (Data)0);

while (!stack_empty(st)) {

size_t left = (size_t)stack_get(st); stack_pop(st);
size_t right = (size_t)stack_get(st); stack_pop(st);
int stage = (int)stack_get(st); stack_pop(st);

size_t mid = left + (right - left) / 2;

if (stage == 0) {

if (right - left <= 1)
continue;

stack_push(st, (Data)1);
stack_push(st, (Data)right);
stack_push(st, (Data)left);

stack_push(st, (Data)0);
stack_push(st, (Data)right);
stack_push(st, (Data)mid);

stack_push(st, (Data)0);
stack_push(st, (Data)mid);
stack_push(st, (Data)left);
}
else {
merge(v, left, mid, right, temp);
}
}

stack_delete(st);
vector_delete(temp);
}


void solve_file(const char* filename) {
Vector* v = vector_create();

ifstream file(filename);

int x;
while (file >> x) {
size_t sz = vector_size(v);
vector_resize(v, sz + 1);
vector_set(v, sz, x);
}

merge_sort(v);

for (size_t i = 0; i < vector_size(v); ++i)
cout << vector_get(v, i) << " ";
cout << endl;

vector_delete(v);
}

int main(int argc, char* argv[]) {
if (argc > 1) {
for (int i = 1; i < argc; i++)
solve_file(argv[i]);
}
else {
const char* filenames[] = { "input1.txt", "input2.txt", "input3.txt", "input4.txt", "input5.txt", "input_big.txt" };
for (int i = 0; i < 6; i++)
solve_file(filenames[i]);
}
return 0;
}
39 changes: 39 additions & 0 deletions Lab3CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
cmake_minimum_required(VERSION 3.10)
project(Lab3CPP)

set(CMAKE_CXX_STANDARD 11)

add_executable(Lab3CPP lab3.cpp)
target_include_directories(Lab3CPP PUBLIC ../LibraryCPP)
target_link_libraries(Lab3CPP LibraryCPP)

# Тест с более гибким регулярным выражением
add_test(NAME TestLab3CPP COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/input.txt)
set_tests_properties(TestLab3CPP PROPERTIES
PASS_REGULAR_EXPRESSION "4"
)

add_test(NAME TestLab3CPP1 COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/input1.txt)
set_tests_properties(TestLab3CPP1 PROPERTIES
PASS_REGULAR_EXPRESSION "1"
)

add_test(NAME TestLab3CPP2 COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/input2.txt)
set_tests_properties(TestLab3CPP2 PROPERTIES
PASS_REGULAR_EXPRESSION "5"
)

add_test(NAME TestLab3CPP3 COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/input3.txt)
set_tests_properties(TestLab3CPP3 PROPERTIES
PASS_REGULAR_EXPRESSION "3"
)

add_test(NAME TestLab3CPP4 COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/input4.txt)
set_tests_properties(TestLab3CPP4 PROPERTIES
PASS_REGULAR_EXPRESSION "0"
)

add_test(NAME TestLab3CPP5 COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/input5.txt)
set_tests_properties(TestLab3CPP5 PROPERTIES
PASS_REGULAR_EXPRESSION "2"
)
5 changes: 5 additions & 0 deletions Lab3CPP/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
######
#...X#
#.#.##
#3.4.#
######
5 changes: 5 additions & 0 deletions Lab3CPP/input1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
########
#..1#3.#
#.####.#
#X.....#
########
8 changes: 8 additions & 0 deletions Lab3CPP/input2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
############
#...X###5###
#.#.####.###
#.#......###
#.#####.####
#.#####....#
#...2#####1#
############
8 changes: 8 additions & 0 deletions Lab3CPP/input3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
########
#..3####
#.####5#
#......#
#.######
#X####9#
#......#
########
7 changes: 7 additions & 0 deletions Lab3CPP/input4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
########
##....X#
##.###.#
##..0#.#
#7####.#
#......#
########
5 changes: 5 additions & 0 deletions Lab3CPP/input5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
##########
#...X....#
#.###.##9#
#.....2..#
##########
Loading