C++ Primer ex 9.34, using a Stack

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++ $
 
G

Guest

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.

The easiest way to note that an opening parenthesis was seen is to push
is onto the stack. Then when you read in a closing parenthesis you start
poping until you find an opening one.

The below code will replace all parenthesised expressions with 5 (even
nested parentheses (I wrote it really quick, so it might contain bugs).

#include <iostream>
#include <stack>

int main()
{
std::stack<char> stack;

char c;
while ( std::cin >> c )
{
if ( c != ')' )
stack.push(c);

else
{
char p;
do {
p = stack.top();
stack.pop();
} while ( p != '(' );

stack.push('5');
}
}

while ( !stack.empty() )
{
std::cout << stack.top() << "\n";
stack.pop();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C++ Primer ex 9.27 4
C++ Primer ex 6.20 36
C++ Primer ex 7.6 17
removing brackets from input 13
C++ Primer ex 7.16 - arguments to main 15
C++ Primer ex 3.24 (bitset class) 5
C++ Primer ex 7.12 2
C++ Primer exercise 3.13 17

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top