Skip to content

solve homework - #6

Open
0NeR wants to merge 4 commits into
DafeMipt212:mainfrom
0NeR:feature/solve_first_homework
Open

solve homework#6
0NeR wants to merge 4 commits into
DafeMipt212:mainfrom
0NeR:feature/solve_first_homework

Conversation

@0NeR

@0NeR 0NeR commented Nov 12, 2022

Copy link
Copy Markdown

No description provided.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

В первую строку принято помещать включение одноимённого хидера #include "utils.hpp". Остальные инклюды идут после и отделяются от одноимённого хидела пустой строкой

}
#include <string>
#include <string_view>
#include <vector>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Между инклюдами и первой строчкой кода принято размещать пустую строку (так у текста в файле появляется структура)

std::vector<std::string> SplitString(std::string const& data) {
std::vector<std::string> finish_array;
std::string active_data = "";
bool brackets_condition = 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.

Вас можно похвалить за выбор говорящих имён для переменных. Но над именами можно ещё подумать. Например, final_array лечше, чем finish_array, а ещё лучше result или res. active_data я бы переназвал как word или token

std::vector<std::string> finish_array;
std::string active_data = "";
bool brackets_condition = false;
for (char const& active_element : data) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Вместо имени active_element можно просто ch или даже c. Если хотите избегать коротких имён, то больше подходит имя current_char

А ещё значение элементарного типа тут лучше просто копировать, чем брать по ссылке: for (const char active_element : data) {

Comment thread homework_01/task_01/src/utils.cpp Outdated
if (active_element == '(') brackets_condition = 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.

Прошу убрать ненужные ; после закрывающих блок скобок

Comment thread homework_01/task_02/src/utils.cpp Outdated

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

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 (c >= '0' && c <= '9') {

Comment thread homework_01/task_02/src/utils.cpp Outdated
} else {
int_number = atoi(number.c_str());
int_numbers[n] = int_number;
n = n + 1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Вместо трёх строк 20-22 я бы написал numbers.push_back(std::stoi(number_part)). И переменные int_number и n больше не нужны

n = n + 1;
number.clear();
if ((c == '+') || (c == '-') || (c == '*') || (c == '/')) {
action = c;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Кстати, на будущее замечу, что если во входной строке не окажется арифметического оператора, то переменная action останется вообще неинициализорованной, и обращение к ней приведёт к undefined behavior. Лучше в коде такого не допускать

}
}

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

numbers.push_back(std::stoi(number_part));

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

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 для целочисленных типов рекомендуется использовать оператор switch (но это вопрос вкуса/кодстайла)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants