From 0ada48f859ab59acdbd67e67e887026cacc29e33 Mon Sep 17 00:00:00 2001 From: Stoycho Kyosev Date: Wed, 9 Nov 2022 22:56:24 +0200 Subject: add Seminar06 --- Seminar06/task02.cpp | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 Seminar06/task02.cpp (limited to 'Seminar06/task02.cpp') 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 +#include +#include +#include + +bool isSymethric(const std::list& 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 q) { + std::stack 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>& ll) { + std::list l; + + for(auto elem : ll) + l.push_back(evaluateQueue(elem)); + + return isSymethric(l); +} + +int main() { + std::list> 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 -- cgit v1.3.1-23-gd82d