diff --git a/homework_01/task_01/src/main b/homework_01/task_01/src/main new file mode 100755 index 0000000..9c844d0 Binary files /dev/null and b/homework_01/task_01/src/main differ diff --git a/homework_01/task_01/src/main.cpp b/homework_01/task_01/src/main.cpp index 0b3dcb2..c769d9e 100644 --- a/homework_01/task_01/src/main.cpp +++ b/homework_01/task_01/src/main.cpp @@ -1,9 +1,9 @@ #include -#include +#include "utils.hpp" int main() { - for (const auto& word : SplitString("asdas das das fgag (adasd 1fas)")) { + for (const auto& word : SplitString("a (a a) b (asd as) ")) { std::cout << word << "\n"; } return 0; diff --git a/homework_01/task_01/src/split_string_test.cpp b/homework_01/task_01/src/split_string_test.cpp index 82d4994..2a64a4b 100644 --- a/homework_01/task_01/src/split_string_test.cpp +++ b/homework_01/task_01/src/split_string_test.cpp @@ -12,4 +12,6 @@ TEST_CASE("SplitString", "[simple]") { CHECK(SplitString("a\na\ta a") == std::vector{"a\na", "a", "a"}); CHECK(SplitString("a (a a)") == std::vector{"a", "(a a)"}); CHECK(SplitString("a (a a) b (asd as) ") == std::vector{"a", "(a a)", "b", "(asd as)"}); + CHECK(SplitString("a\ta ") == std::vector{"a", "a"}); + CHECK(SplitString("a\ta") == std::vector{"a", "a"}); } diff --git a/homework_01/task_01/src/utils.cpp b/homework_01/task_01/src/utils.cpp index a6989bd..668522e 100644 --- a/homework_01/task_01/src/utils.cpp +++ b/homework_01/task_01/src/utils.cpp @@ -1,7 +1,42 @@ #include "utils.hpp" +#include #include +#include +#include -std::vector SplitString(const std::string& data) { - return {}; +using namespace std; + + +std::vector SplitString(const std::string& data) { + vector result; + std::string word; + int flag = 0; + for (auto c : data) { + if(c == ')'){ + word += c; + flag = 0; + continue; + } + if(flag == 1){ + word += c; + continue; + } + if(c == '('){ + word += c; + flag = 1; + continue; + } + if(c != ' ' && c != '\t') { + word += c; + } + else if(!word.empty()) { + result.push_back(word); + word = ""; + } + } + if(!word.empty()){ + result.push_back(word); + } + return result; } diff --git a/homework_01/task_02/src/calculate_test.cpp b/homework_01/task_02/src/calculate_test.cpp index 57a8cb9..a36aab2 100644 --- a/homework_01/task_02/src/calculate_test.cpp +++ b/homework_01/task_02/src/calculate_test.cpp @@ -1,4 +1,6 @@ -#include +#include "catch2/catch_test_macros.hpp" +#include + #include "utils.hpp" @@ -22,3 +24,39 @@ TEST_CASE("Calculate", "[simple_multiply]") { CHECK(Calculate("1*0") == 0); CHECK(Calculate("0*0") == 0); } + +TEST_CASE("Calculate", "[simple_division]") { + CHECK(Calculate("10/2") == 5); + CHECK(Calculate("4/2") == 2); + CHECK(Calculate("0/1") == 0); +} + +TEST_CASE("Calculate", "[negative_numbers]") { + CHECK(Calculate("-3-2") == -5); + CHECK(Calculate("-6/2") == -3); + CHECK(Calculate("-8+5") == -3); + CHECK(Calculate("-6*2") == -12); +} + +TEST_CASE("Calculate", "[simple_exponentiation]") { + CHECK(Calculate("3^2") == 9); + CHECK(Calculate("-2.5^2") == Catch::Approx(6.25)); + CHECK(Calculate("-3.5^0") == 1); + CHECK(Calculate("0^2") == Catch::Approx(0)); +} + + +TEST_CASE("Calculate", "[fractional numbers]") { + CHECK(Calculate("6.2*10") == Catch::Approx(62)); + CHECK(Calculate("-1.5+2.5") == Catch::Approx(1)); + CHECK(Calculate("-3.5/3.5") == Catch::Approx(-1)); + CHECK(Calculate("-3.5-3.5") == Catch::Approx(-7)); +} + +TEST_CASE("Calculate", "[advanced_expressions]") { + CHECK(Calculate("6+10/2*2/2-1") == 10); + CHECK(Calculate("(2+3)+5") == 10); + CHECK(Calculate("(-2)+(-2)*(-2)^2") == -10); + CHECK(Calculate("(3/10)+(2.5*2)") == Catch::Approx(5.3)); + CHECK(Calculate("3+((-7)-5/(2+3))*(7-2.5)") == -33); +} \ No newline at end of file diff --git a/homework_01/task_02/src/main b/homework_01/task_02/src/main new file mode 100755 index 0000000..ba59632 Binary files /dev/null and b/homework_01/task_02/src/main differ diff --git a/homework_01/task_02/src/main.cpp b/homework_01/task_02/src/main.cpp index 72a6f13..1bc8461 100644 --- a/homework_01/task_02/src/main.cpp +++ b/homework_01/task_02/src/main.cpp @@ -1,10 +1,10 @@ #include -#include +#include "utils.hpp" int main() { std::string data; std::getline(std::cin, data); - std::cout << Calculate(data); + std::cout << Calculate(data)< -#include +double Calculate(const std::string& data) { + std::stack operation; + std::stack values; + Search(data,operation,values); + while(operation.size() > 0){ + if(!Math(operation, values)){ + return values.top(); + } + } + return values.top(); +} + +bool Math(std::stack& operation, std::stack& values){ + double a,b; + a = values.top(); + values.pop(); + switch (operation.top()) { + case '-': + b = values.top(); + values.pop(); + operation.pop(); + values.push(b - a); + break; + case '+': + b = values.top(); + values.pop(); + operation.pop(); + values.push(b + a); + break; + case '*': + b = values.top(); + values.pop(); + operation.pop(); + values.push(b * a); + break; + case '/': + b = values.top(); + if(a==0){ + std::cerr<<"Can't divide by zero"; + return false; + } + values.pop(); + operation.pop(); + values.push(b / a); + break; + case '^': + b = values.top(); + values.pop(); + operation.pop(); + values.push(pow(b,a)); + break; + default: + return false; + } + return true; +} + +void Search(std::string str,std::stack& operation, std::stack& values) { + std::string word; + int flag = 1; + for (auto c : str) { + if (c == '+' || (c == '-' && flag == 0) || c == '*' || c == '/' || c == '^') { + if(!operation.empty() && Rang(c)<=Rang(operation.top())){ + PushValues(values,word); + Math(operation,values); + operation.push(c); + } + else{ + operation.push(c); + } + } + if(c == '('){ + flag = 1; + operation.push(c); + continue; + } + if(c == ')'){ + PushValues(values,word); + while(operation.top() != '('){ + if(!Math(operation, values)){ + return; + } + } + operation.pop(); + continue; + } + if (c >= '0' && c <= '9' || c == '.' || c == '-' && flag == 1) { + word += c; + flag = 0; + } + else{ + PushValues(values,word); + } + } + PushValues(values,word); +} + +void PushValues(std::stack& values,std::string& word){ + if(!word.empty()){ + values.push(std::stof(word)); + word = ""; + } +} -int Calculate(const std::string& data) { - return 0; +int Rang(char c){ + if(c == '+' || c == '-')return 1; + if(c == '*' || c == '/')return 2; + if(c == '^')return 3; + return 0; } diff --git a/homework_01/task_02/src/utils.hpp b/homework_01/task_02/src/utils.hpp index f1bceeb..7b42a9a 100644 --- a/homework_01/task_02/src/utils.hpp +++ b/homework_01/task_02/src/utils.hpp @@ -1,7 +1,11 @@ #pragma once #include -#include #include - -int Calculate(const std::string& data); +#include +#include +double Calculate(const std::string& data); +void Search(std::string str,std::stack& operation, std::stack& values); +int Rang(char c); +bool Math(std::stack& operation, std::stack& values); +void PushValues(std::stack& values,std::string& word);