-
Notifications
You must be signed in to change notification settings - Fork 21
solve homework #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Между инклюдами и первой строчкой кода принято размещать пустую строку (так у текста в файле появляется структура) |
||
|
|
||
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вас можно похвалить за выбор говорящих имён для переменных. Но над именами можно ещё подумать. Например, |
||
| for (char const& active_element : data) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вместо имени А ещё значение элементарного типа тут лучше просто копировать, чем брать по ссылке: |
||
| 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; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В конец файла принято добавлять пустую строку |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| #include <iostream> | ||
|
|
||
| #include <utils.hpp> | ||
|
|
||
| int main() { | ||
|
|
||
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Имена переменных очень неудачные, за исключением, пожалуй, Чтобы не мучиться подбором подходящего имени, иногда можно просто сократить число переменных. Я бы из 6-ти переменных оставил максимум четыре: |
||
|
|
||
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Кстати, на будущее замечу, что если во входной строке не окажется арифметического оператора, то переменная |
||
| } | ||
| } | ||
| } | ||
|
|
||
| int_numbers[n] = atoi(number.c_str()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| 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]); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вместо сцепленных |
||
| return answer; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В первую строку принято помещать включение одноимённого хидера
#include "utils.hpp". Остальные инклюды идут после и отделяются от одноимённого хидела пустой строкой