From 7375af90a8d7f0a316615f5a87e99b8b5327ce64 Mon Sep 17 00:00:00 2001 From: Kaushik Narayan R Date: Thu, 12 Oct 2023 23:48:51 -0700 Subject: [PATCH] day 14.1: 227-basic-calculator-ii --- 227-basic-calculator-ii/driver.cpp | 10 +++ 227-basic-calculator-ii/soln.cpp | 103 +++++++++++++++++++++++++++++ 227-basic-calculator-ii/soln.hpp | 7 ++ 3 files changed, 120 insertions(+) create mode 100644 227-basic-calculator-ii/driver.cpp create mode 100644 227-basic-calculator-ii/soln.cpp create mode 100644 227-basic-calculator-ii/soln.hpp diff --git a/227-basic-calculator-ii/driver.cpp b/227-basic-calculator-ii/driver.cpp new file mode 100644 index 0000000..196e45b --- /dev/null +++ b/227-basic-calculator-ii/driver.cpp @@ -0,0 +1,10 @@ +#include "soln.hpp" + +int main() +{ + string s = "4+1-3*10/2*5"; + int answer = -70; + Solution soln; + cout << "Evaluated expression correctly? " << soln.test(s, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/227-basic-calculator-ii/soln.cpp b/227-basic-calculator-ii/soln.cpp new file mode 100644 index 0000000..0ce9a7d --- /dev/null +++ b/227-basic-calculator-ii/soln.cpp @@ -0,0 +1,103 @@ +#include "soln.hpp" + +int Solution::calculate(string s) +{ + unordered_map precedences{{'/', 2}, {'*', 2}, {'+', 1}, {'-', 1}}; + int cur_int = 0; + vector values; + vector ops; + for (int i = 0; i < s.length(); i++) + { + char c = s[i]; + if (c == ' ') + continue; + // digit, get the number (only positive nums) + if (isdigit(c)) + { + cur_int = 0; + while (i < s.size() && isdigit(c)) + { + cur_int *= 10; + cur_int += c - '0'; + i++; + if (i == s.length()) + break; + c = s[i]; + } + i--; + values.push_back(cur_int); + cur_int = 0; + } + // operator + else + { + // if operator stack not empty + while (ops.empty() != true) + { + // new one is equal or lower, pop and perform old op first + if (precedences.at(ops.back()) >= precedences.at(c)) + { + char op = ops.back(); + ops.pop_back(); + int val2 = values.back(); + values.pop_back(); + int val1 = values.back(); + values.pop_back(); + int result; + switch (op) + { + case '+': + result = val1 + val2; + break; + case '-': + result = val1 - val2; + break; + case '*': + result = val1 * val2; + break; + case '/': + result = val1 / val2; + break; + } + values.push_back(result); + } + else + { + break; + } + } + ops.push_back(c); + } + } + while (ops.empty() != true) + { + char op = ops.back(); + ops.pop_back(); + int val2 = values.back(); + values.pop_back(); + int val1 = values.back(); + values.pop_back(); + int result; + switch (op) + { + case '+': + result = val1 + val2; + break; + case '-': + result = val1 - val2; + break; + case '*': + result = val1 * val2; + break; + case '/': + result = val1 / val2; + break; + } + values.push_back(result); + } + return values[0]; +} + +bool Solution::test(string s, int answer) { + return calculate(s) == answer; +} \ No newline at end of file diff --git a/227-basic-calculator-ii/soln.hpp b/227-basic-calculator-ii/soln.hpp new file mode 100644 index 0000000..44ea978 --- /dev/null +++ b/227-basic-calculator-ii/soln.hpp @@ -0,0 +1,7 @@ +#include +using namespace std; +class Solution { + public: + int calculate(string s); + bool test(string s, int answer); +}; \ No newline at end of file