From 7eee5a78005a9a569186d2c8b4b645a818ab9bf1 Mon Sep 17 00:00:00 2001 From: Stoycho Kyosev Date: Sun, 6 Oct 2024 07:04:44 -0400 Subject: Cleanup --- Seminar02/task03.cpp | 59 ---------------------------------------------------- 1 file changed, 59 deletions(-) delete mode 100644 Seminar02/task03.cpp (limited to 'Seminar02/task03.cpp') diff --git a/Seminar02/task03.cpp b/Seminar02/task03.cpp deleted file mode 100644 index 1c7a095..0000000 --- a/Seminar02/task03.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include -#include - -bool isOperation(char c) { - return (c == '+') || (c == '-') || (c == '*') || (c == '/'); -} - -bool isDigit(char c) { - return c >= '0' && c <= '9'; -} - -int eval(int fst, int snd, char op) { - assert(isOperation(op)); - - switch (op) { - case '+': - return fst + snd; - case '-': - return fst - snd; - case '*': - return fst * snd; - case '/': - return fst / snd; - } -} - -int RPN(const std::string& str) { - std::stack numbers; - - for (size_t i = 0; i < str.size(); i++) { - if(isOperation(str[i])) { - int snd = numbers.top(); - numbers.pop(); - - int fst = numbers.top(); - numbers.pop(); - - numbers.push(eval(fst, snd, str[i])); - } - else if(str[i] != ' ') { - int number = 0; - while(i < str.size() && isDigit(str[i])) { - number *= 10; - number += str[i] - '0'; - i++; - } - i--; - numbers.push(number); - } - } - - return numbers.top(); -} - -int main() { - std::cout << RPN("9 3 4 * - 2 5 * -"); -} \ No newline at end of file -- cgit v1.3.1-23-gd82d