diff --git a/homework_01/task_01/src/main.cpp b/homework_01/task_01/src/main.cpp index 0b3dcb2..ff7c495 100644 --- a/homework_01/task_01/src/main.cpp +++ b/homework_01/task_01/src/main.cpp @@ -3,8 +3,14 @@ #include int main() { - for (const auto& word : SplitString("asdas das das fgag (adasd 1fas)")) { - std::cout << word << "\n"; + std::string line; + /*std::getline(std::cin, line); + for (const auto& word : SplitString(line)) { + std::cout << word << '\n'; + }*/ + line="a\na\ta a"; + for (const auto& word : SplitString(line)) { + std::cout << word << '\n'; } return 0; } diff --git a/homework_01/task_01/src/utils.cpp b/homework_01/task_01/src/utils.cpp index a6989bd..44f52b8 100644 --- a/homework_01/task_01/src/utils.cpp +++ b/homework_01/task_01/src/utils.cpp @@ -1,7 +1,44 @@ #include "utils.hpp" -#include +#include +#include +#include +#include +#include +using namespace std; std::vector SplitString(const std::string& data) { - return {}; + std::vector v; + std::string buff = ""; + bool flag = false; //скобки закрыты (или их нет) + cout << size(data) << endl; + for (int i = 0; i < size(data); ++i) { + if (data[i] == '(') { + flag = true; //скобки открыты + } else if (data[i] == ')') { + flag = false; //скобки закрыты + } + if (((data[i] != ' ') and (data[i] != '\t')) or (flag == true)) { + buff += data[i]; + } + cout << "Data" << i << " : " << data[i] << endl; + cout << "Buff: " << buff << endl; + if (flag == false) { + if ((data[i] == ' ') or (data[i] == '\t') or (i + 1 >= size(data))) { + if (buff != "") { + v.push_back(buff); + buff = ""; + } + } + } else { + if (i + 1 >= size(data)) { + if (buff != "") { + v.push_back(buff); + buff = ""; + } + } + } + } + std::cout << "Size of vector: " << v.size() << "\n"; + return v; } diff --git a/homework_01/task_02/src/main.cpp b/homework_01/task_02/src/main.cpp index 72a6f13..f0b65d0 100644 --- a/homework_01/task_02/src/main.cpp +++ b/homework_01/task_02/src/main.cpp @@ -1,10 +1,22 @@ -#include +#include +#include +#include +#include #include - +using namespace std; int main() { - std::string data; - std::getline(std::cin, data); - std::cout << Calculate(data); + string input; + int res; + cout << "REZULTAT" << Calculate("5*9-8") << endl; + while (7) { + std::cout << "Vvedite, poghaluista, stroku: " << std::endl; + getline(cin, input); + if (input == "quit") { + break; + } + cout << Parce(input) << endl; + cout << "Double: " << IsDouble(input) << endl; + } return 0; } diff --git a/homework_01/task_02/src/utils.cpp b/homework_01/task_02/src/utils.cpp index ebc7ffc..00b524e 100644 --- a/homework_01/task_02/src/utils.cpp +++ b/homework_01/task_02/src/utils.cpp @@ -1,7 +1,190 @@ #include "utils.hpp" +#include + +#include +#include #include +#include +#include +using namespace std; + +std::vector SplitString(const std::string& data) { + std::vector v; + std::string buff = ""; + bool flag = false; + for (int i = 0; i < size(data); ++i) { + if (data[i] == '(') { + flag = true; + } else if (data[i] == ')') { + flag = false; + } + if (((data[i] != ' ') and (data[i] != '\t') and (data[i] != '+') and + (data[i] != '-') and (data[i] != '*') and (data[i] != '/')) or + (flag == true)) { + buff += data[i]; + } + if (flag == false) { + if ((data[i] == ' ') or (data[i] == '\t') or (data[i] == '+') or + (data[i] == '-') or (data[i] == '*') or (data[i] == '/') or + (i + 1 >= size(data))) { + if (buff != "") { + v.push_back(buff); + buff = ""; + if ((data[i] == '+') or (data[i] == '-') or (data[i] == '*') or + (data[i] == '/')) { + buff += data[i]; + v.push_back(buff); + buff = ""; + } + } + } + } else { + if (i + 1 >= size(data)) { + if (buff != "") { + v.push_back(buff); + buff = ""; + } + } + } + } + return v; +} + +bool IsInt_bad_version(const std::string& str) { + int a = 1; + bool flag = true; + if (str.empty()) flag = false; + for (size_t i = 1; i < str.size(); ++i) { + if ((str[i] >= '0') && (str[i] <= '9')) + a = 1; + else + flag = false; + + if ((!std::isdigit(str[0])) && (str[0] != '-') && (str[0] != '+')) { + flag = false; + } + } + return flag; +} + +bool IsInt(const std::string& str) { + static std::regex r(R"([+-]?\d+)"); // проверяем число ли это в системе + // regular expressions (regex) + return std::regex_match(str, r); +} + +bool IsDouble(const std::string& str) { + static std::regex r( + R"([+-]?(\d*\.)?\d+([Ee][+-]?\d+)?)"); // проверяем double число ли это в + // системе regular expressions + // (regex) + return std::regex_match(str, r); +} + +string Parce(const std::string& input) { + bool Int_test = IsInt(input); + if (Int_test) { + return "true"; + std: + cout << std::stod(input) << std::endl; + } else + return "false"; +} int Calculate(const std::string& data) { - return 0; + int i = 0; + int buff_int = 0; + std::string znak; + int rez = 0; + // std::string buff_str; + vector chisla; + vector r = SplitString(data); + for (int i = 0; i < r.size(); ++i) { + std::cout << i << "element: " << r[i] << '\n'; + } + for (i = 0; i < data.size(); ++i) { + if ((data[i] >= '0') && (data[i] <= '9')) { + } + for (i = 0; i < r.size(); ++i) { + if ((r[i] != "+") and (r[i] != "-") and (r[i] != "*") and (r[i] != "/")) { + chisla.push_back(stoi(r[i])); + } else { + znak = r[i]; + } + } + if (!chisla.empty()) + rez = chisla[0]; + else { + cout << "Error" << endl; + rez = -1; + } + cout << znak << endl; + if (!chisla.empty()) { + for (i = 1; i < chisla.size(); ++i) { + if (znak == "+") { + rez += chisla[i]; + } + if (znak == "-") { + rez -= chisla[i]; + } + if (znak == "*") { + rez *= chisla[i]; + } + if (znak == "/") { + rez /= chisla[i]; + } + } + } + return rez; + } +} + +int CalculatePrefix(const std::string& data) { + cout << "Not Ready" << endl; + int i = 0; + int buff_int = 0; + std::string znak; + int rez = 0; + // std::string buff_str; + vector chisla; + vector r = SplitString(data); + for (int i = 0; i < r.size(); ++i) { + std::cout << i << "element: " << r[i] << '\n'; + } + for (i = 0; i < data.size(); ++i) { + if ((data[i] >= '0') && (data[i] <= '9')) { + } + for (i = 0; i < r.size(); ++i) { + if ((r[i] != "+") and (r[i] != "-") and (r[i] != "*") and (r[i] != "/")) { + chisla.push_back(stoi(r[i])); + } else { + znak = r[i]; + } + } + if (!chisla.empty()) + rez = chisla[0]; + else { + cout << "Error" << endl; + rez = -1; + } + cout << znak << endl; + if (!chisla.empty()) { + for (i = 1; i < chisla.size(); ++i) { + if (znak == "+") { + rez += chisla[i]; + } + if (znak == "-") { + rez -= chisla[i]; + } + if (znak == "*") { + rez *= chisla[i]; + } + if (znak == "/") { + rez /= chisla[i]; + } + } + } + return rez; + } } diff --git a/homework_01/task_02/src/utils.hpp b/homework_01/task_02/src/utils.hpp index f1bceeb..711028c 100644 --- a/homework_01/task_02/src/utils.hpp +++ b/homework_01/task_02/src/utils.hpp @@ -5,3 +5,8 @@ #include int Calculate(const std::string& data); +std::vector SplitString(const std::string& data); +bool IsInt_bad_version(const std::string& str); +bool IsInt(const std::string& str); +bool IsDouble(const std::string& str); +std::string Parce(const std::string& input); \ No newline at end of file