mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 05:24:07 +00:00
day 14.1: 227-basic-calculator-ii
This commit is contained in:
parent
c738e8abfa
commit
7375af90a8
10
227-basic-calculator-ii/driver.cpp
Normal file
10
227-basic-calculator-ii/driver.cpp
Normal file
@ -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;
|
||||
}
|
||||
103
227-basic-calculator-ii/soln.cpp
Normal file
103
227-basic-calculator-ii/soln.cpp
Normal file
@ -0,0 +1,103 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int Solution::calculate(string s)
|
||||
{
|
||||
unordered_map<char, int> precedences{{'/', 2}, {'*', 2}, {'+', 1}, {'-', 1}};
|
||||
int cur_int = 0;
|
||||
vector<int> values;
|
||||
vector<char> 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;
|
||||
}
|
||||
7
227-basic-calculator-ii/soln.hpp
Normal file
7
227-basic-calculator-ii/soln.hpp
Normal file
@ -0,0 +1,7 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
class Solution {
|
||||
public:
|
||||
int calculate(string s);
|
||||
bool test(string s, int answer);
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user