Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar06 / task02.cpp
diff options
context:
space:
mode:
Commit0ada48f859ab59acdbd67e67e887026cacc29e33 (patch)
Author
 Stoycho Kyosev <stoycho.kk@gmail.com>
2022-11-09 22:56:24 +0200
Committer
 Stoycho Kyosev <stoycho.kk@gmail.com>
2022-11-09 22:56:24 +0200
Files4b4edeee0fc768ddf48073804c4a0f8719e580ed /Seminar06/task02.cpp
Data-structures-and-algorithms-0ada48f859ab59acdbd67e67e887026cacc29e33.tar
Data-structures-and-algorithms-0ada48f859ab59acdbd67e67e887026cacc29e33.tar.gz
Data-structures-and-algorithms-0ada48f859ab59acdbd67e67e887026cacc29e33.zip
Parent16a916c1fc7ecf89bfbf910445740fb4596aae8f (diff)
add Seminar06
1 files changed, 110 insertions, 0 deletions
-rw-r--r--Seminar06/task02.cpp110
diff --git a/Seminar06/task02.cpp b/Seminar06/task02.cpp
new file mode 100644
index 0000000..5f8bf92
--- /dev/null
+++ b/Seminar06/task02.cpp
@@ -0,0 +1,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);
+} \ No newline at end of file