Skip to content

Commit

Permalink
Merge branch 'main' into shemiakina-alesia-lab3
Browse files Browse the repository at this point in the history
  • Loading branch information
alesyacurtis authored Jun 11, 2024
2 parents 1843829 + 0735d63 commit 1d1e072
Show file tree
Hide file tree
Showing 28 changed files with 942 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lab-guide/topics.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
| Генератор всевозможных сочетаний k элементов массива |Васильев Иван 3821Б1ФИ3|Тушенцова Карина 3821Б1ПР1|
| Ханойская башня |Поздняков Василий 3821Б1ПР1|Шмелев Иван 3821Б1ФИ1|
| Минимальное число ходов коня на шахматной доске между двумя позициями (с препятствиями) |Подъячих Михаил 3821Б1ПР1|Махиня Данил 3821Б1ПМоп3|
| Минимальное остовное дерево (Алгоритм Краскала, Прима или Борувки) |Сафронов Михаил 3821Б1ФИ3||
| Минимальное остовное дерево (Алгоритм Краскала, Прима или Борувки) |Сафронов Михаил 3821Б1ФИ3|Сафронов Михаил 3821Б1ФИ3|

### Раздел: Структуры данных

Expand All @@ -48,7 +48,7 @@
| Двухсторонняя очередь |Кутарин Александр 3821Б1ПР2|Филатов Максим 3821Б1ПР2|
| Линейный односвязный список |Новостроев Иван 3821Б1ПР2|Кутарин Александр 3821Б1ПР2|
| Линейный двухсвязный список |Филатов Максим 3821Б1ПР2|Иванченко Алексей 3821Б1ПМоп3|
| Дерево (построение, поиск по значению) |Никитин Кирилл 3821Б1ПР2|Сафронов Михаил 3821Б1ФИ3|
| Дерево (построение, поиск по значению) |Никитин Кирилл 3821Б1ПР2||
| Граф (подсчет числа компонент связности) |Шемякина Алеся 3821Б1ПМоп3|Коновалов Игорь 3821Б1ПМоп3|
| Левосторонняя куча |Федорец Илья 3821Б1ПР3||
| Модифицированный стек |Кистримова Екатерина 3821Б1ПР2|Бахтияров Александр 3821Б1ПР2|
Expand Down
1 change: 1 addition & 0 deletions modules/graph_comp_shem_a/application/graph_components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <iostream>


int main(int argc, char* argv[]) {
auto output = GraphComponentsApplication()(argc, argv);

Expand Down
1 change: 1 addition & 0 deletions modules/graph_comp_shem_a/include/graph_components_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <vector>
#include <utility>
#include <algorithm>

#include <string>

#include "include/graph_components.h"
Expand Down
1 change: 1 addition & 0 deletions modules/graph_comp_shem_a/src/graph_components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "include/graph_components.h"

#include <vector>
#include <iostream>

void DFS(int x, std::vector<std::vector<int>>& v, std::vector<int>& was) {
was[x] = 1;
Expand Down
2 changes: 2 additions & 0 deletions modules/graph_comp_shem_a/src/graph_components_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <cstring>
#include <sstream>
#include <iostream>
#include <stdexcept>
#include <vector>

bool GraphComponentsApplication::validate(int argc, char* argv[]) {
if (argc == 1) {
Expand Down
12 changes: 12 additions & 0 deletions modules/kokin_sort_station_org/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Declare variables for binaries' names
get_filename_component(DIR_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
set(MODULE "${DIR_NAME}")
set(LIBRARY "lib_${MODULE}")
set(TESTS "test_${MODULE}")

# Include directory with public headers
include_directories(${CMAKE_CURRENT_SOURCE_DIR} include)

# Add all submodules
add_subdirectory(src)
add_subdirectory(test)
50 changes: 50 additions & 0 deletions modules/kokin_sort_station_org/CTestTests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#############################################
##### Testing
#############################################

set(prefix "${MODULE}")

add_test(
NAME ${prefix}_run
COMMAND ${APPLICATION}
)
set_tests_properties (${prefix}_run PROPERTIES
PASS_REGULAR_EXPRESSION "Usage:"
LABELS "${MODULE}"
)

add_test(
NAME ${prefix}_run_help
COMMAND ${APPLICATION} --help
)
set_tests_properties (${prefix}_run_help PROPERTIES
PASS_REGULAR_EXPRESSION "Usage:"
LABELS "${MODULE}"
)

add_test(
NAME ${prefix}_run_expression
COMMAND ${APPLICATION} "2+2"
)
set_tests_properties (${prefix}_run_expression PROPERTIES
PASS_REGULAR_EXPRESSION "Result: 4"
LABELS "${MODULE}"
)

add_test(
NAME ${prefix}_run_complex_expression
COMMAND ${APPLICATION} "2+3*4"
)
set_tests_properties (${prefix}_run_complex_expression PROPERTIES
PASS_REGULAR_EXPRESSION "Result: 14"
LABELS "${MODULE}"
)

add_test(
NAME ${prefix}_run_expression_with_parentheses
COMMAND ${APPLICATION} "(2+3)*4"
)
set_tests_properties (${prefix}_run_expression_with_parentheses PROPERTIES
PASS_REGULAR_EXPRESSION "Result: 20"
LABELS "${MODULE}"
)
15 changes: 15 additions & 0 deletions modules/kokin_sort_station_org/application/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
set(target ${APPLICATION})

file(GLOB srcs "*.cpp")
set_source_files_properties(${srcs} PROPERTIES
LABELS "${MODULE};Application")
include_directories(include)
add_executable(${target} ${srcs})
set_target_properties(${target} PROPERTIES
OUTPUT_NAME ${MODULE}
LABELS "${MODULE};Application")

target_link_libraries(${target} ${LIBRARY})
if (UNIX)
target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT})
endif (UNIX)
11 changes: 11 additions & 0 deletions modules/kokin_sort_station_org/application/application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2024 Kokin Ivan

#include <iostream>

#include "Console_app.h"

int main(int argc, char** argv) {
ConsoleApp app;
app.run(argc, argv);
return 0;
}
20 changes: 20 additions & 0 deletions modules/kokin_sort_station_org/include/Console_app.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2024 Kokin Ivan
#ifndef MODULES_KOKIN_SORT_STATION_ORG_INCLUDE_CONSOLE_APP_H_
#define MODULES_KOKIN_SORT_STATION_ORG_INCLUDE_CONSOLE_APP_H_

#include <string>

#include "include/sort_station.h"


class ConsoleApp {
public:
void run(int argc, const char** argv);

private:
void printHelp();
std::string calculateExpression(const std::string& expression);
SortStationDerived sortStation;
};

#endif // MODULES_KOKIN_SORT_STATION_ORG_INCLUDE_CONSOLE_APP_H_
23 changes: 23 additions & 0 deletions modules/kokin_sort_station_org/include/sort_station.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2024 Kokin Ivan

#ifndef MODULES_KOKIN_SORT_STATION_ORG_INCLUDE_SORT_STATION_H_
#define MODULES_KOKIN_SORT_STATION_ORG_INCLUDE_SORT_STATION_H_

#include <unordered_map>
#include <string>

class SortStationDerived {
public:
double calculateFunction(const std::string& expression);
double evaluatePostfixExpression(const std::string& postfix);
private:
double applyOperator(double a, double b, char op);
std::string convertToPostfix(const std::string& expression);

protected:
std::unordered_map<char, int> precedence = {
{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'^', 3}
};
};

#endif // MODULES_KOKIN_SORT_STATION_ORG_INCLUDE_SORT_STATION_H_
18 changes: 18 additions & 0 deletions modules/kokin_sort_station_org/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
set(target ${LIBRARY})

file(GLOB srcs "*.cpp")
file(GLOB hdrs "../include/*.h")
set_source_files_properties(${srcs} ${hdrs} PROPERTIES
LABELS "${MODULE};Library")

add_library(${target} STATIC ${srcs} ${hdrs})
set_target_properties(${target} PROPERTIES
OUTPUT_NAME ${MODULE}
LABELS "${MODULE};Library")

if (UNIX)
target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT})
endif (UNIX)
target_link_libraries(${target} ${LIBRARY_DEPS})

set(LIBRARY_DEPS "${LIBRARY_DEPS};${target}" PARENT_SCOPE)
31 changes: 31 additions & 0 deletions modules/kokin_sort_station_org/src/Console_app.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 Kokin Ivan
#include <iostream>
#include <string>

#include "Console_app.h"

void ConsoleApp::run(int argc, const char** argv) {
if (argc == 1) {
printHelp();
} else if (argc == 2) {
std::string expression = argv[1];
calculateExpression(expression);
} else {
std::cerr << "Invalid number of arguments." << std::endl;
printHelp();
}
}

void ConsoleApp::printHelp() {
std::cout << "Usage: " << std::endl;
std::cout << " SortStation [expression]" << std::endl;
std::cout << "Example: " << std::endl;
std::cout << " SortStation 2+2" << std::endl;
}

std::string ConsoleApp::calculateExpression(const std::string& expression) {
std::string s = "Result: " + std::to_string(
sortStation.calculateFunction(expression));
std::cout << s << std::endl;
return s;
}
72 changes: 72 additions & 0 deletions modules/kokin_sort_station_org/src/sort_station.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2024 Kokin Ivan

#include <math.h>
#include <cctype>
#include <stack>
#include <string>

#include "../include/sort_station.h"

double SortStationDerived::calculateFunction(const std::string& expression) {
std::string postfix = convertToPostfix(expression);
return evaluatePostfixExpression(postfix);
}

double SortStationDerived::evaluatePostfixExpression(
const std::string& postfix) {
std::stack<double> stack;
for (char c : postfix) {
if (isdigit(c)) {
stack.push(static_cast<double>(c - '0'));
} else {
double b = stack.top(); stack.pop();
double a = stack.top(); stack.pop();
stack.push(applyOperator(a, b, c));
}
}
return stack.top();
}

double SortStationDerived::applyOperator(double a, double b, char op) {
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
case '^': return std::pow(a, b);
default: return 0.0;
}
}

std::string SortStationDerived::convertToPostfix(
const std::string& expression) {
std::string postfix;
std::stack<char> stack;
for (char c : expression) {
if (isdigit(c)) {
postfix += c;
} else if (c == '(') {
stack.push(c);
} else if (c == ')') {
while (!stack.empty() && stack.top() != '(') {
postfix += stack.top();
stack.pop();
}
if (!stack.empty() && stack.top() == '(') {
stack.pop();
}
} else {
while (!stack.empty() && precedence[stack.top()] >= precedence[c]) {
postfix += stack.top();
stack.pop();
}
stack.push(c);
}
}
while (!stack.empty()) {
postfix += stack.top();
stack.pop();
}
return postfix;
}

27 changes: 27 additions & 0 deletions modules/kokin_sort_station_org/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
set(target ${TESTS})

file(GLOB srcs "*.cpp")
set_source_files_properties(${srcs} PROPERTIES
LABELS "${MODULE};Test")

add_executable(${target} ${srcs} ${hdrs})
set_target_properties(${target} PROPERTIES
LABELS "${MODULE};Test")

if (UNIX)
target_link_libraries(${target} gtest ${CMAKE_THREAD_LIBS_INIT} pthread)
endif (UNIX)
target_link_libraries(${target} gtest ${LIBRARY})

# VS2012 doesn't support correctly the tuples yet,
# see http://code.google.com/p/googletest/issues/detail?id=412
if(MSVC)
target_compile_definitions(${target} PUBLIC _VARIADIC_MAX=10)
endif()

add_test(
NAME ${MODULE}_gtest
COMMAND ${target}
)
set_tests_properties (${MODULE}_gtest PROPERTIES
LABELS "${MODULE}")
Loading

0 comments on commit 1d1e072

Please sign in to comment.