diff --git a/cpp09/ex01/RPN.cpp b/cpp09/ex01/RPN.cpp index a33286a..466aea0 100644 --- a/cpp09/ex01/RPN.cpp +++ b/cpp09/ex01/RPN.cpp @@ -1,73 +1,77 @@ #include "RPN.hpp" -bool RPN::parseInput( std::string const &input ) +bool RPN::parseInput(std::string const &input) { - std::stringstream ss(input); - std::string token; + std::stringstream ss(input); + std::string token; + bool firstToken = false; - while ( ss >> token ) + while (ss >> token) { - if ( token.length() != 1 ) - return ( false ); - if ( !isdigit(token[0]) && token != "+" && token != "-" && token != "*" && token != "/" ) - return ( false ); + firstToken = true; + if (token.length() != 1) + return (false); + if (!isdigit(token[0]) && token != "+" && token != "-" && token != "*" && token != "/") + return (false); } - return ( true ); + return (firstToken); } -int RPN::convertInt( std::string const &digitStr ) +int RPN::convertInt(std::string const &digitStr) { - std::stringstream ss(digitStr); - int value; + std::stringstream ss(digitStr); + int value; ss >> value; if (ss.fail()) - throw ( RPNInputException() ); - return ( value ); + throw(RPNInputException()); + return (value); } -int RPN::calcOperation( int val1, int val2, std::string const &symbol ) +int RPN::calcOperation(int val1, int val2, std::string const &symbol) { - if ( symbol == "+" ) - return ( val1 + val2 ); - if ( symbol == "-" ) - return ( val1 - val2 ); - if ( symbol == "*" ) - return ( val1 * val2 ); - if ( symbol == "/" ) + if (symbol == "+") + return (val1 + val2); + if (symbol == "-") + return (val1 - val2); + if (symbol == "*") + return (val1 * val2); + if (symbol == "/") { - if ( val2 == 0 ) - throw ( RPNInputException() ); - return ( val1 / val2 ); + if (val2 == 0) + throw(RPNInputException()); + return (val1 / val2); } - throw ( RPNInputException() ); + throw(RPNInputException()); } -char const *RPN::RPNInputException::what( void ) const throw() +char const *RPN::RPNInputException::what(void) const throw() { - return ( "Error" ); + return ("Error"); } -void RPN::compute( std::string const &input ) +void RPN::compute(std::string const &input) { - std::stack buffer; - std::stringstream ss(input); - std::string token; + std::stack buffer; + std::stringstream ss(input); + std::string token; - if ( !parseInput( input ) ) - throw ( RPNInputException() ); - while ( ss >> token ) + if (!parseInput(input)) + throw(RPNInputException()); + while (ss >> token) { - if ( isdigit(token[0]) ) + if (isdigit(token[0])) { - buffer.push( convertInt( token ) ); - continue ; + buffer.push(convertInt(token)); + continue; } - if ( buffer.empty() ) - throw ( RPNInputException() ); - int val1 = buffer.top(); buffer.pop(); - int val2 = buffer.top(); buffer.pop(); - buffer.push( calcOperation( val2, val1, token ) ); + if (buffer.empty()) + throw(RPNInputException()); + int val1 = buffer.top(); + buffer.pop(); + int val2 = buffer.top(); + buffer.pop(); + buffer.push(calcOperation(val2, val1, token)); } std::cout << buffer.top() << std::endl; } diff --git a/cpp09/ex01/main.cpp b/cpp09/ex01/main.cpp index a3d3518..8df0ef3 100644 --- a/cpp09/ex01/main.cpp +++ b/cpp09/ex01/main.cpp @@ -1,19 +1,19 @@ #include "RPN.hpp" -int main( int argc, char **argv ) +int main(int argc, char **argv) { - if ( argc != 2 ) + if (argc != 2) { std::cout << "No input." << std::endl; - return ( 0 ); + return (0); } try { - RPN::compute( argv[1] ); + RPN::compute(argv[1]); } - catch ( RPN::RPNInputException const &e ) + catch (RPN::RPNInputException const &e) { std::cout << e.what() << std::endl; } - return ( 0 ); + return (0); } \ No newline at end of file