Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Basic Calculator III in C++
Suppose we have a simple expression string and we have to Implement a basic calculator to evaluate that expression. The expression string may contain opening and closing parentheses, the plus + or minus sign -, non-negative integers and empty spaces. The expression string contains only non-negative integers, +, -, *, / operators, opening and closing parentheses and empty spaces. The integer division should truncate toward zero.
So, if the input is like "6-4 / 2", then the output will be 4
To solve this, we will follow these steps −
l1 := 0, l2 := 1
o1 := 1, o2 := 1
Define one stack st
n := size of s
-
for initialize i := 0, when i < n, update (increase i by 1), do −
x := s[i]
-
if x >= '0' and x <= '9', then −
num := x - '0'
-
while (i + 1 < n and s[i + 1] >= '0' and s[i + 1] <= '9'), do −
(increase i by 1)
num := (num * 10) + (s[i] - '0')
l2 := (if o2 is same as 1, then l2 * num, otherwise l2 / num)
-
otherwise when x is same as '(', then −
insert l1 into st, insert o1 into st
insert l2 into st, insert o2 into st
l1 := 0, o2 := 1
o1 := 1, l2 := 1
-
otherwise when x is same as ')', then −
temp := l1 + o1 * l2
o2 := top element of st
delete element from st
l2 := top element of st
delete element from st
o1 := top element of st
delete element from st
l1 := top element of st
delete element from st
l2 := (if o2 is same as 1, then l2 * temp, otherwise l2 / temp)
-
otherwise when x is same as '*' or x is same as '/', then −
o2 := (if x is same as '*', then 1, otherwise -1)
-
otherwise when x is same as '+' or x is same as '-', then −
-
if x is same as '-' and (i is same as 0 or (i - 1 >= 0 and s[i - 1] is same as '(')), then −
o1 := -1
Ignore following part, skip to the next iteration
l1 := l1 + o1 * l2
o1 := (if x is same as '+', then 1, otherwise -1)
l2 := 1, o2 := 1
-
return l1 + o1 * l2
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
class Solution {
public:
int calculate(string s) {
lli l1 = 0;
lli l2 = 1;
lli o1 = 1;
lli o2 = 1;
stack<lli> st;
lli n = s.size();
for (lli i = 0; i < n; i++) {
char x = s[i];
if (x >= '0' && x <= '9') {
lli num = x - '0';
while (i + 1 < n && s[i + 1] >= '0' && s[i + 1] <= '9') {
i++;
num = (num * 10) + (s[i] - '0');
}
l2 = (o2 == 1) ? l2 * num : l2 / num;
}
else if (x == '(') {
st.push(l1);
st.push(o1);
st.push(l2);
st.push(o2);
l1 = 0;
o2 = 1;
o1 = 1;
l2 = 1;
}
else if (x == ')') {
lli temp = l1 + o1 * l2;
o2 = st.top();
st.pop();
l2 = st.top();
st.pop();
o1 = st.top();
st.pop();
l1 = st.top();
st.pop();
l2 = (o2 == 1) ? l2 * temp : l2 / temp;
}
else if (x == '*' || x == '/') {
o2 = (x == '*') ? 1 : -1;
}
else if (x == '+' || x == '-') {
if (x == '-' && (i == 0 || (i - 1 >= 0 && s[i - 1] == '('))) {
o1 = -1;
continue;
}
l1 += o1 * l2;
o1 = (x == '+') ? 1 : -1;
l2 = 1;
o2 = 1;
}
}
return l1 + o1 * l2;
}
};
main(){
Solution ob;
cout << (ob.calculate("(5+9*3)/8"));
}
Input
"(5+9*3)/8"
Output
4