Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion homework_01/task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <iostream>

#include <utils.hpp>
#include "utils.hpp"

int main() {
for (const auto& word : SplitString("asdas das das fgag (adasd 1fas)")) {
Expand Down
3 changes: 2 additions & 1 deletion homework_01/task_01/src/split_string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ TEST_CASE("SplitString", "[simple]") {
CHECK(SplitString("??? ??") == std::vector<std::string>{"???", "??"});
CHECK(SplitString("a\na\ta a") == std::vector<std::string>{"a\na", "a", "a"});
CHECK(SplitString("a (a a)") == std::vector<std::string>{"a", "(a a)"});
CHECK(SplitString("a (a a) b (asd as) ") == std::vector<std::string>{"a", "(a a)", "b", "(asd as)"});
CHECK(SplitString("a (a a) b (asd as) ") ==
std::vector<std::string>{"a", "(a a)", "b", "(asd as)"});
}
37 changes: 33 additions & 4 deletions homework_01/task_01/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
#include "utils.hpp"

#include <stack>
#include <string>
#include <string_view>
#include <vector>

std::vector<std::string> SplitString(const std::string& data) {
return {};
}
std::vector<std::string> SplitString(std::string const& data) {
std::vector<std::string> result;
std::string token = "";
bool brackets_condition = false;
for (char const& active_element : data) {
if (brackets_condition) {
token.push_back(active_element);
if (active_element == ')') {
brackets_condition = false;
result.push_back(token);
token = "";
}
} else {
if (active_element == ' ' || active_element == '\t') {
if (!token.empty()) {
result.push_back(token);
token = "";
}
} else {
active_data.push_back(active_element);
if (active_element == '(') brackets_condition = true;
}
}
}
if (!token.empty()) {
result.push_back(active_data);
token = "";
}
return result;
};
2 changes: 1 addition & 1 deletion homework_01/task_01/src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
#include <string_view>
#include <vector>

std::vector<std::string> SplitString(const std::string& data);
std::vector<std::string> SplitString(const std::string& data);
11 changes: 9 additions & 2 deletions homework_01/task_02/src/calculate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
1 change: 0 additions & 1 deletion homework_01/task_02/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <iostream>

#include <utils.hpp>

int main() {
Expand Down
38 changes: 35 additions & 3 deletions homework_01/task_02/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
#include "utils.hpp"

#include <cstring>
#include <iostream>
#include <stack>

int Calculate(const std::string& data) {
return 0;
}
int Calculate(const std::string &data) {
std::string number = "";
char action;
int variables[2];
int answer = 0;

for (char c : data) {
if (c >= '0' && c <= '9') {
number = number + c;
} else {
numbers.push_back(std::stoi(number_part)) number.clear();
if ((c == '+') || (c == '-') || (c == '*') || (c == '/')) {
action = c;
}
}
}

int_numbers[n] = atoi(number.c_str());

if (action == '+') {
answer = (int_numbers[0] + int_numbers[1]);
}
if (action == '-') {
answer = (int_numbers[0] - int_numbers[1]);
}
if (action == '*') {
answer = (int_numbers[0] * int_numbers[1]);
}
if (action == '/') {
answer = (int_numbers[0] / int_numbers[1]);
}
return answer;
}