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
90 changes: 47 additions & 43 deletions cpp09/ex01/RPN.cpp
Original file line number Diff line number Diff line change
@@ -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<int> buffer;
std::stringstream ss(input);
std::string token;
std::stack<int> 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;
}
12 changes: 6 additions & 6 deletions cpp09/ex01/main.cpp
Original file line number Diff line number Diff line change
@@ -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);
}