C++ Primer ex 9.34, using a Stack

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cront

    #1

    C++ Primer ex 9.34, using a Stack

    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++ $
  • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

    #2
    Re: C++ Primer ex 9.34, using a Stack

    On 2007-10-19 08:35, cront wrote:
    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();
    }
    }

    --
    Erik Wikström

    Comment

    Working...