Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added homework_01/task_01/src/main
Binary file not shown.
4 changes: 2 additions & 2 deletions homework_01/task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <iostream>

#include <utils.hpp>
#include "utils.hpp"

int main() {
for (const auto& word : SplitString("asdas das das fgag (adasd 1fas)")) {
for (const auto& word : SplitString("a (a a) b (asd as) ")) {
std::cout << word << "\n";
}
return 0;
Expand Down
2 changes: 2 additions & 0 deletions homework_01/task_01/src/split_string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ TEST_CASE("SplitString", "[simple]") {
CHECK(SplitString("a\na\ta a") == std::vector<std::string>{"a\na", "a", "a"});
CHECK(SplitString("a (a a)") == std::vector<std::string>{"a", "(a a)"});
CHECK(SplitString("a (a a) b (asd as) ") == std::vector<std::string>{"a", "(a a)", "b", "(asd as)"});
CHECK(SplitString("a\ta ") == std::vector<std::string>{"a", "a"});
CHECK(SplitString("a\ta") == std::vector<std::string>{"a", "a"});
}
39 changes: 37 additions & 2 deletions homework_01/task_01/src/utils.cpp
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

считается плохим тоном использовать using namespace std; да и других пространств имен, в исключении случая когда они используются внутри функции



std::vector<string> SplitString(const std::string& data) {
vector<string> result;
std::string word;
int flag = 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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;
}
40 changes: 39 additions & 1 deletion homework_01/task_02/src/calculate_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <catch2/catch_test_macros.hpp>
#include "catch2/catch_test_macros.hpp"
#include <catch2/catch_approx.hpp>


#include "utils.hpp"

Expand All @@ -22,3 +24,39 @@ TEST_CASE("Calculate", "[simple_multiply]") {
CHECK(Calculate("1*0") == 0);
CHECK(Calculate("0*0") == 0);
}

TEST_CASE("Calculate", "[simple_division]") {
CHECK(Calculate("10/2") == 5);
CHECK(Calculate("4/2") == 2);
CHECK(Calculate("0/1") == 0);
}

TEST_CASE("Calculate", "[negative_numbers]") {
CHECK(Calculate("-3-2") == -5);
CHECK(Calculate("-6/2") == -3);
CHECK(Calculate("-8+5") == -3);
CHECK(Calculate("-6*2") == -12);
}

TEST_CASE("Calculate", "[simple_exponentiation]") {
CHECK(Calculate("3^2") == 9);
CHECK(Calculate("-2.5^2") == Catch::Approx(6.25));
CHECK(Calculate("-3.5^0") == 1);
CHECK(Calculate("0^2") == Catch::Approx(0));
}


TEST_CASE("Calculate", "[fractional numbers]") {
CHECK(Calculate("6.2*10") == Catch::Approx(62));
CHECK(Calculate("-1.5+2.5") == Catch::Approx(1));
CHECK(Calculate("-3.5/3.5") == Catch::Approx(-1));
CHECK(Calculate("-3.5-3.5") == Catch::Approx(-7));
}

TEST_CASE("Calculate", "[advanced_expressions]") {
CHECK(Calculate("6+10/2*2/2-1") == 10);
CHECK(Calculate("(2+3)+5") == 10);
CHECK(Calculate("(-2)+(-2)*(-2)^2") == -10);
CHECK(Calculate("(3/10)+(2.5*2)") == Catch::Approx(5.3));
CHECK(Calculate("3+((-7)-5/(2+3))*(7-2.5)") == -33);
}
Binary file added homework_01/task_02/src/main
Binary file not shown.
4 changes: 2 additions & 2 deletions homework_01/task_02/src/main.cpp
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;
}
112 changes: 109 additions & 3 deletions homework_01/task_02/src/utils.cpp
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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;
}
10 changes: 7 additions & 3 deletions homework_01/task_02/src/utils.hpp
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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);