-
Notifications
You must be signed in to change notification settings - Fork 280
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
Lab2 #927
Changes from all commits
81475bf
8399771
b8f41d3
30042f2
c078aa0
27e5819
7fada2d
e419565
61fc5b8
118a98a
2ed22d0
90a39bb
62fae98
a9c5f6b
fa3d447
ebdd177
902ed56
4ff89ad
5dbfa23
2723d67
2eadaba
9502e20
94dec7f
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,76 @@ | ||
#include <array.h> | ||
Check notice on line 1 in Lab1CPP/Lab1.cpp GitHub Actions / cpp-linterRun clang-format on Lab1CPP/Lab1.cpp
|
||
#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; | ||
} |
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 |
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) |
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 GitHub Actions / cpp-linterRun clang-format on Lab2CPP/Lab2.cpp
|
||
#include <fstream> | ||
#include <string> | ||
#include <sstream> | ||
#include <map> | ||
#include "stack.h" | ||
|
||
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]; | ||
} 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; | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
push 1 | ||
pop A | ||
call | ||
ret |
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) |
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 GitHub Actions / cpp-linterRun clang-format on LibraryCPP/array.cpp
|
||
|
||
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; | ||
} |
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.
Пример входных данных:
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.
Этот пример не должен выдавать ошибку.