Basic RPN expression evaluator

This is a discussion on Basic RPN expression evaluator within the C++ Programming forums, part of the General Programming Boards category; Works perfectly!!! Got it within 2 tries...but had to put 'p' and 'm' for '+' and '-' respectively.....Any idea why ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    Smile Basic RPN expression evaluator

    Works perfectly!!! Got it within 2 tries...but had to put 'p' and 'm' for '+' and '-' respectively.....Any idea why '+' and ' - ' characters are not accepted by the program? TIA
    If possible please compile the code and see if '+' and '-' do the job in your case..

    Code:
    #include<string>
    #include<sstream>
    #include<iostream>
    #include<deque>
    using namespace std;
    
    int main()
    {
        string input;
        getline(cin,input);
        deque<double> stack;
        istringstream in;
        in.str(input);
        double opn;
        char opt;
        for(;;)
        {
            if(in>>opn)
            {
                stack.push_back(opn);
    
                continue;
            }
            else
            {
                in.clear();
                if (in>>opt)
                {
    
                switch (opt)
                {
                    case 'p':
                    {
                        opn = stack.back();
                        stack.pop_back();
                        opn += stack.back();
                        stack.pop_back();
                        stack.push_back(opn);
                        break;
                    }
                    case 'm':
                    {
                        opn = stack.back();
                        stack.pop_back();
                        opn=stack.back()-opn;
                        stack.pop_back();
                        stack.push_back(opn);
                        break;
                    }
                    case '*':
                    {
                        opn = stack.back();
                        stack.pop_back();
                        opn *= stack.back();
                        stack.pop_back();
                        stack.push_back(opn);
                        break;
                    }
                    case '/':
                    {
                        opn = stack.back();
                        stack.pop_back();
                        opn=stack.back() / opn;
                        stack.pop_back();
                        stack.push_back(opn);
                        break;
                    }
                    default: cout<<"sorry._ :( _.";
    
    
                }
                }
                else break;
            }
    
        }
        opn = stack.back();
        stack.pop_back();
        if(stack.empty())
        cout<<opn;
        else
        cout<<"More Operands than Operators";
        cin.get();
        return 0;
    
    }

    Also searching for a better way to implement the operator selection than the cumbersome switch-case which would grow in complexity with increase in operators .
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by manasij7479
    Also searching for a better way to implement the operator selection than the cumbersome switch-case which would grow in complexity with increase in operators .
    It would not really grow in complexity (or become more complicated, if that is what you mean). It would just become longer, but that is not necessarily a problem. In fact, with what you have now, you can move this:
    Code:
    opn = stack.back();
    stack.pop_back();
    to before the switch and move this:
    Code:
    stack.pop_back();
    stack.push_back(opn);
    to after the switch. As for what I had in mind as an alternative: map strings to function pointers/objects. This way, if you want to add a new operation, you just modify the creation of the map, and add the definition of the function (or function object class).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    After some unary operations or (implementations of a algebraic library ) are added, it could potentially become become more complex.
    How would I fit things like "sin" or "log" inside the switch?
    Is using goto a very bad idea for implementing the unary operators? (to jump over the 2nd pop_back statement)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by manasij7479
    How would I fit things like "sin" or "log" inside the switch?
    It is similiar to a unary operator.

    Quote Originally Posted by manasij7479
    Is using goto a very bad idea for implementing the unary operators?
    Yes, avoid goto.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by laserlight View Post
    ...... and move this:
    Code:
    stack.pop_back();
    stack.push_back(opn);
    to after the switch.
    just after that I've put
    Code:
              stack.pop_back();
                    if(in>>opt_s)
                    {
                        if(opt_s=="sin")stack.push_back(sin(opn));
                    }
    for the similar unary functions...(.as the switch didn't accept them)
    but it shows a segmentation fault at runtime.....Tried putting up cout 's to determine the source with no success...
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    As to why + and - are giving you fits: you are trying to read in a double. A double can start with + and with -, so those symbols are taken as the start of a double rather than falling through to the character parser. I would suggest this is why trying to read doubles-and-somethimes-characters vs. reading character at a time is a pretty bad idea.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by manasij7479
    but it shows a segmentation fault at runtime.....Tried putting up cout 's to determine the source with no success...
    Use a debugger.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    I would suggest this is why trying to read doubles-and-somethimes-characters vs. reading character at a time is a pretty bad idea.
    Then I have to put the character portion of the code before trying to play with the doubles...
    Use a debugger.
    I tried to put off learning to do so as long as possible...!..Would the Code::blocks debugger be a better option than the built in gnu debugger?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by manasij7479 View Post
    I tried to put off learning to do so as long as possible...!..Would the Code::blocks debugger be a better option than the built in gnu debugger?
    C::B has nothing of its own (no compiler, no debugger, no anything). If you have the gnu debugger, then you can use that. C::B has a front end for that debugger, but I've not used it much.

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    I thought the debugger was a standalone plugin of c::B unlike the compiler....!.........So, theoretically any any debugger would work well with it..
    What is the most used debugger in a linux envionment ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    I'd say GDB - you most probably already have it, and it should work with C::B. I don't know what other debuggers does the C::B frontend support, to be honest.

    As for what I had in mind as an alternative: map strings to function pointers/objects. This way, if you want to add a new operation, you just modify the creation of the map, and add the definition of the function (or function object class).
    I concur. Actually, this is exactly what I did (well, it differed in implementation details, irrelevant at this point ).
    Last edited by Xupicor; 02-03-2011 at 04:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 08:15 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Expression Evaluator Contest
    By Stack Overflow in forum Contests Board
    Replies: 20
    Last Post: 03-29-2005, 09:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21