-
Notifications
You must be signed in to change notification settings - Fork 21
My pull request #9
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,42 @@ | ||
| #include "utils.hpp" | ||
|
|
||
| #include <iostream> | ||
| #include <stack> | ||
| #include <regex> | ||
| #include <vector> | ||
|
|
||
| std::vector<std::string> SplitString(const std::string& data) { | ||
| return {}; | ||
| using namespace std; | ||
|
|
||
|
|
||
| std::vector<string> SplitString(const std::string& data) { | ||
| vector<string> result; | ||
| std::string word; | ||
| int flag = 0; | ||
|
Contributor
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. тут лучше подойдет тип bool |
||
| for (auto c : data) { | ||
| if(c == ')'){ | ||
| word += c; | ||
| flag = 0; | ||
| continue; | ||
| } | ||
| if(flag == 1){ | ||
| word += c; | ||
| continue; | ||
| } | ||
| if(c == '('){ | ||
| word += c; | ||
| flag = 1; | ||
| continue; | ||
| } | ||
| if(c != ' ' && c != '\t') { | ||
| word += c; | ||
| } | ||
| else if(!word.empty()) { | ||
| result.push_back(word); | ||
| word = ""; | ||
| } | ||
| } | ||
| if(!word.empty()){ | ||
| result.push_back(word); | ||
| } | ||
| return result; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| #include <iostream> | ||
|
|
||
| #include <utils.hpp> | ||
| #include "utils.hpp" | ||
|
|
||
| int main() { | ||
| std::string data; | ||
| std::getline(std::cin, data); | ||
| std::cout << Calculate(data); | ||
| std::cout << Calculate(data)<<std::endl; | ||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,113 @@ | ||
| #include "utils.hpp" | ||
| #include <iostream> | ||
|
|
||
| #include <stack> | ||
| double Calculate(const std::string& data) { | ||
| std::stack<char> operation; | ||
| std::stack<double> values; | ||
| Search(data,operation,values); | ||
| while(operation.size() > 0){ | ||
| if(!Math(operation, values)){ | ||
| return values.top(); | ||
| } | ||
| } | ||
| return values.top(); | ||
| } | ||
|
|
||
| bool Math(std::stack<char>& operation, std::stack<double>& values){ | ||
| double a,b; | ||
| a = values.top(); | ||
| values.pop(); | ||
| switch (operation.top()) { | ||
| case '-': | ||
| b = values.top(); | ||
| values.pop(); | ||
| operation.pop(); | ||
| values.push(b - a); | ||
| break; | ||
| case '+': | ||
| b = values.top(); | ||
| values.pop(); | ||
| operation.pop(); | ||
| values.push(b + a); | ||
| break; | ||
| case '*': | ||
| b = values.top(); | ||
| values.pop(); | ||
| operation.pop(); | ||
| values.push(b * a); | ||
| break; | ||
| case '/': | ||
| b = values.top(); | ||
| if(a==0){ | ||
| std::cerr<<"Can't divide by zero"; | ||
| return false; | ||
| } | ||
| values.pop(); | ||
| operation.pop(); | ||
| values.push(b / a); | ||
| break; | ||
| case '^': | ||
| b = values.top(); | ||
| values.pop(); | ||
| operation.pop(); | ||
| values.push(pow(b,a)); | ||
| break; | ||
| default: | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| void Search(std::string str,std::stack<char>& operation, std::stack<double>& values) { | ||
| std::string word; | ||
| int flag = 1; | ||
|
Contributor
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. и тут тоже bool |
||
| for (auto c : str) { | ||
| if (c == '+' || (c == '-' && flag == 0) || c == '*' || c == '/' || c == '^') { | ||
| if(!operation.empty() && Rang(c)<=Rang(operation.top())){ | ||
| PushValues(values,word); | ||
| Math(operation,values); | ||
| operation.push(c); | ||
| } | ||
| else{ | ||
| operation.push(c); | ||
| } | ||
| } | ||
| if(c == '('){ | ||
| flag = 1; | ||
| operation.push(c); | ||
| continue; | ||
| } | ||
| if(c == ')'){ | ||
| PushValues(values,word); | ||
| while(operation.top() != '('){ | ||
| if(!Math(operation, values)){ | ||
| return; | ||
| } | ||
| } | ||
| operation.pop(); | ||
| continue; | ||
| } | ||
| if (c >= '0' && c <= '9' || c == '.' || c == '-' && flag == 1) { | ||
| word += c; | ||
| flag = 0; | ||
| } | ||
| else{ | ||
| PushValues(values,word); | ||
| } | ||
| } | ||
| PushValues(values,word); | ||
| } | ||
|
|
||
| void PushValues(std::stack<double>& values,std::string& word){ | ||
| if(!word.empty()){ | ||
| values.push(std::stof(word)); | ||
| word = ""; | ||
| } | ||
| } | ||
|
|
||
| int Calculate(const std::string& data) { | ||
| return 0; | ||
| int Rang(char c){ | ||
| if(c == '+' || c == '-')return 1; | ||
| if(c == '*' || c == '/')return 2; | ||
| if(c == '^')return 3; | ||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| #pragma once | ||
|
|
||
| #include <string> | ||
| #include <string_view> | ||
| #include <vector> | ||
|
|
||
| int Calculate(const std::string& data); | ||
| #include <stack> | ||
| #include <cmath> | ||
| double Calculate(const std::string& data); | ||
| void Search(std::string str,std::stack<char>& operation, std::stack<double>& values); | ||
|
Contributor
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. я бы убрал эти объявления функции, и возможно бы в cpp для того что бы показать что они внутренние поместил бы их в неименованный немспейс |
||
| int Rang(char c); | ||
| bool Math(std::stack<char>& operation, std::stack<double>& values); | ||
| void PushValues(std::stack<double>& values,std::string& word); | ||
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 std; да и других пространств имен, в исключении случая когда они используются внутри функции