C
cront
All I was able to come up with is this code. I am get confused on how to
solve this problem provided a very small amount of Stack operations.
/* C++ Primer - 4/e
*
* Exercise 9.42
* STATEMENT
* Use a stack to process parenthesized expressions. When you see an
open parenthesis, note that it was seen. When you see a close
parenthesis after an open parenthesis, pop elements down to and
including the open parenthesis off the stack. push a value onto the
stack to indicate that a parenthesized expression was replaced.
*
*/
#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 )
{
strStack.push( in_char );
}
}
/* 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 ===============
~/programming/C++ $ g++ -ansi -pedantic -Wall -Wextra ex_09.43.cpp
~/programming/C++ $ ./a.out
comp.(lang).c++
------------ STACK after elements removed ---------------
++c.).pmoc
~/programming/C++ $
solve this problem provided a very small amount of Stack operations.
/* C++ Primer - 4/e
*
* Exercise 9.42
* STATEMENT
* Use a stack to process parenthesized expressions. When you see an
open parenthesis, note that it was seen. When you see a close
parenthesis after an open parenthesis, pop elements down to and
including the open parenthesis off the stack. push a value onto the
stack to indicate that a parenthesized expression was replaced.
*
*/
#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 )
{
strStack.push( in_char );
}
}
/* 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 ===============
~/programming/C++ $ g++ -ansi -pedantic -Wall -Wextra ex_09.43.cpp
~/programming/C++ $ ./a.out
comp.(lang).c++
------------ STACK after elements removed ---------------
++c.).pmoc
~/programming/C++ $