Print Bracket Number | C ++ implementation to print the bracket number ; function to print the bracket number ; used to print the bracket number for the left bracket ; used to obtain the bracket number for the right bracket ; traverse the given expression ' exp ' ; if current character is a left bracket ; print ' left _ bnum ' , ; push ' left _ bum ' on to the stack ' right _ bnum ' ; increment ' left _ bnum ' by 1 ; else if current character is a right bracket ; print the top element of stack ' right _ bnum ' it will be the right bracket number ; pop the top element from the stack ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printBracketNumber ( string exp , int n ) { int left_bnum = 1 ; stack < int > right_bnum ; for ( int i = 0 ; i < n ; i ++ ) { if ( exp [ i ] == ' ( ' ) { cout << left_bnum << " ▁ " ; right_bnum . push ( left_bnum ) ; left_bnum ++ ; } else if ( exp [ i ] == ' ) ' ) { cout << right_bnum . top ( ) << " ▁ " ; right_bnum . pop ( ) ; } } } int main ( ) { string exp = " ( a + ( b * c ) ) + ( d / e ) " ; int n = exp . size ( ) ; printBracketNumber ( exp , n ) ; return 0 ; }