-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
141 lines (133 loc) · 4.01 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "BigInteger.h"
#include "Token.h"
#include <sstream>
#include <list>
#include <stack>
auto InfixToPostfix(std::string const & expression) noexcept -> std::list<Token>;
auto EvaluateInfix(std::list<Token> const & queue) noexcept -> BigInteger;
int main()
{
std::string postfix;
getline(std::cin, postfix);
std::cout << EvaluateInfix(InfixToPostfix(postfix)) << '\n';
return 0;
}
auto InfixToPostfix(std::string const & expression) noexcept -> std::list<Token>
{
std::istringstream exp(expression);
std::list<Token> queue;
std::stack<Operator> ops;
std::string token;
Operator an_operator {};
// For every element in the expression
while (exp >> token)
{
// Verify if the token is an operand
if ((token.size() > 1) || (token.size() == 1 && (isdigit(token.at(0)) != 0)))
{
if (token.at(0) == '-')
{
token.erase(0, 1);
queue.push_back({true, {BigInteger::Sign::MINUS, static_cast<uint16_t>(token.size()), token.c_str()},
false,});
continue;
}
if (token.at(0) == '+')
{
token.erase(0, 1);
queue.push_back({true, {BigInteger::Sign::PLUS, static_cast<uint16_t>(token.size()), token.c_str()},
false,});
continue;
}
queue.push_back({true, {BigInteger::Sign::PLUS, static_cast<uint16_t>(token.size()), token.c_str()},
false,});
continue;
}
// The token is an operator or a brace
an_operator.sym = token.at(0);
if (token.at(0) == '-' || token.at(0) == '+')
{
an_operator.pre = Precedence::ORD_1;
}
else if (token.at(0) == '*' || token.at(0) == '/')
{
an_operator.pre = Precedence::ORD_2;
}
else
{
an_operator.pre = Precedence::PAR;
}
if (an_operator.pre != Precedence::PAR)
{
while (!ops.empty() && ops.top().pre >= an_operator.pre && ops.top().sym != '(')
{
queue.push_back({false, {}, true, {ops.top().sym, ops.top().pre}});
ops.pop();
}
ops.push(an_operator);
continue;
}
if (an_operator.sym == '(')
{
ops.push(an_operator);
continue;
}
if (an_operator.sym == ')')
{
while (!ops.empty() && ops.top().sym != '(')
{
queue.push_back({false, {}, true, {ops.top().sym, ops.top().pre}});
ops.pop();
}
if (!ops.empty() && ops.top().sym == '(')
{
ops.pop();
}
continue;
}
break;
}
while (!ops.empty())
{
queue.push_back({false, {}, true, {ops.top().sym, ops.top().pre}});
ops.pop();
}
return queue;
}
auto EvaluateInfix(std::list<Token> const & queue) noexcept -> BigInteger
{
std::stack<Token> tokens;
BigInteger first;
BigInteger second;
for (const auto & token: queue)
{
if (token.is_operator)
{
first = tokens.top().operand;
tokens.pop();
second = tokens.top().operand;
tokens.pop();
if (token.opt.sym == '+')
{
tokens.push({true, second + first, false, {}});
}
else if (token.opt.sym == '-')
{
tokens.push({true, second - first, false, {}});
}
else if (token.opt.sym == '*')
{
tokens.push({true, second * first, false, {}});
}
else if (token.opt.sym == '/')
{
tokens.push({true, second / first, false, {}});
}
}
else if (token.is_operand)
{
tokens.push(token);
}
}
return tokens.top().operand;
}