blob: 5f8bf92bae7e964843777589f83102ba9dfc3984 (
plain) (
blame)
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
|
#include<list>
#include<iostream>
#include<queue>
#include<stack>
bool isSymethric(const std::list<int>& ll) {
auto start = ll.begin();
auto end = --(ll.end());
while(start != end) {
if(*start != *end)
return false;
++start;
if(start == end)
break;
--end;
}
return true;
}
int evaluateExpr(char op, int fst, int snd) {
// assert(isOperator(op));
switch (op) {
case '+':
return (fst + snd);
break;
case '-':
return (fst - snd);
break;
case '*':
return (fst * snd);
break;
case '/':
return (snd / fst);
break;
}
}
size_t evaluateQueue(std::queue<char> q) {
std::stack<int> nums;
while(!q.empty()) {
if(isdigit(q.front())) {
int cDigit = q.front() - '0';
q.pop();
nums.push(cDigit);
}
else {
int fst = nums.top();
nums.pop();
int snd = nums.top();
nums.pop();
char op = q.front();
q.pop();
nums.push(evaluateExpr(op, fst, snd));
}
}
return nums.top();
}
bool solution(const std::list<std::queue<char>>& ll) {
std::list<int> l;
for(auto elem : ll)
l.push_back(evaluateQueue(elem));
return isSymethric(l);
}
int main() {
std::list<std::queue<char>> test(5);
auto it = test.begin();
(*it).push('4');
(*it).push('5');
(*it).push('+');
++it;
(*it).push('1');
(*it).push('6');
(*it).push('+');
(*it).push('7');
(*it).push('*');
++it;
(*it).push('8');
++it;
(*it).push('1');
(*it).push('7');
(*it).push('7');
(*it).push('*');
(*it).push('*');
++it;
(*it).push('2');
(*it).push('3');
(*it).push('*');
(*it).push('3');
(*it).push('+');
std::cout << solution(test);
}
|