Skip to content

Commit

Permalink
fix(unit test): add missing dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealPad committed Mar 4, 2024
1 parent b524d77 commit 8defad1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
51 changes: 26 additions & 25 deletions src/OperationsPriorities/OperationsPriorities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,35 @@

#include <memory>
#include <vector>
#include <algorithm>
#include "OperationsPriorities.h"
#include "Error.h"
#include "InfiniteNumber.h"

namespace Operations {

std::string OperationsPriorities::operatorPriorities(std::vector<std::string> &blocks, std::vector<std::string> &operators) {
std::unique_ptr<IOperations> infiniteNumber = std::make_unique<InfiniteNumber>();
std::string finalResult;

for (auto c : {"*", "/", "%", "+", "-"}) {
if (std::count(operators.begin(), operators.end(), c)) {
for (unsigned int i = 0; i < operators.size(); ++i) {
std::string str = blocks[i] + operators[i] + blocks[i + 1];
if (operators[i] == c) {
const std::string result = infiniteNumber->makeOperation(str);
blocks[i] = result;
blocks[i + 1] = result;
finalResult = result;
}
}
}
}
return finalResult;
}

// gérer nombre négatif
std::string OperationsPriorities::createBlock(std::string &block) {
std::unique_ptr<IOperations> infiniteNumber = std::make_unique<InfiniteNumber>();
std::vector<std::string> blocks;
std::vector<std::string> operators;
unsigned int nbrParenthesis = 0;
Expand Down Expand Up @@ -49,36 +69,17 @@ namespace Operations {
throw ErrorCalculator::Error(ErrorCalculator::Error::TYPO_USER_INPUT);
for (auto &b : blocks) {
if (b[0] == '(') {
std::string pad = b.substr(1, b.size() - 2);
if (!this->_previousResults.count(pad))
this->_previousResults[pad] = this->createBlock(pad);
b = this->_previousResults[pad];
}
}
std::string lastResult;
for (auto c : {"*", "/", "%", "+", "-"}) {
if (std::count(operators.begin(), operators.end(), c)) {
for (unsigned int i = 0; i < operators.size(); ++i) {
std::string str = blocks[i] + operators[i] + blocks[i + 1];
if (operators[i] == c) {
const std::string result = infiniteNumber->makeOperation(str);
blocks[i] = result;
blocks[i + 1] = result;
lastResult = result;
}
}
std::string tmp = b.substr(1, b.size() - 2);
if (!this->_previousResults.count(tmp))
this->_previousResults[tmp] = this->createBlock(tmp);
b = this->_previousResults[tmp];
}
}
return lastResult;
return this->operatorPriorities(blocks, operators);
}

std::string OperationsPriorities::makeOperation(std::string &operation) {
// std::unique_ptr<Operations::IOperations> infiniteNumber = std::make_unique<Operations::InfiniteNumber>();
// std::string str = "1 + (1 + 1)";
// std::string str = "((1+2)-4)*(5/(2+3))+3";
// str.erase(remove_if(str.begin(), str.end(), isspace), str.end());
operation.erase(remove_if(operation.begin(), operation.end(), isspace), operation.end());
// std::cout << "coucou " << this->createBlock(operation) << std::endl;
return this->createBlock(operation);
}

Expand Down
1 change: 1 addition & 0 deletions src/OperationsPriorities/OperationsPriorities.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Operations {

private:
std::string createBlock(std::string &block);
std::string operatorPriorities(std::vector<std::string> &blocks, std::vector<std::string> &operators);

std::map<std::string, std::string> _previousResults;
};
Expand Down
2 changes: 1 addition & 1 deletion tests/OperationsPriorities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,4 @@ TEST(ProxyOperationsPriorities, OperatorPriorityThree)
const std::string result = operations->makeOperation(str);

EXPECT_EQ(result, "2");
}
}

0 comments on commit 8defad1

Please sign in to comment.