Skip to content

Commit

Permalink
fix(negative number): can handle negative numbers
Browse files Browse the repository at this point in the history
add new unit tests
  • Loading branch information
TheRealPad committed Mar 4, 2024
1 parent 7a5b1f4 commit 0aafc8c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/OperationsPriorities/OperationsPriorities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ 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;
std::string finalResult = blocks[0];

if (finalResult[0] == this->_negativeSymbol)
finalResult[0] = '-';
for (auto c : {"*", "/", "%", "+", "-"}) {
if (std::count(operators.begin(), operators.end(), c)) {
for (unsigned int i = 0; i < operators.size(); ++i) {
if (blocks[i][0] == this->_negativeSymbol)
blocks[i][0] = '-';
if (blocks[i + 1][0] == this->_negativeSymbol)
blocks[i + 1][0] = '-';
std::string str = blocks[i] + operators[i] + blocks[i + 1];
if (operators[i] == c) {
const std::string result = infiniteNumber->makeOperation(str);
Expand All @@ -30,7 +36,6 @@ namespace Operations {
return finalResult;
}

// gérer nombre négatif
std::string OperationsPriorities::createBlock(std::string &block) {
std::vector<std::string> blocks;
std::vector<std::string> operators;
Expand Down Expand Up @@ -66,6 +71,7 @@ namespace Operations {
blocks.push_back(block);
if (operators.size() != blocks.size() - 1)
throw ErrorCalculator::Error(ErrorCalculator::Error::TYPO_USER_INPUT);
// mettre dans la première boucle
for (auto &b : blocks) {
if (b[0] == '(') {
std::string tmp = b.substr(1, b.size() - 2);
Expand All @@ -78,8 +84,11 @@ namespace Operations {
}

std::string OperationsPriorities::makeOperation(std::string &operation) {
operation.erase(remove_if(operation.begin(), operation.end(), isspace), operation.end());
return this->createBlock(operation);
operation.erase(remove_if(operation.begin(), operation.end(), isspace), operation.end());
for (unsigned int i = 0; i < operation.size(); ++i)
if (operation[i] == '-' && (i == 0 || operation[i - 1] == '+' || operation[i - 1] == '*' || operation[i - 1] == '/' || operation[i - 1] == '%' || operation[i - 1] == '-'))
operation[i] = this->_negativeSymbol;
return this->createBlock(operation);
}

}
1 change: 1 addition & 0 deletions src/OperationsPriorities/OperationsPriorities.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Operations {
std::string operatorPriorities(std::vector<std::string> &blocks, std::vector<std::string> &operators);

std::map<std::string, std::string> _previousResults;
const char _negativeSymbol = 'a';
};

} // Operations
Expand Down
27 changes: 27 additions & 0 deletions tests/OperationsPriorities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,30 @@ TEST(ProxyOperationsPriorities, OperatorPriorityThree)

EXPECT_EQ(result, "2");
}

TEST(ProxyOperationsPriorities, NegativeNumbersOne)
{
std::unique_ptr<Operations::IOperations> operations = std::make_unique<Operations::ProxyOperationsPriorities>();
std::string str = "-1+1";
const std::string result = operations->makeOperation(str);

EXPECT_EQ(result, "0");
}

TEST(ProxyOperationsPriorities, NegativeNumbersTwo)
{
std::unique_ptr<Operations::IOperations> operations = std::make_unique<Operations::ProxyOperationsPriorities>();
std::string str = "1+-1";
const std::string result = operations->makeOperation(str);

EXPECT_EQ(result, "0");
}

TEST(ProxyOperationsPriorities, NegativeNumbersThree)
{
std::unique_ptr<Operations::IOperations> operations = std::make_unique<Operations::ProxyOperationsPriorities>();
std::string str = "1--1";
const std::string result = operations->makeOperation(str);

EXPECT_EQ(result, "2");
}

0 comments on commit 0aafc8c

Please sign in to comment.