-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into shemiakina-alesia-lab3
- Loading branch information
Showing
28 changed files
with
942 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
modules/kokin_sort_station_org/application/application.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
Oops, something went wrong.