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

Lab2 #927

Closed
wants to merge 23 commits into from
Closed

Lab2 #927

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
6 changes: 1 addition & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,5 @@ 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)
add_subdirectory(Lab2CPP)
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)
76 changes: 76 additions & 0 deletions Lab1CPP/Lab1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <array.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 4, 6, 8, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 25, 26, 27, 28, 29, 30, 35, 37, 38, 40, 41, 42, 44, 45, 46, 47, 48, 49, 50, 51, 53, 54, 57, 59, 61, 62, 64, 65, 66, 67, 69, 70, 71, 73)

Check failure on line 1 in Lab1CPP/Lab1.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Lab1CPP/Lab1.cpp:1:10 [clang-diagnostic-error]

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

Data task_1(Array*& array, std::ifstream& input) {

Data result = 0;

array = fill_array(input);

Data min = array_get(array, 0);
Data max = array_get(array, 0);

for (size_t i = 1; i < array_size(array); i++) {
Data value = array_get(array, i);
if (value < min) {
min = value;
}
if (value > max) {
max = value;
}
}

double avg = (min + max) / 2.0;

for (size_t i = 0; i < array_size(array); i++) {
if (array_get(array, i) < avg) {
result +=1;
}
}
std::cout << result << std::endl;

return result;
}

void task_2(Array*& array) {

Data max_sum = 0;
size_t start_index = 0;

for (size_t i = 0; i < 5; i++) {
max_sum += array_get(array, i);
}

Data current_sum = max_sum;
for (size_t i = 5; i < array_size(array); i++) {
current_sum = current_sum - array_get(array, i - 5) + array_get(array, i);
if (current_sum > max_sum) {
max_sum = current_sum;
start_index = i - 4;
}
}

for (size_t i = start_index; i < start_index + 5; i++) {
std::cout << "Index: " << i << ", Value: " << array_get(array, i) << std::endl;
}
}
int main(){

std::ifstream input("input.txt");

if (input.is_open()) {
Array* array = NULL;

if (task_1(array, input) != 30) {
std::cout << "task_1 error result!" << std::endl;
}
array_delete(array);

task_2(array);
array_delete(array);
}

input.close();

return 0;
}
4 changes: 4 additions & 0 deletions Lab1CPP/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
10
6 3 7 1 12 5 10 5 9 11
20
4 6 2 3 5 7 3 5 2 3 8 1 6 1 3 8 2 1 2 8
5 changes: 5 additions & 0 deletions Lab2CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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}/input2.txt)
118 changes: 118 additions & 0 deletions Lab2CPP/Lab2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include <iostream>

Check notice on line 1 in Lab2CPP/Lab2.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on Lab2CPP/Lab2.cpp

File Lab2CPP/Lab2.cpp does not conform to Custom style guidelines. (lines 1, 10, 11, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 38, 39, 42, 44, 45, 46, 47, 49, 50, 51, 52, 53, 55, 56, 57, 59, 60, 61, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 111, 112, 113, 115, 116)
#include <fstream>
#include <string>
#include <sstream>
#include <map>
#include "stack.h"

Check failure on line 6 in Lab2CPP/Lab2.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Lab2CPP/Lab2.cpp:6:10 [clang-diagnostic-error]

'stack.h' file not found

enum class CommandType { PUSH, POP, CALL, RET, INVALID };

struct Command {
CommandType type;
size_t value;
};

Command parseCommand(const std::string& input) {
Command cmd = { CommandType::INVALID, 0 };
std::istringstream iss(input);
std::string operation;
iss >> operation;

if (operation == "push") {
cmd.type = CommandType::PUSH;
iss >> cmd.value;
} else if (operation == "pop") {
cmd.type = CommandType::POP;
std::string reg;
iss >> reg;
cmd.value = reg[0];

Check warning on line 28 in Lab2CPP/Lab2.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Lab2CPP/Lab2.cpp:28:21 [bugprone-signed-char-misuse]

'signed char' to 'size_t' (aka 'unsigned long') conversion; consider casting to 'unsigned char' first.
} else if (operation == "call") {
cmd.type = CommandType::CALL;
} else if (operation == "ret") {
cmd.type = CommandType::RET;
}

return cmd;
}

bool isValidRegister(char reg) {
return reg >= 'A' && reg <= 'D';
}

int main(int argc, char* argv[]) {

if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <file_path>" << std::endl;
return 1;
}

std::ifstream inputFile(argv[1]);
if (!inputFile.is_open()) {
std::cerr << "Error opening file: " << argv[1] << std::endl;
return 1;
}

Stack* stack = stack_create();
std::map<char, size_t> registers = { {'A', 0}, {'B', 0}, {'C', 0}, {'D', 0} };
const size_t CALL_MARKER = static_cast<size_t>(-1);

std::string input;
while (std::getline(inputFile, input)) {
Command cmd = parseCommand(input);

switch (cmd.type) {
case CommandType::PUSH: {
stack_push(stack, cmd.value);
break;
}
case CommandType::POP: {
if (stack_empty(stack)) {
std::cout << "BAD POP" << std::endl;
inputFile.close();
stack_delete(stack);
return 1;
}

size_t value = stack_get(stack);
if (value == CALL_MARKER || !isValidRegister(static_cast<char>(cmd.value))) {
std::cout << "BAD POP" << std::endl;
inputFile.close();
stack_delete(stack);
return 1;
}

registers[static_cast<char>(cmd.value)] = value;
stack_pop(stack);
break;
}
case CommandType::CALL: {
stack_push(stack, CALL_MARKER);
break;
}
case CommandType::RET: {
if (stack_empty(stack) || stack_get(stack) != CALL_MARKER) {
std::cout << "BAD RET" << std::endl;
inputFile.close();
stack_delete(stack);
return 1;
}
stack_pop(stack);
break;
}
case CommandType::INVALID: {
std::cout << "Invalid command!" << std::endl;
inputFile.close();
stack_delete(stack);
return 1;
}
}
}

for (const auto& reg : registers) {
std::cout << reg.first << " = " << reg.second << std::endl;
}

inputFile.close();
stack_delete(stack);
return 0;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Пример входных данных:

call
call
ret
ret

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Этот пример не должен выдавать ошибку.

4 changes: 4 additions & 0 deletions Lab2CPP/input2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
push 1
pop A
call
ret
2 changes: 1 addition & 1 deletion LibraryCPP/CMakeLists.txt
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_library(LibraryCPP STATIC vector.cpp stack.cpp)

add_subdirectory(Tests)
26 changes: 13 additions & 13 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 All @@ -14,13 +14,13 @@
# 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(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(TestVectorCPP vector.cpp)
target_include_directories(TestVectorCPP PUBLIC ..)
target_link_libraries(TestVectorCPP LibraryCPP)
add_test(TestVectorCPP TestVectorCPP)
set_tests_properties(TestVectorCPP PROPERTIES TIMEOUT 10)
4 changes: 2 additions & 2 deletions LibraryCPP/Tests/vector.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <iostream>

Check notice on line 1 in LibraryCPP/Tests/vector.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/Tests/vector.cpp

File LibraryCPP/Tests/vector.cpp does not conform to Custom style guidelines. (lines 1, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 67, 68, 69, 71)
#include "vector.h"

Check failure on line 2 in LibraryCPP/Tests/vector.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/Tests/vector.cpp:2:10 [clang-diagnostic-error]

'vector.h' file not found

int main()
{
Expand All @@ -17,7 +17,7 @@

for (size_t i = 0 ; i < vector_size(vector) ; ++i)
{
if (vector_get(vector, i) != (int)i)
if (vector_get(vector, i) != (Data)i)
{
std::cout << "Invalid vector element " << i << "\n";
return 1;
Expand Down Expand Up @@ -45,7 +45,7 @@

for (size_t i = 0 ; i < vector_size(vector) ; ++i)
{
if (vector_get(vector, i) != (int)i)
if (vector_get(vector, i) != (Data)i)
{
std::cout << "Invalid vector element " << i << "\n";
return 1;
Expand Down
50 changes: 45 additions & 5 deletions LibraryCPP/array.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,74 @@
#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 3, 4, 5, 6, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 25, 26, 27, 28, 29, 34, 35, 36, 37, 38, 43, 44, 45, 46, 47, 52, 53, 54, 55, 56, 61, 62, 63, 64, 65, 66, 68, 69, 70, 71)

struct Array
{
Data* array; //pointer to a dynamic array
size_t size; //array size
};

// create array
Array *array_create(size_t size)
{
return new Array;
if(size != 0) {
Array* array = new Array;
array->size = size;
array->array = new Data[size];

return array;
}
else {
return nullptr;
}
}

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

// returns specified array element
Data array_get(const Array *arr, size_t index)
Data array_get(const Array* arr, size_t index)
{
return (Data)0;
if (index < arr->size)
return arr->array[index];
else
return 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->array[index] = value;
}
}

// returns array size
size_t array_size(const Array *arr)
{
if (arr->array != nullptr) {
return arr->size;
}
return 0;
}

// fill array from file
Array* fill_array(std::ifstream &input)
{
size_t size;
Data value;
input >> size;
Array* array = array_create(size);

for (size_t i = 0; i < size; i++) {
input >> value;
array_set(array, i, value);
}

return array;
}
Loading
Loading