-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPN.cpp
More file actions
38 lines (36 loc) · 816 Bytes
/
Copy pathRPN.cpp
File metadata and controls
38 lines (36 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "RPN.hpp"
static double do_op(double num1, char c, double num2)
{
if (c == '+')
return (num1 + num2);
if (c == '-')
return (num1 - num2);
if (c == '*')
return (num1 * num2);
if (c == '/')
return (num1 / num2);
return(0);
}
void RPN::solve(std::string input)
{
std::stack<double> stack;
double num1, num2;
for (size_t i = 0; i < input.length(); i++)
{
if (input[i] >= '0' && input[i] <= '9')
stack.push(input[i] - '0');
else if ((input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/') && stack.size() > 1)
{
num2 = stack.top();
stack.pop();
num1 = stack.top();
stack.pop();
stack.push(do_op(num1, input[i], num2));
}
else if (std::isspace(input[i]))
continue;
else
throw std::exception();
}
std::cout << stack.top() << std::endl;
}