-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse_polish_notation.cpp
61 lines (54 loc) · 1.18 KB
/
reverse_polish_notation.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Author: Jakub Pawlina
// Algorithm: Reverse Polish Notation (computing)
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
int32_t main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
std::string input;
std::stack <long double> numbers;
while (std::cin >> input && input != "=")
{
if (input == "+")
{
long double number1 = numbers.top();
numbers.pop();
long double number2 = numbers.top();
numbers.pop();
numbers.push(number1 + number2);
}
else if (input == "-")
{
long double number1 = numbers.top();
numbers.pop();
long double number2 = numbers.top();
numbers.pop();
numbers.push(number2 - number1);
}
else if (input == "*")
{
long double number1 = numbers.top();
numbers.pop();
long double number2 = numbers.top();
numbers.pop();
numbers.push(number1 * number2);
}
else if (input == "/")
{
long double number1 = numbers.top();
numbers.pop();
long double number2 = numbers.top();
numbers.pop();
numbers.push(number2 / number1);
}
else
{
long double number = std::stold(input);
numbers.push(number);
}
}
std::cout << numbers.top() << '\n';
return 0;
}