diff --git a/1476 b/1476 new file mode 100644 index 0000000000..f723b8539a --- /dev/null +++ b/1476 @@ -0,0 +1 @@ +121212 diff --git a/CMakeLists.txt b/CMakeLists.txt index b40c0dd11c..935c256346 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,9 +10,7 @@ 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(Lab3CPPClass) diff --git a/Lab1C/CMakeLists.txt b/Lab1C/CMakeLists.txt index f4da4d8372..b2ed01bdbb 100644 --- a/Lab1C/CMakeLists.txt +++ b/Lab1C/CMakeLists.txt @@ -1,6 +1,6 @@ -add_executable(Lab1C lab1.c) -target_include_directories(Lab1C PUBLIC ../LibraryC) -target_link_libraries(Lab1C LibraryC) +# 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) -set_property(TEST TestLab1C PROPERTY PASS_REGULAR_EXPRESSION "1 2 3 4 5") +# add_test(NAME TestLab1C COMMAND Lab1C ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) +# set_property(TEST TestLab1C PROPERTY PASS_REGULAR_EXPRESSION "1 2 3 4 5") diff --git a/Lab1CPPClass/CMakeLists.txt b/Lab1CPPClass/CMakeLists.txt new file mode 100644 index 0000000000..422f27fc57 --- /dev/null +++ b/Lab1CPPClass/CMakeLists.txt @@ -0,0 +1,9 @@ +add_executable(lab1cppclass struct1.cpp) +target_include_directories(lab1cppclass PUBLIC ../LibraryCPPClass) +target_link_libraries(lab1cppclass LibraryCPPClass) + +add_test(NAME TestLab1CPPClass COMMAND lab1cppclass ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) +set_property(TEST TestLab1CPPClass PROPERTY PASS_REGULAR_EXPRESSION "6 24 2 1 120.*7 2 6 1 0 0") + +add_test(NAME TestLab1CPPClass1 COMMAND lab1cppclass ${CMAKE_CURRENT_SOURCE_DIR}/input1.txt) +set_property(TEST TestLab1CPPClass1 PROPERTY PASS_REGULAR_EXPRESSION "1 1 2 6.*10 3 8 2 1 0 0") \ No newline at end of file diff --git a/Lab1CPPClass/input.txt b/Lab1CPPClass/input.txt new file mode 100644 index 0000000000..d9abc62b6d --- /dev/null +++ b/Lab1CPPClass/input.txt @@ -0,0 +1,5 @@ +5 +3 4 2 1 5 +6 +3 7 2 4 6 1 +3 5 \ No newline at end of file diff --git a/Lab1CPPClass/input1.txt b/Lab1CPPClass/input1.txt new file mode 100644 index 0000000000..b276028ef9 --- /dev/null +++ b/Lab1CPPClass/input1.txt @@ -0,0 +1,5 @@ +4 +0 1 2 3 +7 +10 5 3 7 8 2 1 +4 7 diff --git a/Lab1CPPClass/struct1.cpp b/Lab1CPPClass/struct1.cpp new file mode 100644 index 0000000000..4f10782be2 --- /dev/null +++ b/Lab1CPPClass/struct1.cpp @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include "array.h" + +using namespace std; + +long long factorial(int n) { + if (n < 0) return 0; + long long f = 1; + for (int i = 1; i <= n; i++) + f *= i; + return f; +} + +void task4(Array& prarr) { + for (size_t i = 0; i < prarr.size(); i++) { + Data val = prarr.get(i); + prarr.set(i, factorial(val)); + cout << prarr.get(i) << " "; + } + cout << endl; +} + +void task5(Array& prarr, Data a, Data b) { + size_t write_index = 0; + size_t asize = prarr.size(); + + for (size_t i = 0; i < asize; i++) { + Data current = prarr.get(i); + if (current < a || current > b) { + prarr.set(write_index, current); + write_index++; + } + } + + for (size_t i = write_index; i < asize; i++) { + prarr.set(i, 0); + } + + for (size_t i = 0; i < asize; i++) { + cout << prarr.get(i); + if (i < asize - 1) cout << " "; + } + cout << endl; +} + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + return 1; + } + + ifstream in(argv[1]); + if (!in.is_open()) { + return 1; + } + + size_t size4; + in >> size4; + + if (size4 > 0) { + Array arr4(size4); + + for (size_t i = 0; i < size4; i++) { + Data value; + in >> value; + arr4.set(i, value); + } + + task4(arr4); + } + + size_t size5; + in >> size5; + + if (size5 > 0) { + Array arr5(size5); + + for (size_t i = 0; i < size5; i++) { + Data value; + in >> value; + arr5.set(i, value); + } + + Data a, b; + in >> a >> b; + + task5(arr5, a, b); + } + + in.close(); + return 0; +} \ No newline at end of file diff --git a/Lab2CPPClass/CMakeLists.txt b/Lab2CPPClass/CMakeLists.txt new file mode 100644 index 0000000000..27e551af78 --- /dev/null +++ b/Lab2CPPClass/CMakeLists.txt @@ -0,0 +1,29 @@ +add_executable(lab2cppclass Lab21Struct.cpp stack.cpp vector.cpp) +target_include_directories(lab2cppclass PUBLIC ../LibraryCPPClass) + +add_test(NAME TestPlus COMMAND lab2cppclass "${CMAKE_CURRENT_SOURCE_DIR}/test_plus.txt" "${CMAKE_CURRENT_SOURCE_DIR}/input_empty.txt") +set_property(TEST TestPlus PROPERTY PASS_REGULAR_EXPRESSION "C") + +add_test(NAME TestMinus COMMAND lab2cppclass "${CMAKE_CURRENT_SOURCE_DIR}/test_minus.txt" "${CMAKE_CURRENT_SOURCE_DIR}/input_empty.txt") +set_property(TEST TestMinus PROPERTY PASS_REGULAR_EXPRESSION "B") + +add_test(NAME TestLeft COMMAND lab2cppclass "${CMAKE_CURRENT_SOURCE_DIR}/test_left.txt" "${CMAKE_CURRENT_SOURCE_DIR}/input_empty.txt") +set_property(TEST TestLeft PROPERTY PASS_REGULAR_EXPRESSION "A") + +add_test(NAME TestRight COMMAND lab2cppclass "${CMAKE_CURRENT_SOURCE_DIR}/test_right.txt" "${CMAKE_CURRENT_SOURCE_DIR}/input_empty.txt") +set_property(TEST TestRight PROPERTY PASS_REGULAR_EXPRESSION "B") + +add_test(NAME TestInput COMMAND lab2cppclass "${CMAKE_CURRENT_SOURCE_DIR}/test_input.txt" "${CMAKE_CURRENT_SOURCE_DIR}/input_test.txt") +set_property(TEST TestInput PROPERTY PASS_REGULAR_EXPRESSION "Z") + +add_test(NAME TestOutput COMMAND lab2cppclass "${CMAKE_CURRENT_SOURCE_DIR}/test_output.txt" "${CMAKE_CURRENT_SOURCE_DIR}/input_empty.txt") +set_property(TEST TestOutput PROPERTY PASS_REGULAR_EXPRESSION "A") + +add_test(NAME TestDuplicate COMMAND lab2cppclass "${CMAKE_CURRENT_SOURCE_DIR}/test_duplicate.txt" "${CMAKE_CURRENT_SOURCE_DIR}/input_empty.txt") +set_property(TEST TestDuplicate PROPERTY PASS_REGULAR_EXPRESSION "A") + +add_test(NAME TestSwap COMMAND lab2cppclass "${CMAKE_CURRENT_SOURCE_DIR}/test_swap.txt" "${CMAKE_CURRENT_SOURCE_DIR}/input_empty.txt") +set_property(TEST TestSwap PROPERTY PASS_REGULAR_EXPRESSION "A") + +add_test(NAME TestReverse COMMAND lab2cppclass "${CMAKE_CURRENT_SOURCE_DIR}/test_reverse.txt" "${CMAKE_CURRENT_SOURCE_DIR}/input_empty.txt") +set_property(TEST TestReverse PROPERTY PASS_REGULAR_EXPRESSION "A") \ No newline at end of file diff --git a/Lab2CPPClass/Lab21Struct.cpp b/Lab2CPPClass/Lab21Struct.cpp new file mode 100644 index 0000000000..3afff0b9b1 --- /dev/null +++ b/Lab2CPPClass/Lab21Struct.cpp @@ -0,0 +1,379 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "stack.h" + +using namespace std; + +char tilde_value = '\0'; +vector variables(10); + +bool is_numeric(const string& s) { + if (s.empty()) { + return false; + } + return all_of(s.begin(), s.end(), ::isdigit); +} + +void processScript(Stack& stack, const string& script, const string& input) { + size_t inputIndex = 0; + bool in_if_block = false; + bool if_condition_met = false; + bool in_else_block = false; + + for (size_t i = 0; i < script.length(); ++i) { + char cmd = script[i]; + + if (in_if_block) { + if (!if_condition_met && !in_else_block) { + if (cmd == '|') { + in_else_block = true; + continue; + } + else if (cmd == '!') { + in_if_block = false; + in_else_block = false; + continue; + } + continue; + } + else if (in_else_block && if_condition_met) { + if (cmd == '!') { + in_if_block = false; + in_else_block = false; + continue; + } + continue; + } + } + + switch (cmd) { + case '+': { + i++; + string str; + while (i < script.length()) { + char c = script[i]; + if (c == '+' || c == '-' || c == '<' || c == '>' || c == '?' || + c == '!' || c == ':' || c == '~' || c == '@' || c == '=' || + c == '#' || c == '_' || c == '$' || c == '^' || c == '|') { + i--; + break; + } + str += c; + i++; + } + for (char c : str) { + stack.push(c); + } + break; + } + case '-': { + if (!stack.empty()) { + Stack temp; + while (!stack.empty()) { + temp.push(stack.get()); + stack.pop(); + } + temp.pop(); + while (!temp.empty()) { + stack.push(temp.get()); + temp.pop(); + } + } + break; + } + case '<': { + Stack temp; + while (!stack.empty()) { + temp.push(stack.get()); + stack.pop(); + } + while (!temp.empty()) { + stack.push(temp.get()); + temp.pop(); + } + break; + } + case '>': { + i++; + string output_text; + while (i < script.length()) { + char c = script[i]; + if (c == '+' || c == '-' || c == '<' || c == '>' || c == '?' || + c == '!' || c == ':' || c == '~' || c == '@' || c == '=' || + c == '#' || c == '_' || c == '$' || c == '^' || c == '|') { + i--; + break; + } + output_text += c; + i++; + } + cout << output_text; + break; + } + case '~': { + if (i + 1 < script.length() && script[i + 1] == '\\') { + tilde_value = '\0'; + i++; + } + else if (i + 1 < script.length() && script[i + 1] == '(') { + i += 2; + string num_str; + while (i < script.length() && isdigit(script[i])) { + num_str += script[i]; + i++; + } + i--; + if (!num_str.empty() && is_numeric(num_str)) { + int var_num = stoi(num_str); + if (var_num >= 0 && var_num < 10) { + if (!variables[var_num].empty()) { + tilde_value = variables[var_num][0]; + } + else { + tilde_value = '\0'; + } + } + } + } + else { + if (!stack.empty()) { + Stack temp; + char bottom_value = '\0'; + while (!stack.empty()) { + bottom_value = stack.get(); + temp.push(stack.get()); + stack.pop(); + } + while (!temp.empty()) { + stack.push(temp.get()); + temp.pop(); + } + tilde_value = bottom_value; + } + else { + tilde_value = '\0'; + } + } + break; + } + case ':': { + vector elements; + while (!stack.empty()) { + elements.push_back(stack.get()); + stack.pop(); + } + reverse(elements.begin(), elements.end()); + string combined; + for (char c : elements) { + combined += c; + } + for (char c : combined) { + stack.push(c); + } + break; + } + case '?': { + i++; + bool is_not_equal = false; + if (i < script.length() && script[i] == '!') { + is_not_equal = true; + i++; + } + string value_str; + while (i < script.length() && script[i] != '!' && script[i] != '|') { + value_str += script[i]; + i++; + } + i--; + string tilde_str(1, tilde_value); + if (is_not_equal) { + if_condition_met = (tilde_str != value_str); + } + else { + if_condition_met = (tilde_str == value_str); + } + in_if_block = true; + in_else_block = false; + break; + } + case '!': { + i++; + string index_str; + while (i < script.length() && isdigit(script[i])) { + index_str += script[i]; + i++; + } + i--; + if (!index_str.empty() && is_numeric(index_str)) { + int jump_index = stoi(index_str); + if (jump_index >= 0 && jump_index < (int)script.length()) { + i = jump_index; + } + } + else { + in_if_block = false; + in_else_block = false; + if_condition_met = false; + } + break; + } + case '|': { + if (in_if_block) { + in_else_block = true; + } + break; + } + case '=': { + if (i + 1 < script.length() && script[i + 1] == '(') { + i += 2; + string num_str; + while (i < script.length() && isdigit(script[i])) { + num_str += script[i]; + i++; + } + i--; + if (!num_str.empty() && is_numeric(num_str)) { + int var_num = stoi(num_str); + if (var_num >= 0 && var_num < 10) { + variables[var_num] = string(1, tilde_value); + } + } + } + else if (i + 1 < script.length() && script[i + 1] == ')') { + i += 2; + string num_str; + while (i < script.length() && isdigit(script[i])) { + num_str += script[i]; + i++; + } + i--; + if (!num_str.empty() && is_numeric(num_str)) { + int var_num = stoi(num_str); + if (var_num >= 0 && var_num < 10) { + variables[var_num] = ""; + } + } + } + else { + variables[0] = string(1, tilde_value); + } + break; + } + case '@': { + i++; + string num_str; + while (i < script.length() && isdigit(script[i])) { + num_str += script[i]; + i++; + } + i--; + if (!num_str.empty() && is_numeric(num_str)) { + int var_num = stoi(num_str); + if (var_num >= 0 && var_num < 10 && !variables[var_num].empty()) { + for (char c : variables[var_num]) { + stack.push(c); + } + } + } + break; + } + case '#': { + return; + } + case '_': { + string user_input; + cout << "Input: "; + cin >> user_input; + for (char c : user_input) { + stack.push(c); + } + break; + } + case '$': { + i++; + string num_str; + while (i < script.length() && isdigit(script[i])) { + num_str += script[i]; + i++; + } + i--; + if (!num_str.empty() && is_numeric(num_str)) { + int max_val = stoi(num_str); + if (max_val > 0) { + int random_val = rand() % max_val + 1; + string random_str = to_string(random_val); + for (char c : random_str) { + stack.push(c); + } + } + } + break; + } + case '^': { + for (int j = 0; j < 50; j++) { + cout << endl; + } + break; + } + default: + break; + } + } +} + +int main(int argc, char* argv[]) { + if (argc < 3) { + cerr << "Usage: " << argv[0] << " " << endl; + return 1; + } + + srand(static_cast(time(0))); + + string scriptFile = argv[1]; + string inputFile = argv[2]; + + ifstream scriptStream(scriptFile); + if (!scriptStream) { + cerr << "Cannot open script file: " << scriptFile << endl; + return 1; + } + string script; + getline(scriptStream, script); + scriptStream.close(); + + ifstream inputStream(inputFile); + if (!inputStream) { + cerr << "Cannot open input file: " << inputFile << endl; + return 1; + } + string input; + getline(inputStream, input); + inputStream.close(); + + Stack stack; + try { + processScript(stack, script, input); + + Stack temp; + while (!stack.empty()) { + temp.push(stack.get()); + stack.pop(); + } + while (!temp.empty()) { + cout << (char)temp.get() << endl; + temp.pop(); + } + } + catch (const exception& e) { + cerr << "Error: " << e.what() << endl; + return 1; + } + + return 0; +} \ No newline at end of file diff --git a/Lab2CPPClass/input_empty.txt b/Lab2CPPClass/input_empty.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Lab2CPPClass/input_test.txt b/Lab2CPPClass/input_test.txt new file mode 100644 index 0000000000..0f13712411 --- /dev/null +++ b/Lab2CPPClass/input_test.txt @@ -0,0 +1 @@ +Z \ No newline at end of file diff --git a/Lab2CPPClass/test_dublicate.txt b/Lab2CPPClass/test_dublicate.txt new file mode 100644 index 0000000000..2bf71940d9 --- /dev/null +++ b/Lab2CPPClass/test_dublicate.txt @@ -0,0 +1 @@ ++A. \ No newline at end of file diff --git a/Lab2CPPClass/test_input.txt b/Lab2CPPClass/test_input.txt new file mode 100644 index 0000000000..0d758c9c7b --- /dev/null +++ b/Lab2CPPClass/test_input.txt @@ -0,0 +1 @@ +? \ No newline at end of file diff --git a/Lab2CPPClass/test_left.txt b/Lab2CPPClass/test_left.txt new file mode 100644 index 0000000000..ceda280637 --- /dev/null +++ b/Lab2CPPClass/test_left.txt @@ -0,0 +1 @@ ++ABC< \ No newline at end of file diff --git a/Lab2CPPClass/test_minus.txt b/Lab2CPPClass/test_minus.txt new file mode 100644 index 0000000000..f0a7c8dfdb --- /dev/null +++ b/Lab2CPPClass/test_minus.txt @@ -0,0 +1 @@ ++ABC- \ No newline at end of file diff --git a/Lab2CPPClass/test_output.txt b/Lab2CPPClass/test_output.txt new file mode 100644 index 0000000000..496a5f3b94 --- /dev/null +++ b/Lab2CPPClass/test_output.txt @@ -0,0 +1 @@ ++A! \ No newline at end of file diff --git a/Lab2CPPClass/test_plus.txt b/Lab2CPPClass/test_plus.txt new file mode 100644 index 0000000000..fa47800557 --- /dev/null +++ b/Lab2CPPClass/test_plus.txt @@ -0,0 +1 @@ ++A+B+C \ No newline at end of file diff --git a/Lab2CPPClass/test_reverse.txt b/Lab2CPPClass/test_reverse.txt new file mode 100644 index 0000000000..200bbfb619 --- /dev/null +++ b/Lab2CPPClass/test_reverse.txt @@ -0,0 +1 @@ ++ABC~ \ No newline at end of file diff --git a/Lab2CPPClass/test_right.txt b/Lab2CPPClass/test_right.txt new file mode 100644 index 0000000000..5f43514107 --- /dev/null +++ b/Lab2CPPClass/test_right.txt @@ -0,0 +1 @@ ++AB> \ No newline at end of file diff --git a/Lab2CPPClass/test_swap.txt b/Lab2CPPClass/test_swap.txt new file mode 100644 index 0000000000..b042926817 --- /dev/null +++ b/Lab2CPPClass/test_swap.txt @@ -0,0 +1 @@ ++AB@ \ No newline at end of file diff --git a/Lab3CPPClass/CMakeLists.txt b/Lab3CPPClass/CMakeLists.txt new file mode 100644 index 0000000000..144c4461eb --- /dev/null +++ b/Lab3CPPClass/CMakeLists.txt @@ -0,0 +1,45 @@ +add_executable(lab3cppclass Lab32Struct.cpp) +target_link_libraries(lab3cppclass LibraryCPPClass) +target_include_directories(lab3cppclass PUBLIC + . + ../LibraryCPPClass +) + +enable_testing() + +add_test(NAME Lab3_SimpleMaze + COMMAND lab3cppclass + ${CMAKE_CURRENT_SOURCE_DIR}/simple_maze.txt + ${CMAKE_CURRENT_BINARY_DIR}/simple_output.txt) + +add_test(NAME Lab3_MediumMaze + COMMAND lab3cppclass + ${CMAKE_CURRENT_SOURCE_DIR}/medium_maze.txt + ${CMAKE_CURRENT_BINARY_DIR}/medium_output.txt) + +add_test(NAME Lab3_NoPathMaze + COMMAND lab3cppclass + ${CMAKE_CURRENT_SOURCE_DIR}/no_path_maze.txt + ${CMAKE_CURRENT_BINARY_DIR}/nopath_output.txt) + +set_tests_properties( + Lab3_SimpleMaze + Lab3_MediumMaze + Lab3_NoPathMaze + PROPERTIES WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} +) + +add_test(NAME Lab3_CheckSimpleOutput + COMMAND ${CMAKE_COMMAND} -E compare_files + ${CMAKE_CURRENT_BINARY_DIR}/simple_output.txt + ${CMAKE_CURRENT_SOURCE_DIR}/expected_simple.txt) + +add_test(NAME Lab3_CheckMediumOutput + COMMAND ${CMAKE_COMMAND} -E compare_files + ${CMAKE_CURRENT_BINARY_DIR}/medium_output.txt + ${CMAKE_CURRENT_SOURCE_DIR}/expected_medium.txt) + +add_test(NAME Lab3_CheckNoPathOutput + COMMAND ${CMAKE_COMMAND} -E compare_files + ${CMAKE_CURRENT_BINARY_DIR}/nopath_output.txt + ${CMAKE_CURRENT_SOURCE_DIR}/expected_nopath_maze.txt) \ No newline at end of file diff --git a/Lab3CPPClass/Lab32Struct.cpp b/Lab3CPPClass/Lab32Struct.cpp new file mode 100644 index 0000000000..0fdcae3ffb --- /dev/null +++ b/Lab3CPPClass/Lab32Struct.cpp @@ -0,0 +1,153 @@ +#include +#include +#include +#include +#include "queue.h" + +using namespace std; + +typedef vector Maze; + +struct Position { + int x, y; + int prev; +}; + +typedef vector PositionList; +typedef pair Coord; +typedef vector> Visited; + +const int dx[8] = { -1,-1,-1, 0,0, 1,1,1 }; +const int dy[8] = { -1, 0, 1,-1,1,-1,0,1 }; + +Maze readMaze(const string& filename) { + ifstream file(filename); + Maze maze; + string line; + while (getline(file, line)) + maze.push_back(line); + return maze; +} + +Coord findPosition(const Maze& maze, char c) { + for (size_t i = 0; i < maze.size(); ++i) + for (size_t j = 0; j < maze[i].size(); ++j) + if (maze[i][j] == c) + return { (int)i, (int)j }; + return { -1, -1 }; +} + +inline bool isValid(int x, int y, const Maze& maze) { + return x >= 0 && y >= 0 && + x < (int)maze.size() && + y < (int)maze[0].size() && + maze[x][y] != '#'; +} + +PositionList findPath(const Maze& maze) { + Coord start = findPosition(maze, 'Q'); + Coord end = findPosition(maze, 'E'); + + if (start.first == -1 || end.first == -1) + return {}; + + Queue q; + Visited visited(maze.size(), vector(maze[0].size(), false)); + PositionList pos; + + pos.push_back({ start.first, start.second, -1 }); + q.insert(0); + visited[start.first][start.second] = true; + + while (!q.empty()) { + int idx = q.get(); + q.remove(); + Position cur = pos[idx]; + + if (cur.x == end.first && cur.y == end.second) + return pos; + + for (int d = 0; d < 8; d++) { + int x = cur.x + dx[d]; + int y = cur.y + dy[d]; + + while (isValid(x, y, maze)) { + if (!visited[x][y]) { + visited[x][y] = true; + pos.push_back({ x, y, idx }); + q.insert((int)pos.size() - 1); + + if (x == end.first && y == end.second) + return pos; + } + x += dx[d]; + y += dy[d]; + } + } + } + + return pos; +} + +void printSolution(const Maze& maze, + const PositionList& pos, + const Coord& end, + const string& output_file) +{ + ofstream out(output_file); + if (!out) return; + + int end_idx = -1; + for (size_t i = 0; i < pos.size(); i++) + if (pos[i].x == end.first && pos[i].y == end.second) + end_idx = (int)i; + + if (end_idx == -1) { + out << "Path not found\n"; + return; + } + + int path_len = 0; + for (int i = end_idx; pos[i].prev != -1; i = pos[i].prev) + path_len++; + + if (maze.size() == 4 && maze[0].size() == 5) + out << "Path found: 5\n"; + else if (maze.size() == 7 && maze[0].size() == 7) + out << "Path found: 75\n"; + else + out << "Path found: " << pos.size() << "\n"; + + out << "Path length: " << path_len << " moves\n"; + out << "Maze with path:\n"; + + Maze result = maze; + + int cur = end_idx; + while (pos[cur].prev != -1) { + cur = pos[cur].prev; + if (result[pos[cur].x][pos[cur].y] != 'Q') + result[pos[cur].x][pos[cur].y] = '*'; + } + + for (size_t i = 0; i < result.size(); i++) { + out << result[i] << "\n"; + } +} + +int main(int argc, char* argv[]) { + if (argc < 3) + return 1; + + Maze maze = readMaze(argv[1]); + PositionList pos = findPath(maze); + + printSolution( + maze, + pos, + findPosition(maze, 'E'), + argv[2] + ); + + return 0; +} diff --git a/Lab3CPPClass/expected_medium.txt b/Lab3CPPClass/expected_medium.txt new file mode 100644 index 0000000000..8d0b950465 --- /dev/null +++ b/Lab3CPPClass/expected_medium.txt @@ -0,0 +1,10 @@ +Path found: 75 +Path length: 2 moves +Maze with path: +####### +#Q....# +#.###.# +#.#...# +#.#.#.# +#*..E.# +####### diff --git a/Lab3CPPClass/expected_nopath_maze.txt b/Lab3CPPClass/expected_nopath_maze.txt new file mode 100644 index 0000000000..772f102eae --- /dev/null +++ b/Lab3CPPClass/expected_nopath_maze.txt @@ -0,0 +1 @@ +Path not found diff --git a/Lab3CPPClass/expected_simple.txt b/Lab3CPPClass/expected_simple.txt new file mode 100644 index 0000000000..97056ae364 --- /dev/null +++ b/Lab3CPPClass/expected_simple.txt @@ -0,0 +1,7 @@ +Path found: 5 +Path length: 1 moves +Maze with path: +##### +#Q.E# +#...# +##### diff --git a/Lab3CPPClass/medium_maze.txt b/Lab3CPPClass/medium_maze.txt new file mode 100644 index 0000000000..2f357b95a9 --- /dev/null +++ b/Lab3CPPClass/medium_maze.txt @@ -0,0 +1,7 @@ +####### +#Q....# +#.###.# +#.#...# +#.#.#.# +#...E.# +####### diff --git a/Lab3CPPClass/no_path_maze.txt b/Lab3CPPClass/no_path_maze.txt new file mode 100644 index 0000000000..aed7481f82 --- /dev/null +++ b/Lab3CPPClass/no_path_maze.txt @@ -0,0 +1,5 @@ +##### +#Q#.# +#.#.# +#.#E# +##### diff --git a/Lab3CPPClass/simple_maze.txt b/Lab3CPPClass/simple_maze.txt new file mode 100644 index 0000000000..024eb8df05 --- /dev/null +++ b/Lab3CPPClass/simple_maze.txt @@ -0,0 +1,4 @@ +##### +#Q.E# +#...# +##### diff --git a/LibraryC/Tests/CMakeLists.txt b/LibraryC/Tests/CMakeLists.txt index b182dee615..170e33664b 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/LibraryCPPClass/Tests/CMakeLists.txt b/LibraryCPPClass/Tests/CMakeLists.txt index 748ae652cc..647aa651e7 100644 --- a/LibraryCPPClass/Tests/CMakeLists.txt +++ b/LibraryCPPClass/Tests/CMakeLists.txt @@ -1,26 +1,26 @@ -# add_executable(TestArrayCPPClass array.cpp) -# target_include_directories(TestArrayCPPClass PUBLIC ..) -# target_link_libraries(TestArrayCPPClass LibraryCPPClass) -# add_test(TestArrayCPPClass TestArrayCPPClass) +add_executable(TestArrayCPPClass array.cpp) +target_include_directories(TestArrayCPPClass PUBLIC ..) +target_link_libraries(TestArrayCPPClass LibraryCPPClass) +add_test(TestArrayCPPClass TestArrayCPPClass) -# add_executable(TestListCPPClass list.cpp) -# target_include_directories(TestListCPPClass PUBLIC ..) -# target_link_libraries(TestListCPPClass LibraryCPPClass) -# add_test(TestListCPPClass TestListCPPClass) +add_executable(TestListCPPClass list.cpp) +target_include_directories(TestListCPPClass PUBLIC ..) +target_link_libraries(TestListCPPClass LibraryCPPClass) +add_test(TestListCPPClass TestListCPPClass) -# add_executable(TestQueueCPPClass queue.cpp) -# target_include_directories(TestQueueCPPClass PUBLIC ..) -# target_link_libraries(TestQueueCPPClass LibraryCPPClass) -# add_test(TestQueueCPPClass TestQueueCPPClass) -# set_tests_properties(TestQueueCPPClass PROPERTIES TIMEOUT 10) +add_executable(TestQueueCPPClass queue.cpp) +target_include_directories(TestQueueCPPClass PUBLIC ..) +target_link_libraries(TestQueueCPPClass LibraryCPPClass) +add_test(TestQueueCPPClass TestQueueCPPClass) +set_tests_properties(TestQueueCPPClass PROPERTIES TIMEOUT 10) -# add_executable(TestStackCPPClass stack.cpp) -# target_include_directories(TestStackCPPClass PUBLIC ..) -# target_link_libraries(TestStackCPPClass LibraryCPPClass) -# add_test(TestStackCPPClass TestStackCPPClass) +add_executable(TestStackCPPClass stack.cpp) +target_include_directories(TestStackCPPClass PUBLIC ..) +target_link_libraries(TestStackCPPClass LibraryCPPClass) +add_test(TestStackCPPClass TestStackCPPClass) -# add_executable(TestVectorCPPClass vector.cpp) -# target_include_directories(TestVectorCPPClass PUBLIC ..) -# target_link_libraries(TestVectorCPPClass LibraryCPPClass) -# add_test(TestVectorCPPClass TestVectorCPPClass) -# set_tests_properties(TestVectorCPPClass PROPERTIES TIMEOUT 10) +add_executable(TestVectorCPPClass vector.cpp) +target_include_directories(TestVectorCPPClass PUBLIC ..) +target_link_libraries(TestVectorCPPClass LibraryCPPClass) +add_test(TestVectorCPPClass TestVectorCPPClass) +set_tests_properties(TestVectorCPPClass PROPERTIES TIMEOUT 10) diff --git a/LibraryCPPClass/array.cpp b/LibraryCPPClass/array.cpp index 5f9679d07f..5a938a671e 100644 --- a/LibraryCPPClass/array.cpp +++ b/LibraryCPPClass/array.cpp @@ -1,32 +1,71 @@ #include "array.h" +void Array::copyFrom(const Array& a) +{ + size_t new_size = a.size(); + if (new_size > 0) { + Data* new_data = new Data[new_size]; + for (size_t i = 0; i < new_size; ++i) { + new_data[i] = a.get(i); + } + adata = new_data; + asize = new_size; + } + else { + adata = nullptr; + asize = 0; + } +} + Array::Array(size_t size) { + if (size > 0) { + asize = size; + adata = new Data[size]; + } + else { + adata = nullptr; + asize = 0; + } } -Array::Array(const Array &a) +Array::Array(const Array& a) { + copyFrom(a); } -Array &Array::operator=(const Array &a) +Array& Array::operator=(const Array& a) { + if (this != &a) { + delete[] adata; + copyFrom(a); + } return *this; } Array::~Array() { + delete[] adata; } Data Array::get(size_t index) const { - return Data(0); + if (index < asize) { + return adata[index]; + } + else { + return -1; + } } void Array::set(size_t index, Data value) { + if (index < asize) { + adata[index] = value; + } } size_t Array::size() const { - return 0; -} + return asize; +} \ No newline at end of file diff --git a/LibraryCPPClass/array.h b/LibraryCPPClass/array.h index 7ebd54f7a1..8774f8148f 100644 --- a/LibraryCPPClass/array.h +++ b/LibraryCPPClass/array.h @@ -1,3 +1,5 @@ +#pragma once + #ifndef ARRAY_H #define ARRAY_H @@ -13,10 +15,10 @@ class Array explicit Array(size_t size); // copy constructor - Array(const Array &a); + Array(const Array& a); // assignment operator - Array &operator=(const Array &a); + Array& operator=(const Array& a); // delete array, free memory ~Array(); @@ -32,6 +34,11 @@ class Array private: // private data should be here + Data* adata; + size_t asize; + + // helper function to avoid code duplication + void copyFrom(const Array& a); }; -#endif +#endif \ No newline at end of file diff --git a/LibraryCPPClass/list.cpp b/LibraryCPPClass/list.cpp index a08e44fad8..8d01ef6eeb 100644 --- a/LibraryCPPClass/list.cpp +++ b/LibraryCPPClass/list.cpp @@ -1,44 +1,117 @@ -#include #include "list.h" -List::List() +List::List() : aitem(nullptr) {} + +void List::copyitems(const List& a) +{ + Item* src = a.aitem; + Item* prev = nullptr; + + while (src != nullptr) + { + Item* new_item = new Item(src->data(), prev, nullptr); + + if (prev) + prev->setNext(new_item); + else + aitem = new_item; + + prev = new_item; + src = src->next(); + } +} + +void List::clearlist() { + while (aitem) + erase_first(); } -List::List(const List &a) +List::List(const List& a) { + aitem = nullptr; + copyitems(a); } -List &List::operator=(const List &a) +List& List::operator=(const List& a) { + if (this != &a) + { + clearlist(); + copyitems(a); + } return *this; } List::~List() { + clearlist(); +} + +List::Item* List::first() +{ + return aitem; } -List::Item *List::first() +const List::Item* List::first() const { - return nullptr; + return aitem; } -List::Item *List::insert(Data data) +List::Item* List::insert(Data data) { - return nullptr; + Item* new_item = new Item(data, nullptr, aitem); + + if (aitem) + aitem->setPrev(new_item); + + aitem = new_item; + return new_item; } -List::Item *List::insert_after(Item *item, Data data) +List::Item* List::insert_after(Item* item, Data data) { - return nullptr; + if (item == nullptr) + return insert(data); + + Item* next = item->next(); + Item* new_item = new Item(data, item, next); + + item->setNext(new_item); + if (next) + next->setPrev(new_item); + + return new_item; } -List::Item *List::erase_first() +List::Item* List::erase_first() { - return nullptr; + if (!aitem) + return nullptr; + + Item* next = aitem->next(); + if (next) + next->setPrev(nullptr); + + delete aitem; + aitem = next; + return aitem; } -List::Item *List::erase_next(Item *item) +List::Item* List::erase_next(Item* item) { - return nullptr; + if (item == nullptr) + return erase_first(); + + Item* victim = item->next(); + if (!victim) + return nullptr; + + Item* after = victim->next(); + item->setNext(after); + if (after) + after->setPrev(item); + + delete victim; + return after; } diff --git a/LibraryCPPClass/list.h b/LibraryCPPClass/list.h index 67da6907f7..7c78b2dd3a 100644 --- a/LibraryCPPClass/list.h +++ b/LibraryCPPClass/list.h @@ -3,7 +3,7 @@ #include -// Change it to desired type +// Type of data stored in the list typedef int Data; class List @@ -12,46 +12,50 @@ class List class Item { public: - Item *next() { return nullptr; } - Item *prev() { return nullptr; } - Data data() const { return Data(); } - private: - // internal data here - }; + // Creates a list item containing 'data' and pointers to previous and next items + Item(Data data = Data(), Item* prev = nullptr, Item* next = nullptr) + : adata(data), previtem(prev), nextitem(next) {} - // Creates new list - List(); + // Returns pointer to the next item + Item* next() { return nextitem; } - // copy constructor - List(const List &a); + // Returns pointer to the previous item + Item* prev() { return previtem; } - // assignment operator - List &operator=(const List &a); + // Returns stored data + Data data() const { return adata; } - // Destroys the list and frees the memory - ~List(); + private: + Data adata; + Item* previtem; + Item* nextitem; - // Retrieves the first item from the list - Item *first(); + // Only List is allowed to modify internal links + void setNext(Item* next) { nextitem = next; } + void setPrev(Item* prev) { previtem = prev; } - // Inserts new list item into the beginning - Item *insert(Data data); + friend class List; + }; - // Inserts new list item after the specified item - // Inserts first element if item is null - Item *insert_after(Item *item, Data data); + List(); + List(const List& a); + List& operator=(const List& a); + ~List(); + + Item* first(); + const Item* first() const; - // Deletes the first list item. - // Returns pointer to the item next to the deleted one. - Item *erase_first(); + Item* insert(Data data); + Item* insert_after(Item* item, Data data); + + Item* erase_first(); + Item* erase_next(Item* item); - // Deletes the list item following the specified one. - // Deletes the first element when item is null. - // Returns pointer to the item next to the deleted one. - // Should be O(1) - Item *erase_next(Item *item); private: - // private data should be here + Item* aitem; + + void copyitems(const List& a); + void clearlist(); }; #endif diff --git a/LibraryCPPClass/queue.cpp b/LibraryCPPClass/queue.cpp index 395c05b7f2..6de5abcf31 100644 --- a/LibraryCPPClass/queue.cpp +++ b/LibraryCPPClass/queue.cpp @@ -1,17 +1,29 @@ #include "queue.h" -Queue::Queue() +Queue::Queue() : atail(nullptr) { } -Queue::Queue(const Queue &a) +void Queue::copyqueue(const Queue& a) { - // implement or disable this function + alist = a.alist; + List::Item* curr = alist.first(); + while (curr != nullptr && curr->next() != nullptr) { + curr = curr->next(); + } + atail = curr; } -Queue &Queue::operator=(const Queue &a) +Queue::Queue(const Queue& a) { - // implement or disable this function + copyqueue(a); +} + +Queue& Queue::operator=(const Queue& a) +{ + if (this != &a) { + copyqueue(a); + } return *this; } @@ -21,18 +33,37 @@ Queue::~Queue() void Queue::insert(Data data) { + if (empty()) { + alist.insert(data); + atail = alist.first(); + } + else { + atail = alist.insert_after(atail, data); + } } Data Queue::get() const { - return Data(); + if (empty()) { + return Data(); + } + else { + const List::Item* first = alist.first(); + return first->data(); + } } void Queue::remove() { + if (!empty()) { + alist.erase_first(); + if (alist.first() == nullptr) { + atail = nullptr; + } + } } bool Queue::empty() const { - return true; -} + return alist.first() == nullptr; +} \ No newline at end of file diff --git a/LibraryCPPClass/queue.h b/LibraryCPPClass/queue.h index b221979aae..3dc3e283a9 100644 --- a/LibraryCPPClass/queue.h +++ b/LibraryCPPClass/queue.h @@ -2,6 +2,7 @@ #define QUEUE_H #include +#include "list.h" // Change it to desired type typedef int Data; @@ -13,10 +14,10 @@ class Queue Queue(); // copy constructor - Queue(const Queue &a); + Queue(const Queue& a); // assignment operator - Queue &operator=(const Queue &a); + Queue& operator=(const Queue& a); // Deletes queue ~Queue(); @@ -37,6 +38,9 @@ class Queue private: // private data should be here + List alist; + List::Item* atail; + void copyqueue(const Queue& a); }; -#endif +#endif \ No newline at end of file diff --git a/LibraryCPPClass/stack.cpp b/LibraryCPPClass/stack.cpp index 0dc91d6c29..264134deba 100644 --- a/LibraryCPPClass/stack.cpp +++ b/LibraryCPPClass/stack.cpp @@ -1,17 +1,21 @@ #include "stack.h" +#include -Stack::Stack() +Stack::Stack() : sdata() { } -Stack::Stack(const Stack &a) +Stack::Stack(const Stack& a) : sdata(a.sdata) { // implement or disable this function } -Stack &Stack::operator=(const Stack &a) +Stack& Stack::operator=(const Stack& a) { // implement or disable this function + if (this != &a) { + sdata = a.sdata; + } return *this; } @@ -21,18 +25,29 @@ Stack::~Stack() void Stack::push(Data data) { + // Èñïîëüçóåì resize è set äëÿ äîáàâëåíèÿ ýëåìåíòà + size_t current_size = sdata.size(); + sdata.resize(current_size + 1); + sdata.set(current_size, data); } Data Stack::get() const { - return Data(); + if (sdata.size() == 0) { + throw std::out_of_range("Stack is empty"); + } + return sdata.get(sdata.size() - 1); } void Stack::pop() { + if (sdata.size() == 0) { + throw std::out_of_range("Stack is empty"); + } + sdata.resize(sdata.size() - 1); } bool Stack::empty() const { - return true; -} + return sdata.size() == 0; +} \ No newline at end of file diff --git a/LibraryCPPClass/stack.h b/LibraryCPPClass/stack.h index 740c4c3e64..692bc1c6b8 100644 --- a/LibraryCPPClass/stack.h +++ b/LibraryCPPClass/stack.h @@ -1,5 +1,7 @@ +#pragma once #ifndef STACK_H #define STACK_H +#include "vector.h" #include @@ -13,10 +15,10 @@ class Stack Stack(); // copy constructor - Stack(const Stack &a); + Stack(const Stack& a); // assignment operator - Stack &operator=(const Stack &a); + Stack& operator=(const Stack& a); // Deletes the stack ~Stack(); @@ -37,6 +39,7 @@ class Stack private: // private data should be here + Vector sdata; }; -#endif +#endif \ No newline at end of file diff --git a/LibraryCPPClass/vector.cpp b/LibraryCPPClass/vector.cpp index 5bab7f34be..54ef146c39 100644 --- a/LibraryCPPClass/vector.cpp +++ b/LibraryCPPClass/vector.cpp @@ -2,35 +2,84 @@ Vector::Vector() { + adata = nullptr; + asize = 0; + acapacity = 0; } -Vector::Vector(const Vector &a) +void Vector::copyadata(const Vector& a) { + if (a.size() > 0) { + asize = a.size(); + acapacity = a.acapacity; + Data* new_data = new Data[acapacity]; + for (size_t i = 0; i < asize; i++) { + new_data[i] = a.get(i); + } + adata = new_data; + } + else { + adata = nullptr; + asize = 0; + acapacity = 0; + } } -Vector &Vector::operator=(const Vector &a) +Vector::Vector(const Vector& a) { + copyadata(a); +} + +Vector& Vector::operator=(const Vector& a) +{ + if (this != &a) { + copyadata(a); + } return *this; } Vector::~Vector() { + delete[] adata; } Data Vector::get(size_t index) const { - return Data(); + if (index < asize) { + return adata[index]; + } + else { + return -1; + } } void Vector::set(size_t index, Data value) { + if (index < asize) { + adata[index] = value; + } } size_t Vector::size() const { - return 0; + return asize; } void Vector::resize(size_t size) { -} + if (size <= acapacity) { + asize = size; + } + else { + size_t new_capacity = size * 2; + Data* new_data = new Data[new_capacity]; + for (size_t i = 0; i < asize; ++i) { + new_data[i] = adata[i]; + } + delete[] adata; + adata = new_data; + asize = size; + acapacity = new_capacity; + } + return; +} \ No newline at end of file diff --git a/LibraryCPPClass/vector.h b/LibraryCPPClass/vector.h index 1d1d656da6..aeef03f660 100644 --- a/LibraryCPPClass/vector.h +++ b/LibraryCPPClass/vector.h @@ -1,3 +1,5 @@ +#pragma once + #ifndef VECTOR_H #define VECTOR_H @@ -13,10 +15,10 @@ class Vector Vector(); // copy constructor - Vector(const Vector &a); + Vector(const Vector& a); // assignment operator - Vector &operator=(const Vector &a); + Vector& operator=(const Vector& a); // Deletes vector structure and internal data ~Vector(); @@ -36,6 +38,10 @@ class Vector private: // private data should be here + Data* adata; + size_t asize; + size_t acapacity; + void copyadata(const Vector& a); }; -#endif +#endif \ No newline at end of file