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
7 changes: 4 additions & 3 deletions homework_01/task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include <iostream>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

оставь пустую строчку, так приянто разделять библиотечные инклуды и инклуды из программы

#include <utils.hpp>

int main() {
for (const auto& word : SplitString("asdas das das fgag (adasd 1fas)")) {
std::cout << word << "\n";
std::string line;
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>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

это сишны инклуд, лучше использовать цппшный (cstring)


#include <iostream>
#include <stack>
#include <string>
#include <vector>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

вставь пустую строчку, так будет лучше выглядеть, и после using namespace

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

плохое имя переменной

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

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

лучше использовать data.size()

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.

and и or - альтернативные записи операторов && и ||. Вы имеете право их использовать, но мне кажется, что это плохая идея. Причина: они используются крайне редко и будут вызывать удивление у коллег программистов. Кроме того, их поддержка зависит от стандартами языка С++, компилятора и набора ключей для компилятора, а значит код становится менее переносимым. Предлагаю заменить and и or на привычный синтаксис && и ||.

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;
}
13 changes: 8 additions & 5 deletions homework_01/task_02/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include <iostream>
#include <string.h>

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

вставь пустую строчку перед #include <utils.hpp>


using namespace std;
int main() {
std::string data;
std::getline(std::cin, data);
std::cout << Calculate(data);
cout << "Itog:" << endl;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

лучше не использовать транслит в коде

cout << Calculate("8/0") << endl;
cout << Calculate("8 / 0") << endl;
return 0;
}
92 changes: 91 additions & 1 deletion homework_01/task_02/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,97 @@
#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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

плохое название переменной

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

в плюсовом коде используются || или &&

(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 = "";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

buff.clear() - смотрится лучше, и даже скорее всего работает быстрее

}
}
} else {
if (i + 1 >= size(data)) {
if (buff != "") {
v.push_back(buff);
buff = "";
}
}
}
}
return v;
}

int Calculate(const std::string& data) {
return 0;
int i = 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

если это индекс для for то обяви его в for
for (int i = 0;....)

int buff_int = 0;
std::string znak = "";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

не используй транслит

int rez = 0;
vector<int> chisla;
vector<string> r = SplitString(data);
for (i = 0; i < r.size(); ++i) {
cout << r[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.

Прошу убрать отладочный код

if ((r[i] != "+") and (r[i] != "-") and (r[i] != "*") and (r[i] != "/")) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Вы в коде несколько раз проверяете, является ли символ арифметическим оператором, а это длинный if с четырьмя проверками. Возможно, стоит ввести специальную функцию bool IsMathOperator(char c). Это позволит не дублировать код и уменьшит вероятность ошибиться

chisla.push_back(stoi(r[i]));
} else {
znak = r[i];
}
}
if (!chisla.empty() and !znak.empty())
rez = chisla[0];
else {
cout << "Error" << endl;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

тут можно просто кинуть исключение, например runtime_error

rez = -1;
}
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 == "/") {
if (chisla[i] != 0)
rez /= chisla[i];
else {
cout << "Delit na Nol nelzya!" << endl;
rez = -1;
}
}
}
}
return rez;
}
1 change: 1 addition & 0 deletions homework_01/task_02/src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
#include <vector>

int Calculate(const std::string& data);
std::vector<std::string> SplitString(const std::string& data);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

эта функция используется в реализации, ее лучше не вытаскивать в hpp, и оставить в cpp