Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 651231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:08:25+00:00 2026-05-13T22:08:25+00:00

So I’m attempting to use a queue to parse some input, turning prefix mathematical

  • 0

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.

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)+()

Hopefully that’s enough examples, if you need more, just ask.

I’m using GCC 4.2 in XCode on Mac OSX 10.6.2.

And here is the code that does this wonderful thing:

#include "EParse.h"
#include <iostream>
#include <iomanip>


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

void EParse::showParsed()
{
    parse( s, 0, len, new std::queue< char* >(), new std::queue< char >() );
}

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

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

void EParse::getNum( std::string *s, char* str, int &beg, int len )
{
    if( num( str[ beg ] ) )
    {
        char *t = new char[ 1 ];
        t[ 0 ] = str[ beg ];
        s->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< char* > *n, std::queue< char > *ex, int beg, std::string *str )
{
    if( ex->empty() )
    {
        return;
    }

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

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

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:08:25+00:00Added an answer on May 13, 2026 at 10:08 pm

    While I don’t have the exact answer to your problem, I did notice this:

    std::string *s = new std::string();
    getNum( s, str, beg, len );
    //std::cout << s->c_str() << std::endl;
    n->push( const_cast< char* >( s->c_str() ) );
    delete s;
    

    The problem there is you are pushing s into the queue and then you are deleting it. The queue, then, will be referencing a string’s value that no longer is valid, which could lead to the errors you are describing.

    To make life a little easier for you, I would recommend changing your queue type to:

    std::queue<std::string>
    

    Then you can push and pop whole std::strings instead of pointers to their data:

    n->push(s);
    

    Note that you’ll have to change the APIs of your routines from taking a char* to a std::string&, but you will be able to modify the string’s value like you did the char*.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 406k
  • Answers 406k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Finally solve this issue with following line of code :… May 15, 2026 at 6:06 am
  • Editorial Team
    Editorial Team added an answer HI, For my knowledge, in order to satisfy you requisites,… May 15, 2026 at 6:06 am
  • Editorial Team
    Editorial Team added an answer The cast to "short" is happening after the assignment. What… May 15, 2026 at 6:06 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.