Skip to content

Commit

Permalink
feat(operations): divide user input in different block
Browse files Browse the repository at this point in the history
make it recursively to handle more complex calculs
  • Loading branch information
TheRealPad committed Feb 29, 2024
1 parent 69e290c commit ef21d5f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Calculator/Calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Calculator {

prompt();
while (std::getline(std::cin, input)) {
if (input.empty()) {
if (input.empty() || input == "exit") {
std::cout << "Input terminated." << std::endl;
break;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Interface/IOperations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ namespace Operations {

public:
virtual ~IOperations() = default;

/**
* \brief take a string as a paramater containing only character from 0 to 9 and + - * / %
* \param operation
* \return result of the operation
*/
virtual std::string makeOperation(std::string &operation) = 0;

};
Expand Down
63 changes: 61 additions & 2 deletions src/OperationsPriorities/OperationsPriorities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,74 @@
//

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

namespace Operations {

// modifie valeur du block
// stock block dans la mémoire pour éviter d'aller plus loin si possible
// priorité calcul
std::string OperationsPriorities::createBlock(std::string &block) {
std::vector<std::string> blocks;
std::vector<std::string> operators;
unsigned int nbrParenthesis = 0;
bool inParenthesis = false;

std::cout << block << std::endl;
for (int i = 0; i < block.size() ; ++i) {
if ((block[i] == '+' || block[i] == '-' || block[i] == '*' || block[i] == '/' || block[i] == '%') && !inParenthesis) {
std::string s(1, block[i]);
operators.push_back(s);
if (!block.substr(0, i).empty())
blocks.push_back(block.substr(0, i));
block.erase(0, i + 1);
i = -1;
}
if (block[i] == '(') {
inParenthesis = true;
++nbrParenthesis;
}
if (block[i] == ')' && nbrParenthesis == 1) {
inParenthesis = false;
nbrParenthesis = 0;
if (!block.substr(0, i + 1).empty())
blocks.push_back(block.substr(0, i + 1));
block.erase(0, i + 1);
i = -1;
}
if (block[i] == ')' && nbrParenthesis > 1) {
--nbrParenthesis;
}
}
if (!block.empty())
blocks.push_back(block);
for (auto const &b : blocks) {
if (b[0] == '(') {
std::string pad = b.substr(1, b.size() - 2);
this->createBlock(pad);
}
std::cout << b << std::endl;
}
for (auto const &b : operators) {
std::cout << b << std::endl;
}
return {"1"};
}

/**
* \brief divise en block le calcul avec soit nombre ou parenthese et si parenthese le résultat est un appel recusrsif sur la parenthèse
* \param operation
* \return
*/
std::string OperationsPriorities::makeOperation(std::string &operation) {
std::unique_ptr<Operations::IOperations> infiniteNumber = std::make_unique<Operations::InfiniteNumber>();
std::string str = "-45671+245678";
return infiniteNumber->makeOperation(str);
// 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());
this->createBlock(str);
return infiniteNumber->makeOperation(operation);
}

}
3 changes: 3 additions & 0 deletions src/OperationsPriorities/OperationsPriorities.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ namespace Operations {
OperationsPriorities() = default;
~OperationsPriorities() override = default;
std::string makeOperation(std::string &operation) override;

private:
std::string createBlock(std::string &block);
};

} // Operations
Expand Down

0 comments on commit ef21d5f

Please sign in to comment.