
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- Basic Calculator in C++
- Basic Calculator II in Python
- Basic calculator program using Java
- Basic calculator program using Python
- Basic calculator program using C#
- Basic calculator program using Python program
- Golang Program to create a Class that can perform basic Calculator Operations
- Broken Calculator in Python
- Switch case calculator in JavaScript
- Create a Calculator function in JavaScript
- Prefix calculator using stack in JavaScript
- Program for EMI Calculator in C program
- Program to create grade calculator in Python
- Simple GUI calculator using Tkinter in Python
- Average Speed Calculator using Tkinter
