From 70246a4b10b14ede6da20a6eeaa60aec8ced42de Mon Sep 17 00:00:00 2001 From: 0NeR Date: Sat, 12 Nov 2022 08:54:43 +0000 Subject: [PATCH 1/4] first commit --- homework_01/task_01/src/utils.cpp | 45 ++++++++++++++++++++++++++----- homework_01/task_01/src/utils.hpp | 2 +- homework_01/task_02/src/utils.cpp | 29 +++++++++++++++++--- 3 files changed, 65 insertions(+), 11 deletions(-) diff --git a/homework_01/task_01/src/utils.cpp b/homework_01/task_01/src/utils.cpp index a6989bd..2948c93 100644 --- a/homework_01/task_01/src/utils.cpp +++ b/homework_01/task_01/src/utils.cpp @@ -1,7 +1,40 @@ -#include "utils.hpp" +#include +#include +#include -#include - -std::vector SplitString(const std::string& data) { - return {}; -} +std::vector SplitString(const std::string &data) { + std::vector words; + std::string word = ""; + int check_int = 0; + for (char const &symbol : data) { + if (check_int == 0) { + if (symbol == '(') { + check_int = 1; + } else if (symbol != ' ' and symbol != '\t') { + word.push_back(symbol); + } else if (symbol != ' ' or symbol == '\t') { + if (!word.empty()) { + words.push_back(word); + word.clear(); + } + }; + }; + if (check_int == 1) { + if (symbol != ')') { + word.push_back(symbol); + } else if (symbol == ')') { + word.push_back(symbol); + if (!word.empty()) { + words.push_back(word); + word.clear(); + } + check_int = 0; + }; + }; + }; + if (!word.empty()) { + words.push_back(word); + word.clear(); + } + return {words}; +}; \ No newline at end of file diff --git a/homework_01/task_01/src/utils.hpp b/homework_01/task_01/src/utils.hpp index 539ba98..5bd49fa 100644 --- a/homework_01/task_01/src/utils.hpp +++ b/homework_01/task_01/src/utils.hpp @@ -4,4 +4,4 @@ #include #include -std::vector SplitString(const std::string& data); +std::vector SplitString(const std::string& data); \ No newline at end of file diff --git a/homework_01/task_02/src/utils.cpp b/homework_01/task_02/src/utils.cpp index ebc7ffc..62f6053 100644 --- a/homework_01/task_02/src/utils.cpp +++ b/homework_01/task_02/src/utils.cpp @@ -1,7 +1,28 @@ #include "utils.hpp" - +#include #include -int Calculate(const std::string& data) { - return 0; -} +int Calculate(const std::string &data) { + char element_a = data[0]; + + int int_a = int(element_a); + + char action = data[1]; + + char element_b = data[2]; + + int int_b = int(element_b); + + if (action == '+') { + return element_a + element_b; + } + if (action == '-') { + return element_a - element_b; + } + if (action == '/') { + return element_a / element_b; + } + if (action == '*') { + return element_a * element_b; + } +} \ No newline at end of file From 30d6e0c80180548f95eccb482462088226522ed1 Mon Sep 17 00:00:00 2001 From: 0NeR Date: Sat, 26 Nov 2022 08:03:40 +0000 Subject: [PATCH 2/4] solve first homework --- homework_01/task_01/src/utils.cpp | 65 +++++++++++++--------- homework_01/task_02/src/calculate_test.cpp | 11 +++- homework_01/task_02/src/utils.cpp | 44 ++++++++++----- 3 files changed, 78 insertions(+), 42 deletions(-) diff --git a/homework_01/task_01/src/utils.cpp b/homework_01/task_01/src/utils.cpp index 2948c93..0c22a55 100644 --- a/homework_01/task_01/src/utils.cpp +++ b/homework_01/task_01/src/utils.cpp @@ -1,40 +1,51 @@ +#include "utils.hpp" #include #include #include -std::vector SplitString(const std::string &data) { - std::vector words; - std::string word = ""; - int check_int = 0; - for (char const &symbol : data) { - if (check_int == 0) { - if (symbol == '(') { - check_int = 1; - } else if (symbol != ' ' and symbol != '\t') { - word.push_back(symbol); - } else if (symbol != ' ' or symbol == '\t') { - if (!word.empty()) { - words.push_back(word); - word.clear(); +std::vector SplitString(std::string const& data) { + std::vector end_of_working; + std::string active_data = ""; + std::string skob_index = "False"; + + + for (char const& word_symbol : data) { + if (skob_index == "False") { + if (word_symbol == '(') { + skob_index = "True"; + } + else if (word_symbol != ' ') { + if (word_symbol != '\t'){ + active_data.push_back(word_symbol); + } + else if (!active_data.empty()) { + end_of_working.push_back(active_data); + active_data.clear(); } }; }; - if (check_int == 1) { - if (symbol != ')') { - word.push_back(symbol); - } else if (symbol == ')') { - word.push_back(symbol); - if (!word.empty()) { - words.push_back(word); - word.clear(); + + + if (skob_index == "True") { + if (word_symbol != ')') { + active_data.push_back(word_symbol); + } + else if (word_symbol == ')') { + active_data.push_back(word_symbol); + if (!active_data.empty()) { + end_of_working.push_back(active_data); + active_data.clear(); } - check_int = 0; + skob_index = "False"; }; }; }; - if (!word.empty()) { - words.push_back(word); - word.clear(); + + + + if (!active_data.empty()) { + end_of_working.push_back(active_data); + active_data.clear(); } - return {words}; + return end_of_working; }; \ No newline at end of file diff --git a/homework_01/task_02/src/calculate_test.cpp b/homework_01/task_02/src/calculate_test.cpp index 57a8cb9..eccc131 100644 --- a/homework_01/task_02/src/calculate_test.cpp +++ b/homework_01/task_02/src/calculate_test.cpp @@ -3,7 +3,7 @@ #include "utils.hpp" TEST_CASE("Calculate", "[simple_sum]") { - CHECK(Calculate("1+2") == 3); + CHECK(Calculate("12+2") == 14); CHECK(Calculate("2+2") == 4); CHECK(Calculate("1+0") == 1); CHECK(Calculate("0+0") == 0); @@ -17,8 +17,15 @@ TEST_CASE("Calculate", "[simple_difference]") { } TEST_CASE("Calculate", "[simple_multiply]") { - CHECK(Calculate("5*2") == 10); + CHECK(Calculate("52*2") == 104); CHECK(Calculate("2*2") == 4); CHECK(Calculate("1*0") == 0); CHECK(Calculate("0*0") == 0); } + +TEST_CASE("Calculate", "[simple_del]") { + CHECK(Calculate("52/2") == 26); + CHECK(Calculate("2/2") == 1); + CHECK(Calculate("0/3") == 0); + CHECK(Calculate("23/1") == 23); +} \ No newline at end of file diff --git a/homework_01/task_02/src/utils.cpp b/homework_01/task_02/src/utils.cpp index 62f6053..1482857 100644 --- a/homework_01/task_02/src/utils.cpp +++ b/homework_01/task_02/src/utils.cpp @@ -1,28 +1,46 @@ #include "utils.hpp" + +#include #include #include int Calculate(const std::string &data) { - char element_a = data[0]; - - int int_a = int(element_a); + std::string number = ""; + char action; + int int_number = 0; + int int_numbers[2]; + int n = 0; + int i = 0; + int answer = 0; - char action = data[1]; - - char element_b = data[2]; + for (char c : data) { + if ((c == '0') || (c == '1') || (c == '2') || (c == '3') || (c == '4') || + (c == '5') || (c == '6') || (c == '7') || (c == '8') || (c == '9')) { + number = number + c; + } else { + int_number = atoi(number.c_str()); + int_numbers[n] = int_number; + n = n + 1; + number.clear(); + if ((c == '+') || (c == '-') || (c == '*') || (c == '/')) { + action = c; + } + } + } - int int_b = int(element_b); + int_numbers[n] = atoi(number.c_str()); if (action == '+') { - return element_a + element_b; + answer = (int_numbers[0] + int_numbers[1]); } if (action == '-') { - return element_a - element_b; - } - if (action == '/') { - return element_a / element_b; + answer = (int_numbers[0] - int_numbers[1]); } if (action == '*') { - return element_a * element_b; + answer = (int_numbers[0] * int_numbers[1]); + } + if (action == '/') { + answer = (int_numbers[0] / int_numbers[1]); } + return answer; } \ No newline at end of file From bd30418c24c1e427d6cf1f6ec80800cb03885e0c Mon Sep 17 00:00:00 2001 From: 0NeR Date: Mon, 28 Nov 2022 16:04:29 +0000 Subject: [PATCH 3/4] hell --- homework_01/task_01/src/main.cpp | 2 +- homework_01/task_01/src/utils.cpp | 60 +++++++++++-------------------- homework_01/task_02/src/utils.cpp | 1 - 3 files changed, 22 insertions(+), 41 deletions(-) diff --git a/homework_01/task_01/src/main.cpp b/homework_01/task_01/src/main.cpp index 0b3dcb2..0feb697 100644 --- a/homework_01/task_01/src/main.cpp +++ b/homework_01/task_01/src/main.cpp @@ -1,6 +1,6 @@ #include -#include +#include "utils.hpp" int main() { for (const auto& word : SplitString("asdas das das fgag (adasd 1fas)")) { diff --git a/homework_01/task_01/src/utils.cpp b/homework_01/task_01/src/utils.cpp index 0c22a55..d68cd3c 100644 --- a/homework_01/task_01/src/utils.cpp +++ b/homework_01/task_01/src/utils.cpp @@ -1,51 +1,33 @@ -#include "utils.hpp" #include #include #include - std::vector SplitString(std::string const& data) { - std::vector end_of_working; + std::vector finish_array; std::string active_data = ""; - std::string skob_index = "False"; - - - for (char const& word_symbol : data) { - if (skob_index == "False") { - if (word_symbol == '(') { - skob_index = "True"; - } - else if (word_symbol != ' ') { - if (word_symbol != '\t'){ - active_data.push_back(word_symbol); - } - else if (!active_data.empty()) { - end_of_working.push_back(active_data); - active_data.clear(); - } + bool brackets_condition = false; + for (char const& active_element : data) { + if (brackets_condition) { + active_data.push_back(active_element); + if (active_element == ')') { + brackets_condition = false; + finish_array.push_back(active_data); + active_data = ""; }; - }; - - - if (skob_index == "True") { - if (word_symbol != ')') { - active_data.push_back(word_symbol); - } - else if (word_symbol == ')') { - active_data.push_back(word_symbol); + } else { + if (active_element == ' ' || active_element == '\t') { if (!active_data.empty()) { - end_of_working.push_back(active_data); - active_data.clear(); + finish_array.push_back(active_data); + active_data = ""; } - skob_index = "False"; - }; + } else { + active_data.push_back(active_element); + if (active_element == '(') brackets_condition = true; + } }; }; - - - if (!active_data.empty()) { - end_of_working.push_back(active_data); - active_data.clear(); + finish_array.push_back(active_data); + active_data = ""; } - return end_of_working; -}; \ No newline at end of file + return finish_array; +}; diff --git a/homework_01/task_02/src/utils.cpp b/homework_01/task_02/src/utils.cpp index 1482857..8d6535b 100644 --- a/homework_01/task_02/src/utils.cpp +++ b/homework_01/task_02/src/utils.cpp @@ -10,7 +10,6 @@ int Calculate(const std::string &data) { int int_number = 0; int int_numbers[2]; int n = 0; - int i = 0; int answer = 0; for (char c : data) { From e6e880898a4286cc15ef1c1fca95c5180cc79066 Mon Sep 17 00:00:00 2001 From: 0NeR Date: Sat, 17 Dec 2022 06:53:56 +0000 Subject: [PATCH 4/4] wip --- homework_01/task_01/src/split_string_test.cpp | 3 +- homework_01/task_01/src/utils.cpp | 33 ++++++++++--------- homework_01/task_02/src/main.cpp | 1 - homework_01/task_02/src/utils.cpp | 12 ++----- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/homework_01/task_01/src/split_string_test.cpp b/homework_01/task_01/src/split_string_test.cpp index 82d4994..c501970 100644 --- a/homework_01/task_01/src/split_string_test.cpp +++ b/homework_01/task_01/src/split_string_test.cpp @@ -11,5 +11,6 @@ TEST_CASE("SplitString", "[simple]") { CHECK(SplitString("??? ??") == std::vector{"???", "??"}); 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 (a a) b (asd as) ") == + std::vector{"a", "(a a)", "b", "(asd as)"}); } diff --git a/homework_01/task_01/src/utils.cpp b/homework_01/task_01/src/utils.cpp index d68cd3c..fd6150f 100644 --- a/homework_01/task_01/src/utils.cpp +++ b/homework_01/task_01/src/utils.cpp @@ -1,33 +1,36 @@ +#include "utils.hpp" + #include #include #include + std::vector SplitString(std::string const& data) { - std::vector finish_array; - std::string active_data = ""; + std::vector result; + std::string token = ""; bool brackets_condition = false; for (char const& active_element : data) { if (brackets_condition) { - active_data.push_back(active_element); + token.push_back(active_element); if (active_element == ')') { brackets_condition = false; - finish_array.push_back(active_data); - active_data = ""; - }; + result.push_back(token); + token = ""; + } } else { if (active_element == ' ' || active_element == '\t') { - if (!active_data.empty()) { - finish_array.push_back(active_data); - active_data = ""; + if (!token.empty()) { + result.push_back(token); + token = ""; } } else { active_data.push_back(active_element); if (active_element == '(') brackets_condition = true; } - }; - }; - if (!active_data.empty()) { - finish_array.push_back(active_data); - active_data = ""; + } + } + if (!token.empty()) { + result.push_back(active_data); + token = ""; } - return finish_array; + return result; }; diff --git a/homework_01/task_02/src/main.cpp b/homework_01/task_02/src/main.cpp index 72a6f13..6d061bd 100644 --- a/homework_01/task_02/src/main.cpp +++ b/homework_01/task_02/src/main.cpp @@ -1,5 +1,4 @@ #include - #include int main() { diff --git a/homework_01/task_02/src/utils.cpp b/homework_01/task_02/src/utils.cpp index 8d6535b..37a84b4 100644 --- a/homework_01/task_02/src/utils.cpp +++ b/homework_01/task_02/src/utils.cpp @@ -7,20 +7,14 @@ int Calculate(const std::string &data) { std::string number = ""; char action; - int int_number = 0; - int int_numbers[2]; - int n = 0; + int variables[2]; int answer = 0; for (char c : data) { - if ((c == '0') || (c == '1') || (c == '2') || (c == '3') || (c == '4') || - (c == '5') || (c == '6') || (c == '7') || (c == '8') || (c == '9')) { + if (c >= '0' && c <= '9') { number = number + c; } else { - int_number = atoi(number.c_str()); - int_numbers[n] = int_number; - n = n + 1; - number.clear(); + numbers.push_back(std::stoi(number_part)) number.clear(); if ((c == '+') || (c == '-') || (c == '*') || (c == '/')) { action = c; }