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
10 changes: 8 additions & 2 deletions homework_01/task_01/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
#include <utils.hpp>

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;
}
41 changes: 39 additions & 2 deletions homework_01/task_01/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,44 @@
#include "utils.hpp"

#include <stack>
#include <string.h>

#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using namespace - плохая практика

std::vector<std::string> SplitString(const std::string& data) {
return {};
std::vector<std::string> v;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v - плохое имя для переменной

std::string buff = "";
bool flag = false; //скобки закрыты (или их нет)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flag тоже не очень, что это за флаг, за что он отвечает, комментарий это конечно хорошо, но лучше когда код сам по себе читабельный

cout << size(data) << endl;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

думаю это отладка, лучше убрать после того как отладился

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)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В C++ не принято писать and и or, используй вместо && и ||

buff += data[i];
}
cout << "Data" << i << " : " << data[i] << endl;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

отладка?

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 != "") {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!buff.empty()

v.push_back(buff);
buff = "";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше buff.clear()

}
}
}
}
std::cout << "Size of vector: " << v.size() << "\n";
return v;
}
22 changes: 17 additions & 5 deletions homework_01/task_02/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#include <iostream>
#include <string.h>

#include <iostream>
#include <regex>
#include <stack>
#include <utils.hpp>

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;
}
185 changes: 184 additions & 1 deletion homework_01/task_02/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,190 @@
#include "utils.hpp"

#include <string.h>

#include <iostream>
#include <regex>
#include <stack>
#include <string>
#include <vector>
using namespace std;

std::vector<std::string> SplitString(const std::string& data) {
std::vector<std::string> 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<int> chisla;
vector<string> 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<int> chisla;
vector<string> 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;
}
}
5 changes: 5 additions & 0 deletions homework_01/task_02/src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@
#include <vector>

int Calculate(const std::string& data);
std::vector<std::string> 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);