C
cront
I have a problem to work on:
we will ask user to input anything and we will put that back onto the
standard output with all set of brackets removed. We will not remove any
single bracket e.g.
INPUT: comp.lang.(c++)
OUTPUT: comp.lang.
INPUT: comp.lang.(c++
OUTPUT: comp.lang.(c++
INPUT: Bjarn)e St)roustr(up)
OUTPUT: Bjarn)e St)roustr
we do not want to remove any single opening or closing bracket. We are
interested in only set of brackets.
MY IDEA:
I am not able to decide which container to use. I thought of using
a Veoctr and only pushing input onto it if I am sure that I will not
find any closing bracket for the opening one. But how will I know I am
inside of set of brackets or I am just reading a single bracket. I was
reading about Stacks and came up with this code but it removes every
parethesis and elements following after a single opening parenthesis,
and since it is a Stack, it reverses all the input:
#include <iostream>
#include <stack>
#include <string>
int main()
{
std::stack<char> strStack;
bool P_ON = false;
char in_char;
while( std::cin >> in_char )
{
if( in_char == '(' )
{
P_ON = true;
}
if ( in_char == ')')
{
P_ON = false;
}
if( P_ON == false && in_char != ')')
{
strStack.push( in_char );
}
else if( P_ON == false && in_char == ')' )
{
strStack.push( ' ' );
strStack.push( '*' );
strStack.push( ' ' );
/* a '*' with space son both side will represent a set of parentheseis
removed */
}
}
/* print the stack with desired elements removed*/
std::cout << "\n------------ STACK after elements removed ---------------\n";
while( strStack.empty() == false )
{
std::cout << strStack.top();
strStack.pop();
}
std::cout << std::endl;
return 0;
}
=========== OUTPUT ============
/home/arnuld/programming/C++ $ ./a.out
comp.lang.(c++)
------------ STACK after elements removed ---------------
* .gnal.pmoc
/home/arnuld/programming/C++ $ ./a.out
comp.lan(g. (c++(using PAN)
------------ STACK after elements removed ---------------
* nal.pmoc
we will ask user to input anything and we will put that back onto the
standard output with all set of brackets removed. We will not remove any
single bracket e.g.
INPUT: comp.lang.(c++)
OUTPUT: comp.lang.
INPUT: comp.lang.(c++
OUTPUT: comp.lang.(c++
INPUT: Bjarn)e St)roustr(up)
OUTPUT: Bjarn)e St)roustr
we do not want to remove any single opening or closing bracket. We are
interested in only set of brackets.
MY IDEA:
I am not able to decide which container to use. I thought of using
a Veoctr and only pushing input onto it if I am sure that I will not
find any closing bracket for the opening one. But how will I know I am
inside of set of brackets or I am just reading a single bracket. I was
reading about Stacks and came up with this code but it removes every
parethesis and elements following after a single opening parenthesis,
and since it is a Stack, it reverses all the input:
#include <iostream>
#include <stack>
#include <string>
int main()
{
std::stack<char> strStack;
bool P_ON = false;
char in_char;
while( std::cin >> in_char )
{
if( in_char == '(' )
{
P_ON = true;
}
if ( in_char == ')')
{
P_ON = false;
}
if( P_ON == false && in_char != ')')
{
strStack.push( in_char );
}
else if( P_ON == false && in_char == ')' )
{
strStack.push( ' ' );
strStack.push( '*' );
strStack.push( ' ' );
/* a '*' with space son both side will represent a set of parentheseis
removed */
}
}
/* print the stack with desired elements removed*/
std::cout << "\n------------ STACK after elements removed ---------------\n";
while( strStack.empty() == false )
{
std::cout << strStack.top();
strStack.pop();
}
std::cout << std::endl;
return 0;
}
=========== OUTPUT ============
/home/arnuld/programming/C++ $ ./a.out
comp.lang.(c++)
------------ STACK after elements removed ---------------
* .gnal.pmoc
/home/arnuld/programming/C++ $ ./a.out
comp.lan(g. (c++(using PAN)
------------ STACK after elements removed ---------------
* nal.pmoc