-
Notifications
You must be signed in to change notification settings - Fork 21
homework-tk #14
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?
homework-tk #14
Changes from all commits
5b48ec8
927d13d
d7e0ebf
967dcd6
7d98991
4478790
583d79f
cbf9a23
cbe78b7
9d256ec
eef1f60
e10e177
ad1ddc4
a3f08ef
cffd546
82eaff7
b9d468e
f95057c
6165635
7af5548
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,10 +1,16 @@ | ||
| #include <iostream> | ||
|
|
||
| #include <utils.hpp> | ||
| #include "utils.hpp" | ||
|
|
||
| int main() { | ||
| std::string data; | ||
| std::getline(std::cin, data); | ||
| std::cout << Calculate(data); | ||
|
|
||
| try { | ||
| std::cout << Calculate(data); | ||
| } catch (std::string error) { | ||
| std::cout << error; | ||
| } | ||
|
|
||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,133 @@ | ||
| #include "utils.hpp" | ||
|
|
||
| #include <stack> | ||
| #include <iostream> | ||
| #include <regex> | ||
|
|
||
| int Calculate(const std::string& data) { | ||
| return 0; | ||
| bool is_number(const std::string &str) { | ||
| static const std::regex r(R"([+-]?(\d*\.)?\d+([Ee[+-]?\d+)?)"); | ||
| return std::regex_match(str, r); | ||
| } | ||
|
|
||
| double Calculate(const std::string& data) { | ||
| std::vector<double> terms; | ||
| std::string tmp; | ||
| std::string number; | ||
| bool is_brackets = 0; | ||
| int tmp_brackets = 0; | ||
| bool multipl = 0; | ||
| bool division = 0; | ||
| bool minus = 0; | ||
|
|
||
| for (size_t i = 0; i < data.size(); ++i) { | ||
| if (is_brackets) { | ||
| if (data[i] == ')') { | ||
| if (tmp_brackets) { | ||
| tmp += data[i]; | ||
| tmp_brackets -= 1; | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| double tmp_ans = Calculate(tmp); | ||
|
|
||
| if (multipl) { | ||
| terms[terms.size() - 1] *= tmp_ans; | ||
| multipl = 0; | ||
| } else if (division) { | ||
| terms[terms.size() - 1] /= tmp_ans; | ||
| division = 0; | ||
| } else if (minus) { | ||
| terms.push_back(-tmp_ans); | ||
| minus = 0; | ||
| } else { | ||
| terms.push_back(tmp_ans); | ||
| } | ||
|
|
||
| tmp = ""; | ||
| is_brackets = 0; | ||
| } catch (std::string error) { | ||
| throw (error); | ||
| } | ||
| } else { | ||
| if (data[i] == '(') { | ||
| tmp_brackets += 1; | ||
| } | ||
|
|
||
| tmp += data[i]; | ||
| } | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if (data[i] != '*' && | ||
| data[i] != '+' && | ||
| data[i] != '-' && | ||
| data[i] != '/' && | ||
| data[i] != '(' && | ||
| data[i] != ')') { | ||
| number += data[i]; | ||
| continue; | ||
| } | ||
|
|
||
|
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 (data[i] == '(') { | ||
| is_brackets = 1; | ||
| continue; | ||
| } | ||
|
|
||
| if (!is_number(number)) { | ||
| throw ("ошибка!\n"); | ||
| } | ||
|
|
||
| if (!number.empty()) { | ||
| if (multipl) { | ||
| terms[terms.size() - 1] *= std::stod(number); | ||
| multipl = 0; | ||
| } else if (division) { | ||
| terms[terms.size() - 1] /= std::stod(number); | ||
| division = 0; | ||
| } else if (minus) { | ||
| terms.push_back(-std::stod(number)); | ||
| minus = 0; | ||
| } else { | ||
| terms.push_back(std::stod(number)); | ||
| } | ||
| } | ||
|
|
||
| if (data[i] == '*') { | ||
| multipl = 1; | ||
| } | ||
|
|
||
| if (data[i] == '/') { | ||
| division = 1; | ||
| } | ||
|
|
||
| if (data[i] == '-') { | ||
| minus = 1; | ||
| } | ||
|
|
||
| number = ""; | ||
| } | ||
|
|
||
| if (!number.empty()) { | ||
| if (multipl) { | ||
| terms[terms.size() - 1] *= std::stod(number); | ||
| multipl = 0; | ||
| } else if (division) { | ||
| terms[terms.size() - 1] /= std::stod(number); | ||
| division = 0; | ||
| } else if (minus) { | ||
| terms.push_back(-std::stod(number)); | ||
| minus = 0; | ||
| } else { | ||
| terms.push_back(std::stod(number)); | ||
| } | ||
| } | ||
|
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-else ещё одной проверкой: |
||
|
|
||
| double answer = 0; | ||
|
|
||
| for (auto n : terms) { | ||
| answer += n; | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| #pragma once | ||
|
|
||
| #include <string> | ||
| #include <string_view> | ||
| #include <vector> | ||
|
|
||
| int Calculate(const std::string& data); | ||
| double Calculate(const std::string& data); |
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.
Замечу просто, что если отключать флаг
is_bracketsсразу, как только встречается закрывающая скобка, то вложенные друг в друга скобки точно корректно оброватываться не будут. Впрочем, для первого задания и так хорошо