# Removing Brackets from an Algebraic String Containing + and – Operators using C++

C++ProgrammingServer Side Programming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

Given an algebraic string like p-(q-r)-s, we need to remove the brackets and convert this string to a string with the same mathematical result. So the string p-(q-r)-s is converted to p-q+r-s, giving us the same mathematical result. To achieve this, we can use a stack and keep track of whether or not we should flip the upcoming signs in the bracket expression.

• 0 mean + or no flip

• 1 mean - or flip

So on every bracket opening, we will push either 0 or 1 depending on whether the signs in this bracket will flip or not. We will pop the stack when we see a closing bracket.

Note − We have not considered a string that starts with a bracket. We must sanitize such a type of string. For example s="(p-(q+r))" must be s="p-(q+r)". We can easily run the loop from index 1 to length -2, then 0 to length-1 in that case.

Let us look at some input scenarios

Assume the input given to the method are numbers in string data type, in the resultant list, we obtain the expression as it was given except the brackets −

Input: (2×3)─(6─(4+5))
Result: 2×3–6+4+5


Assume the input given to the method start with the brackets, in the resultant list, we obtain an expression without the brackets as follows −

Input: (a+(b-c-d)-e)
Result: a-b+c+d+e


Note − We have not considered a string that starts with a bracket in the program. We must sanitize such a type of string. For example s="(a+(b-c-d)-e)" must be s="a+(b-c-d)-e". We can easily run the loop from index 1 to length -2, then 0 to length-1 in that case.

Here we can play around with polynomial expressions containing brackets as input, so in the resultant list, we obtain the expression as follows −

Input: (p*p)-(p-q)
Result: p*p-p+q


We applied it on a quadratic polynomial but this method can be used for higher degree polynomial expressions as well.

## Example

Suppose we have three strings S1, S2, and S3 with the following values −

string s1 = "p-(-q+(-r+(s-t)))";
string s2 = "(p+q-r+t+u)";
string s3 = "(p+ (q-r-t)-u)";

So let's use the C++ approach to remove the brackets from the above string containing + and - operators. Here is the C++ program for the given problem −

#include <iostream>
#include <stack>
using namespace std;
string solve(string s) {
stack<int> sk;
sk.push(0);
string res = "";
for(int i=0;i<s.size();i++) {
if(s[i] == '(') {
int temp = s[i-1] == '+' ? 0 : 1;
if(sk.top() == 0) sk.push(temp);
else sk.push(temp^1);
} else if(s[i] == ')') {
sk.pop();
} else if(s[i] == '+' || s[i] == '-') {
char op;
if(sk.top() == 0) op = s[i];
else op = (s[i]=='+' ? '-' : '+');
if(res.size() != 0 && (res[res.size()-1]=='+' || res[res.size()- 1]=='-')) res[res.size()-1] = op;
else res+=op;
} else {
res+=s[i];
}
}
return res;
}
int main() {
string s1 = "p-(-q+(-r+(s-t)))";
string s2 = "(p+q-r+t+u)";
string s3 = "(p+ (q-r-t)-u)";
cout << solve(s1) << endl;
cout << solve(s2) << endl;
cout << solve(s3) << endl;
return 0;
}


## Output

p+q+r-s+t
p-q+r-t-u
p- q-r-t+u


## Conclusion

We use a simple stack to track the sign for each bracket opening. After that, with signs, we transform values one by one. The key was to identify how to keep track of the changing signs with brackets, after which the problem becomes easy.

Updated on 10-Aug-2022 07:47:23