Queue giving erroneous data
<p>So I'm attempting to use a queue to parse some input, turning prefix mathematical expressions into infix mathematical expressions with parentheses. For example: +++12 20 3 4 turns into (((12+20)+3)+4). For the most part, my algorithm works, except for one specific thing. When the numbers are greater than 2 digits long, the output becomes strange. I'll give you some examples instead of attempting to explain.</p>

<pre><code>Examples: +++12 200 3 4 becomes (((12+3)+3)+4)
+++12 2000 3 4 becomes (((12+20004)+3)+4)
+++12 20005 3 4 becomes (((12+20004)+3)+4)
+++12 20005 3 45 becomes (((12+20004)+3)+45)
+++12 20005 3 456 becomes (((12+20004)+3)+()
</code></pre>

<p>Hopefully that's enough examples, if you need more, just ask.</p>

<p>I'm using GCC 4.2 in XCode on Mac OSX 10.6.2.</p>

<p>And here is the code that does this wonderful thing:</p>

<pre><code>#include "EParse.h"
#include &lt;iostream&gt;
#include &lt;iomanip&gt;


EParse::EParse( char* s )
{
    this-&gt;s = s;
    len = strlen( s );
}

void EParse::showParsed()
{
    parse( s, 0, len, new std::queue&lt; char* &gt;(), new std::queue&lt; char &gt;() );
}

void EParse::parse( char* str, int beg, int len, std::queue&lt; char* &gt; *n, std::queue&lt; char &gt; *ex )
{
    //ex is for mathematical expressions (+, -, etc.), n is for numbers
    if( beg == len )
    {
        if( ex-&gt;size() &gt; n-&gt;size() )
        {
            std::cout &lt;&lt; "Malformed expression. Too many mathematical expressions to too few numbers." &lt;&lt; std::endl;
            std::cout &lt;&lt; ex-&gt;size() &lt;&lt; " mathematical expressions." &lt;&lt; std::endl;
            std::cout &lt;&lt; n-&gt;size() &lt;&lt; " number(s)." &lt;&lt; std::endl;
            return;
        }
        else
        {
            std::string *s = new std::string();
            output( n, ex, 0, s );
            std::cout &lt;&lt; s-&gt;c_str();
            return;
        }
    }

    if( str[ beg ] == ' ' &amp;&amp; beg != ( len - 1 ) )
        beg++;
    if( num( str[ beg ] ) )
    {
        std::string *s = new std::string();
        getNum( s, str, beg, len );
        //std::cout &lt;&lt; s-&gt;c_str() &lt;&lt; std::endl;
        n-&gt;push( const_cast&lt; char* &gt;( s-&gt;c_str() ) );
        delete s;
        parse( str, beg, len, n, ex );
    }
    else if( mathexp( str[ beg ] ) )
    {
        ex-&gt;push( str[ beg ] );
        parse( str, beg + 1, len, n, ex );
    }
}

void EParse::getNum( std::string *s, char* str, int &amp;beg, int len )
{
    if( num( str[ beg ] ) )
    {
        char *t = new char[ 1 ];
        t[ 0 ] = str[ beg ];
        s-&gt;append( t );
        beg += 1;
        getNum( s, str, beg, len );
    }
}

bool EParse::num( char c )
{
    return c == '0' || c == '1' || c == '2' || c == '3' || c == '4' ||
    c == '5' || c == '6' || c == '7' || c == '8' || c == '9';
}

bool EParse::mathexp( char c )
{
    return c == '+' || c == '*' || c == '/' || c == '%' || c == '-';
}

void EParse::output( std::queue&lt; char* &gt; *n, std::queue&lt; char &gt; *ex, int beg, std::string *str )
{
    if( ex-&gt;empty() )
    {
        return;
    }

    char *t = new char[1];
    t[ 0 ] = ex-&gt;front();
    ex-&gt;pop();
    if( beg == 0 )
    {
        str-&gt;insert( 0, "(" );
        str-&gt;append( n-&gt;front() );
        beg += 1 + strlen( n-&gt;front() );
        n-&gt;pop();
        str-&gt;append( t );
        str-&gt;append( n-&gt;front() );
        str-&gt;append( ")" );
        beg += 2 + strlen( n-&gt;front() );
        n-&gt;pop();
    }       
    else 
    {
        str-&gt;insert( 0, "(" );
        str-&gt;insert( beg, t );
        str-&gt;insert( beg + 1, n-&gt;front() );
        beg += 1 + strlen( n-&gt;front() );
        str-&gt;insert( beg, ")" );
        n-&gt;pop();
        beg++;
    }

    //ex-&gt;pop();
    output( n, ex, beg + 1, str );
    //std::cout &lt;&lt; str &lt;&lt; std::endl;
}
</code></pre>

<p>If you need any commenting or explaining of what exactly certain stuff does, please let me know, I will be checking back here fairly often tonight.</p>
