rendered paste body#include <iostream>
#include <stack>
#include <string>
#include <algorithm>
#include <math.h>
#include <cctype>
using namespace std;
int lower_case ( int c )
{
return tolower ( c );
}
int main()
{
stack <double> RPN;
string buff = NULL;
double N1 = 0;
double N2 = 0;
double answer = 0;
getline(cin, buff, ' ');
transform (buff.begin(),buff.end(), buff.begin(), lower_case); //Convewrt to lower case
while (buff!="q")
{
if (isdigit((int)buff[0]))
{
RPN.push(atof(buff.c_str()));
}
else if (buff == "sqrt")
{
if(!RPN.top()<0)
{
answer = sqrt(RPN.top());
RPN.pop();
RPN.push(answer);
}
else
{
cout<<"<ERROR"<<endl;
}
}
else if (buff == "sum")
{
while( !RPN.empty())
{
answer += RPN.top();
RPN.pop();
}
RPN.push(answer);
}
else
{
switch ((int)buff[0])
{
case '+': //Addition
N1 = RPN.top();
RPN.pop();
N2 = RPN.top();
RPN.pop();
answer = (N2+N1);
break;
case '-': //Subtraction
N1 = RPN.top();
RPN.pop();
N2 = RPN.top();
RPN.pop();
answer = (N2-N1);
break;
case '*': //Multiplication
N1 = RPN.top();
RPN.pop();
N2 = RPN.top();
RPN.pop();
answer = (N2*N1);
break;
case '/': //Division with test for zero in denominator
if(!RPN.top()==0)
{
N1 = RPN.top();
RPN.pop();
N2 = RPN.top();
RPN.pop();
answer = (N2/N1);
}
else
{
cout<<"ERROR"<<endl;
}
break;
case '=': //write top number and toss it
cout<<RPN.top()<<endl;
RPN.pop();
break;
case '.': //write top number and leave it
cout<<RPN.top()<<endl;
break;
default:
cout<<"ERROR"<<endl;
break;
}
}
}
if (buff=="q")
{
return 0;
}
};