-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/solve first homework #1
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,44 @@ | ||
| #include "utils.hpp" | ||
|
|
||
| #include <stack> | ||
| #include <string.h> | ||
|
|
||
| #include <iostream> | ||
| #include <stack> | ||
| #include <string> | ||
| #include <vector> | ||
| using namespace std; | ||
| std::vector<std::string> SplitString(const std::string& data) { | ||
| return {}; | ||
| std::vector<std::string> v; | ||
|
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. v - плохое имя для переменной |
||
| std::string buff = ""; | ||
| bool flag = 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. flag тоже не очень, что это за флаг, за что он отвечает, комментарий это конечно хорошо, но лучше когда код сам по себе читабельный |
||
| cout << size(data) << endl; | ||
|
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 (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)) { | ||
|
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. В C++ не принято писать and и or, используй вместо && и || |
||
| buff += data[i]; | ||
| } | ||
| cout << "Data" << i << " : " << data[i] << endl; | ||
|
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. отладка? |
||
| 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 != "") { | ||
|
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. !buff.empty() |
||
| v.push_back(buff); | ||
| buff = ""; | ||
|
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. лучше buff.clear() |
||
| } | ||
| } | ||
| } | ||
| } | ||
| std::cout << "Size of vector: " << v.size() << "\n"; | ||
| return v; | ||
| } | ||
| 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; | ||
| } |
| 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; | ||
| } | ||
| } |
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.
using namespace - плохая практика