diff --git a/CMakeLists.txt b/CMakeLists.txt index b40c0dd11c..b94423fd2a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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(Lab2CPP) diff --git a/Lab1C/CMakeLists.txt b/Lab1C/CMakeLists.txt deleted file mode 100644 index 596b3f0237..0000000000 --- a/Lab1C/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -add_executable(Lab1C lab1.c) -target_include_directories(Lab1C PUBLIC ../LibraryC) -target_link_libraries(Lab1C LibraryC) - -add_test(NAME TestLab1C COMMAND Lab1C ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) diff --git a/Lab1C/input.txt b/Lab1C/input.txt deleted file mode 100644 index 2f173150b0..0000000000 --- a/Lab1C/input.txt +++ /dev/null @@ -1,4 +0,0 @@ -10 -1 2 3 4 5 6 7 8 9 0 -4 -1 2 3 4 diff --git a/Lab1C/lab1.c b/Lab1C/lab1.c deleted file mode 100644 index fdf6ed1527..0000000000 --- a/Lab1C/lab1.c +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include "array.h" - -Array *array_create_and_read(FILE *input) -{ - int n; - fscanf(input, "%d", &n); - /* Create array */ - Array *arr = array_create(n, NULL); - /* Read array data */ - for (int i = 0 ; i < n ; ++i) - { - int x; - fscanf(input, "%d", &x); - array_set(arr, i, x); - } - return arr; -} - -void task1(Array *arr) -{ -} - -void task2(Array *arr) -{ -} - -int main(int argc, char **argv) -{ - Array *arr = NULL; - FILE *input = fopen(argv[1], "r"); - arr = array_create_and_read(input); - task1(arr); - array_delete(arr); - /* Create another array here */ - arr = array_create_and_read(input); - task2(arr); - array_delete(arr); - fclose(input); -} diff --git a/Lab2CPP/CMakeLists.txt b/Lab2CPP/CMakeLists.txt new file mode 100644 index 0000000000..ec593b4476 --- /dev/null +++ b/Lab2CPP/CMakeLists.txt @@ -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}/input.txt) diff --git a/Lab2CPP/input.txt b/Lab2CPP/input.txt new file mode 100644 index 0000000000..c7950ae0a1 --- /dev/null +++ b/Lab2CPP/input.txt @@ -0,0 +1,7 @@ +1+2*3-1 +(2+2)*3 +4+1- +2*3+(4+4 +3/3*(2+5))+1 +4*2+1) +) \ No newline at end of file diff --git a/Lab2CPP/lab2.cpp b/Lab2CPP/lab2.cpp new file mode 100644 index 0000000000..0e426bc08d --- /dev/null +++ b/Lab2CPP/lab2.cpp @@ -0,0 +1,127 @@ +#include +#include +#include +#include "stack.h" +#include "vector.h" +#include + +int precedence(char op) { + if (op == '+' || op == '-') return 1; + if (op == '*' || op == '/') return 2; + return 0; +} + +int apply_operator(int a, int b, char op) { + switch (op) { + case '+': return a + b; + case '-': return a - b; + case '*': return a * b; + case '/': + if (b == 0) throw std::runtime_error("Division by zero"); + return a / b; + default: + throw std::invalid_argument("Invalid operator"); + } +} + +int evaluate_example(const std::string& example) { + Stack* values = stack_create(); + Stack* operators = stack_create(); + + for (size_t i = 0; i < example.length(); i++) { + if (example[i] == ' ') continue; + + if (isdigit(example[i])) { + int value = 0; + while (i < example.length() && isdigit(example[i])) { + value = value * 10 + (example[i] - '0'); + i++; + } + i--; + stack_push(values, value); + } + else if (example[i] == '(') { + stack_push(operators, example[i]); + } + else if (example[i] == ')') { + while ( stack_get(operators) != '(') { + + int val2 = stack_get(values); + stack_pop(values); + + + int val1 = stack_get(values); + stack_pop(values); + + + char op = (char)stack_get(operators); + stack_pop(operators); + + stack_push(values, apply_operator(val1, val2, op)); + } + if (!stack_empty(operators)) { + stack_pop(operators); + } + else { + throw std::runtime_error("Mismatched parentheses: no matching '(' found"); + } + } + else if (example[i] == '+' || example[i] == '-' || example[i] == '*' || example[i] == '/') { + while (!stack_empty(operators) && precedence((char)stack_get(operators)) >= precedence(example[i])) { + + int val2 = stack_get(values); + stack_pop(values); + + int val1 = stack_get(values); + stack_pop(values); + + + char op = (char)stack_get(operators); + stack_pop(operators); + + stack_push(values, apply_operator(val1, val2, op)); + } + stack_push(operators, example[i]); + } + } + + while (!stack_empty(operators)) { + + int val2 = stack_get(values); + stack_pop(values); + + + int val1 = stack_get(values); + stack_pop(values); + + + char op = (char)stack_get(operators); + stack_pop(operators); + + stack_push(values, apply_operator(val1, val2, op)); + } + + + int result = stack_get(values); + stack_delete(values); + stack_delete(operators); + return result; + +} + +int main() { + std::ifstream inputFile("input.txt"); + std::string example; + + while (std::getline(inputFile, example)) { + try { + int result = evaluate_example(example); + std::cout << "Result: " << result << std::endl; + } + catch (const std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/LibraryC/Tests/CMakeLists.txt b/LibraryC/Tests/CMakeLists.txt index b182dee615..dab8e15fd5 100644 --- a/LibraryC/Tests/CMakeLists.txt +++ b/LibraryC/Tests/CMakeLists.txt @@ -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 ..) diff --git a/LibraryCPP/CMakeLists.txt b/LibraryCPP/CMakeLists.txt index dbd7769feb..e582b1d20b 100644 --- a/LibraryCPP/CMakeLists.txt +++ b/LibraryCPP/CMakeLists.txt @@ -1,3 +1,3 @@ -add_library(LibraryCPP STATIC array.cpp list.cpp queue.cpp stack.cpp vector.cpp) +add_library(LibraryCPP STATIC stack.cpp vector.cpp) add_subdirectory(Tests) diff --git a/LibraryCPP/Tests/CMakeLists.txt b/LibraryCPP/Tests/CMakeLists.txt index c903f9d7f3..fa94ff0a66 100644 --- a/LibraryCPP/Tests/CMakeLists.txt +++ b/LibraryCPP/Tests/CMakeLists.txt @@ -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 ..) @@ -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) diff --git a/LibraryCPP/Tests/array.cpp b/LibraryCPP/Tests/array.cpp index 69f54e42c8..16b69741a1 100644 --- a/LibraryCPP/Tests/array.cpp +++ b/LibraryCPP/Tests/array.cpp @@ -3,7 +3,7 @@ int main() { - Array *arr = array_create(10); + Array* arr = array_create(10); if (array_size(arr) != 10) { diff --git a/LibraryCPP/Tests/vector.cpp b/LibraryCPP/Tests/vector.cpp index 525ed56ef9..6784aededa 100644 --- a/LibraryCPP/Tests/vector.cpp +++ b/LibraryCPP/Tests/vector.cpp @@ -13,7 +13,7 @@ int main() } for (size_t i = 0 ; i < vector_size(vector) ; ++i) - vector_set(vector, i, i); + vector_set(vector, i, static_cast(i)); for (size_t i = 0 ; i < vector_size(vector) ; ++i) { @@ -61,7 +61,7 @@ int main() for (int i = 1 ; i <= 10000000 ; ++i) { vector_resize(vector, i); - vector_set(vector, i - 1, i); + vector_set(vector, static_cast(i) - 1, i); } long long sum = 0; diff --git a/LibraryCPP/array.cpp b/LibraryCPP/array.cpp index c773e5f167..602de84d72 100644 --- a/LibraryCPP/array.cpp +++ b/LibraryCPP/array.cpp @@ -1,34 +1,58 @@ #include "array.h" +#include struct Array { + size_t size; + Data* data; }; // create array Array *array_create(size_t size) { - return new Array; + if (size == 0) { + throw std::invalid_argument("Size must be greater than 0"); + } + + Array* arr = new Array; + arr->size = size; + arr->data = new Data[size]; + return arr; + } // delete array, free memory void array_delete(Array *arr) { - delete arr; + if (arr) { + delete[] arr->data; + delete arr; + } + } // returns specified array element Data array_get(const Array *arr, size_t index) { - return (Data)0; + if (index >= arr->size) { + throw std::out_of_range("Index out of range"); + } + return arr->data[index]; + } // sets the specified array element to the value void array_set(Array *arr, size_t index, Data value) { + if (index >= arr->size) { + throw std::out_of_range("Index out of range"); + } + arr->data[index] = value; } // returns array size size_t array_size(const Array *arr) { - return 0; + return arr->size; + } diff --git a/LibraryCPP/stack.cpp b/LibraryCPP/stack.cpp index c090abba1c..0ddccaddef 100644 --- a/LibraryCPP/stack.cpp +++ b/LibraryCPP/stack.cpp @@ -1,34 +1,43 @@ +#include "vector.h" #include "stack.h" +#include +using namespace std; -struct Stack -{ +struct Stack { + Vector* vector; }; -Stack *stack_create() -{ - return new Stack; +Stack* stack_create() { + Stack* stack = new Stack; + stack->vector = vector_create(); + return stack; } -void stack_delete(Stack *stack) -{ - // TODO: free stack elements +void stack_delete(Stack* stack) { + vector_delete(stack->vector); delete stack; } -void stack_push(Stack *stack, Data data) -{ +void stack_push(Stack* stack, Data data) { + vector_resize(stack->vector, vector_size(stack->vector) + 1); + vector_set(stack->vector, vector_size(stack->vector) - 1, data); } -Data stack_get(const Stack *stack) -{ - return (Data)0; +Data stack_get(const Stack* stack) { + if (stack_empty(stack)) { + throw std::out_of_range("Stack is empty"); + } + return vector_get(stack->vector, vector_size(stack->vector) - 1); + } -void stack_pop(Stack *stack) -{ +void stack_pop(Stack* stack) { + if (stack_empty(stack)) { + throw std::out_of_range("Stack is empty"); + } + vector_resize(stack->vector, vector_size(stack->vector) - 1); } -bool stack_empty(const Stack *stack) -{ - return true; +bool stack_empty(const Stack* stack) { + return vector_size(stack->vector) == 0; } diff --git a/LibraryCPP/vector.cpp b/LibraryCPP/vector.cpp index aee7157b54..48c5732835 100644 --- a/LibraryCPP/vector.cpp +++ b/LibraryCPP/vector.cpp @@ -1,34 +1,53 @@ #include "vector.h" - -struct Vector -{ +#include +struct Vector { + Data* data; + size_t size; + size_t volume; }; -Vector *vector_create() -{ - return new Vector; +Vector* vector_create() { + Vector* vector = new Vector; + vector->size = 0; + vector->volume = 4; + vector->data = new Data[vector->volume]; + return vector; } -void vector_delete(Vector *vector) -{ - // TODO: free vector internals - delete vector; +void vector_delete(Vector* vector) { + delete[] vector->data; + delete vector; } -Data vector_get(const Vector *vector, size_t index) -{ - return (Data)0; +Data vector_get(const Vector* vector, size_t index) { + if (index < vector->size) { + return vector->data[index]; + } + throw std::out_of_range("Index out of range"); } -void vector_set(Vector *vector, size_t index, Data value) -{ +void vector_set(Vector* vector, size_t index, Data value) { + if (index >= vector->size) { + throw std::out_of_range("Index out of range"); + } + vector->data[index] = value; } -size_t vector_size(const Vector *vector) -{ - return 0; +size_t vector_size(const Vector* vector) { + return vector->size; } -void vector_resize(Vector *vector, size_t size) -{ +void vector_resize(Vector* vector, size_t new_size) { + if (new_size > vector->volume) { + while (vector->volume < new_size) { + vector->volume *= 2; + } + Data* new_data = new Data[vector->volume]; + for (size_t i = 0; i < vector->size; ++i) { + new_data[i] = vector->data[i]; + } + delete[] vector->data; + vector->data = new_data; + } + vector->size = new_size; } diff --git a/LibraryCPP/vector.h b/LibraryCPP/vector.h index b15b97737a..f874924200 100644 --- a/LibraryCPP/vector.h +++ b/LibraryCPP/vector.h @@ -1,8 +1,7 @@ #ifndef VECTOR_H #define VECTOR_H - #include - +#include // Vector (dynamic array) // Stores integer values inside typedef int Data; diff --git "a/\320\240\321\213\320\261\320\272\320\270\320\275\320\260 \320\220\320\275\320\260\321\201\321\202\320\260\321\201\320\270\321\217 \320\220\320\273\320\265\320\272\321\201\320\265\320\265\320\262\320\275\320\260 \320\223\321\200\321\203\320\277\320\277\320\260 3091" "b/\320\240\321\213\320\261\320\272\320\270\320\275\320\260 \320\220\320\275\320\260\321\201\321\202\320\260\321\201\320\270\321\217 \320\220\320\273\320\265\320\272\321\201\320\265\320\265\320\262\320\275\320\260 \320\223\321\200\321\203\320\277\320\277\320\260 3091" new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ "b/\320\240\321\213\320\261\320\272\320\270\320\275\320\260 \320\220\320\275\320\260\321\201\321\202\320\260\321\201\320\270\321\217 \320\220\320\273\320\265\320\272\321\201\320\265\320\265\320\262\320\275\320\260 \320\223\321\200\321\203\320\277\320\277\320\260 3091" @@ -0,0 +1 @@ +