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

lab 4 #1130

Open
wants to merge 65 commits into
base: master
Choose a base branch
from
Open

lab 4 #1130

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
9d11189
Рыбкина Анастасия Алексеевна Группа 3091
ms-anastasiia Sep 9, 2024
784dc53
Merge pull request #1 from ms-anastasiia/ms-anastasiia-patch-1
ms-anastasiia Sep 9, 2024
5d39b76
lab1CPP
ms-anastasiia Sep 11, 2024
8defa41
lab1--build
ms-anastasiia Sep 12, 2024
96360b4
\\
ms-anastasiia Sep 13, 2024
31ce04c
update
ms-anastasiia Sep 13, 2024
2a2414e
update
ms-anastasiia Sep 16, 2024
567c9a5
stack and vector
ms-anastasiia Sep 17, 2024
d003d89
stack and vector update
ms-anastasiia Sep 18, 2024
e1acadd
lab2 update
ms-anastasiia Sep 18, 2024
01f79ac
lab2 update
ms-anastasiia Sep 19, 2024
569ea9f
lab2 update
ms-anastasiia Sep 20, 2024
2bf7d1a
lab2 update
ms-anastasiia Sep 23, 2024
f498273
lab2 update
ms-anastasiia Sep 24, 2024
e5a35df
lab2 update
ms-anastasiia Sep 25, 2024
8960175
lab2 update
ms-anastasiia Sep 26, 2024
48fd759
lab2 update
ms-anastasiia Sep 27, 2024
c09a4b4
lab2 update
ms-anastasiia Oct 1, 2024
ab6765b
lab2 update
ms-anastasiia Oct 1, 2024
88ef3b0
lab2 update
ms-anastasiia Oct 1, 2024
06b409e
lab2 update
ms-anastasiia Oct 1, 2024
d0d6df5
lab2 update
ms-anastasiia Oct 2, 2024
bb18f02
lab2 update
ms-anastasiia Oct 2, 2024
5674941
lab2 update
ms-anastasiia Oct 3, 2024
e3fd2ff
lab2 update
ms-anastasiia Oct 6, 2024
703784b
lab2 update
ms-anastasiia Oct 7, 2024
a108d2f
lab2 update
ms-anastasiia Oct 7, 2024
dc0c5b0
lab2 update
ms-anastasiia Oct 9, 2024
1da6b75
lab2 update
ms-anastasiia Oct 10, 2024
e6ef871
lab2 update
ms-anastasiia Oct 11, 2024
756b908
lab3
ms-anastasiia Oct 23, 2024
dbc9f5f
lab3 update
ms-anastasiia Oct 23, 2024
90bd622
lab3 update
ms-anastasiia Oct 23, 2024
4a01161
lab3 update
ms-anastasiia Oct 25, 2024
74a429a
lab3 update
ms-anastasiia Oct 25, 2024
c3a22f6
lab3 update
ms-anastasiia Oct 28, 2024
cc4a71e
lab3 update
ms-anastasiia Oct 28, 2024
35edd69
lab3 update
ms-anastasiia Oct 28, 2024
223b226
lab3 update
ms-anastasiia Oct 28, 2024
897c92a
lab3 update
ms-anastasiia Oct 29, 2024
6132749
lab3 update
ms-anastasiia Oct 29, 2024
aaa6afd
lab4
ms-anastasiia Nov 25, 2024
3ec672f
Update task.txt
ms-anastasiia Nov 25, 2024
fc345ec
lab 4 update
ms-anastasiia Nov 25, 2024
68c7d6f
lab 4 update
ms-anastasiia Nov 25, 2024
b0214fa
Merge branch 'master' of https://github.com/ms-anastasiia/Algorithms
ms-anastasiia Nov 25, 2024
ffa8228
lab 4 update
ms-anastasiia Nov 25, 2024
a4e64cb
lab 4 update
ms-anastasiia Nov 25, 2024
228b320
lab 4 update
ms-anastasiia Nov 25, 2024
bc4fdf6
lab 4 update
ms-anastasiia Nov 25, 2024
3d23344
lab 4 update
ms-anastasiia Nov 25, 2024
afeda70
lab 4 update
ms-anastasiia Nov 25, 2024
ec89c5e
lab 4 update
ms-anastasiia Nov 25, 2024
edf9a44
lab 4 update
ms-anastasiia Nov 25, 2024
e3e0ca8
lab4 update
ms-anastasiia Nov 25, 2024
fa3f14d
lab 4 update
ms-anastasiia Nov 25, 2024
47abe53
lab 4 update
ms-anastasiia Nov 25, 2024
5705d14
lab 4 update
ms-anastasiia Nov 25, 2024
e75c290
lab 4 update
ms-anastasiia Nov 25, 2024
fe30abf
lab 4 update
ms-anastasiia Nov 25, 2024
3aa158c
lab4 update
ms-anastasiia Nov 25, 2024
53a1d93
lab 4 update
ms-anastasiia Nov 25, 2024
85dcec1
lab 4 update
ms-anastasiia Nov 25, 2024
278448a
lab 4 update
ms-anastasiia Nov 25, 2024
d5de9ee
lab 4 update
ms-anastasiia Nov 25, 2024
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
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(LibraryCPP)
add_subdirectory(LibraryCPPClass)
#add_subdirectory(LibraryC)
#add_subdirectory(LibraryCPP)
#add_subdirectory(LibraryCPPClass)
add_subdirectory(LibraryCPPTemplate)

add_subdirectory(Lab1C)
add_subdirectory(Lab4CPP)
5 changes: 0 additions & 5 deletions Lab1C/CMakeLists.txt

This file was deleted.

4 changes: 0 additions & 4 deletions Lab1C/input.txt

This file was deleted.

40 changes: 0 additions & 40 deletions Lab1C/lab1.c

This file was deleted.

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}/input.txt)
7 changes: 7 additions & 0 deletions Lab2CPP/input.txt
Original file line number Diff line number Diff line change
@@ -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)
)
127 changes: 127 additions & 0 deletions Lab2CPP/lab2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#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, 8, 9, 10, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 27, 28, 29, 31, 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 49, 50, 53, 54, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 72, 73, 75, 76, 79, 80, 82, 83, 84, 85, 86, 88, 90, 91, 94, 95, 98, 99, 101, 102, 105, 106, 107, 108, 112, 113, 114, 116, 117, 118, 119, 120, 121, 122, 123, 124)
#include <fstream>
#include <stdexcept>
#include "stack.h"

Check failure on line 4 in Lab2CPP/lab2.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Lab2CPP/lab2.cpp:4:10 [clang-diagnostic-error]

'stack.h' file not found
#include "vector.h"
#include <string>

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] == '(') {

Check warning on line 43 in Lab2CPP/lab2.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Lab2CPP/lab2.cpp:43:37 [bugprone-branch-clone]

repeated branch in conditional chain
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;
}
7 changes: 7 additions & 0 deletions Lab3CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/input.txt
# DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
#add_executable(Lab3CPP lab3.cpp)
#target_include_directories(Lab3CPP PUBLIC ../LibraryCPP)
#target_link_libraries(Lab3CPP LibraryCPP)

#add_test(NAME Test1Lab3CPP COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/input.txt)
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.#
######
149 changes: 149 additions & 0 deletions Lab3CPP/lab3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#include <iostream>

Check notice on line 1 in Lab3CPP/lab3.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on Lab3CPP/lab3.cpp

File Lab3CPP/lab3.cpp does not conform to Custom style guidelines. (lines 1, 8, 11, 12, 13, 14, 17, 18, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 39, 40, 41, 43, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 74, 75, 76, 77, 78, 79, 81, 82, 83, 84, 85, 86, 88, 89, 90, 91, 92, 94, 95, 96, 97, 98, 99, 100, 101, 103, 105, 106, 107, 108, 111, 112, 113, 114, 117, 118, 119, 120, 121, 122, 123, 124, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 140, 141, 142, 143, 144, 147)
#include <fstream>
#include <stdexcept>
#include <string>
#include "queue.h"

Check failure on line 5 in Lab3CPP/lab3.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Lab3CPP/lab3.cpp:5:10 [clang-diagnostic-error]

'queue.h' file not found
#include <vector>

struct Point {
int x, y;
};
void Readout(std::ifstream& file, std::vector<char>& labyrinth, size_t* w, size_t* h);
Point Start_search(size_t w, size_t h, const std::vector<char>& labyrinth);
bool Range_and_Wall(int x, int y, size_t w, size_t h, const std::vector<char>& labyrinth, bool* flag_was_here);
Point Number_closest_to_the_starting_position(const std::vector<char>& labyrinth, Point start, size_t w, size_t h);


void Readout(std::ifstream& file, std::vector<char>& labyrinth, size_t* w, size_t* h) {
char simvol;
bool first = true;
int character_counter = 0;

while (file.read(&simvol, 1)) {
if (simvol != '\n') {
labyrinth.push_back(simvol);
character_counter++;
}
if (simvol == '\n') {
if (first) {
*w = character_counter;
first = false;
}
character_counter = 0;
}
}
file.close();
if (*w == 0) {
throw std::runtime_error("The width of the maze is zero");

}
else {
*h = static_cast<int>(labyrinth.size() / *w);
}
}




Point Start_search(size_t w, size_t h, const std::vector<char>& labyrinth) {
for (size_t y = 0; y < h; y++) {
for (size_t x = 0; x < w; x++) {
if (labyrinth[y * w + x] == 'X') {
return {(int) x,(int) y };
}
}
}
return { -1, -1 };
}


bool Range_and_Wall(int x, int y, size_t w, size_t h, const std::vector<char>& labyrinth, bool* flag_was_here) {
if (x < 0 || y < 0 || (size_t)x >= w ||(size_t) y >= h) {
return false;
}
if (flag_was_here[y * w + x]) {
return false;
}
if (labyrinth[y * w + x] == '#') {
return false;
}

return true;
}

Point Number_closest_to_the_starting_position(const std::vector<char>& labyrinth, Point start, size_t w, size_t h) {
Queue* queue = queue_create();
bool* flag_was_here = new bool[w * h]();
Point four_directions[4] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} };
queue_insert(queue, Data(start.y * w + start.x));
flag_was_here[start.y * w + start.x] = true;

while (!queue_empty(queue)) {
int current = queue_get(queue);
queue_remove_one(queue);
int currentX = int(current % w);
int currentY = int(current / w);
char simvol = labyrinth[currentY * w + currentX];

if (simvol >= '0' && simvol <= '9') {
delete[]flag_was_here;
queue_delete(queue);
return { currentX, currentY };
}

for (int i = 0; i < 4; ++i) {
int newX = currentX + four_directions[i].x;
int newY = currentY + four_directions[i].y;
if (Range_and_Wall(newX, newY, w, h, labyrinth, flag_was_here)) {
flag_was_here[newY * w + newX] = true;
queue_insert(queue, Data(newY * w + newX));
}
}

}

queue_delete(queue);
delete[]flag_was_here;
return { -1, -1 };
}


int main(int argc, char** argv) {
std::vector<char> labyrinth;
std::string filepath = argv[1];
std::ifstream file(filepath);


if (!file)
{
std::cout << "the file did not open\n";
return 1;
}
try {
size_t w = 0, h = 0;
Readout(file, labyrinth, &w, &h);

Point start = Start_search(w, h, labyrinth);
if (start.x != -1) {
Point result = Number_closest_to_the_starting_position(labyrinth, start, w, h);
if (result.x != -1) {
std::cout << labyrinth[result.y * w + result.x] << std::endl;
}
else {
std::cout << "The endpoint was not found." << std::endl;
}
}
else {
std::cout << "The starting point was not found." << std::endl;
}

}
catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}

return 0;
}


7 changes: 7 additions & 0 deletions Lab4CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/input.txt
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
add_executable(Lab4CPP lab4.cpp)
include_directories(${CMAKE_SOURCE_DIR}/LibraryCPPTemplate)


add_test(NAME Test1Lab4CPP COMMAND Lab4CPP ${CMAKE_CURRENT_SOURCE_DIR}/input.txt)
Loading
Loading