Thread: RPN Calc

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    23

    RPN Calc

    Hey, I was just wondering if anyone could give me some suggestion on writing an RPN calc? I am using a double llist lib that I wrote to push and pop. Can anyone suggest how I can decide if I am getting a Char or Int?

    I am looking at taking the input like this:

    3
    1
    2
    +
    *
    9

    Honestly, I am having a bit of a problem thinking in RPN

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Char's are just ascii numbers, within a certain range of the ASCII code table. If you don't have a table to refer to, download one from asciitable.com. (Makes the assumption that your system uses ASCII, of course).

    Basically, if a char is less than 'A', (and you've limited it to only letters and numbers for input), then it must be a number. Be sure to make logic to handle the * + - /, etc. that you need.

    RPN is actually an easier calculator to program, but you do have to get your head wrapped around the whole RPN thing, first. I tried walking backward for a bit, didn' t seem to help a darn, though.

    Read Wiki's stuff, yet?

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Read the wikipedia stuff.

    Does that ASCII solution still work for an input with larger numbers?

    13241
    184781
    +

    Can you tell me how this would look in RPN: 9 (4 - 2) / 4

    THanks for the input and if you have any reading sugestion please post and I'll look though them.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by zensatori View Post
    Read the wikipedia stuff.

    Does that ASCII solution still work for an input with larger numbers?

    13241
    184781
    +

    Can you tell me how this would look in RPN: 9 (4 - 2) / 4

    THanks for the input and if you have any reading sugestion please post and I'll look though them.
    OK, your program should know what input it needs (letters, numbers, or operators), in which function. Maybe you have a menu with some letters as choices:

    A) More info on the program
    B) More info on RPN
    C) Calculate a problem using RPN

    etc. Here, only letters should be accepted as entry, in the A-C range.

    Once you're in the calc() function, there shouldn't be any more letters entered, correct? It's either number or operators.

    9 (4 - 2) / 4 is: 9 4 2 - * 4 / (been a long while though. Note the space between each digit!).

    That ASCII input stuff still works, but you shouldn't have to use it (I belive), with a good program design. If you do, you may want to get all the string input via fgets(), and then use sscanf to work out what's a number, a letter, or an operator.

    Hope that helps. I haven't done any RPN stuff in MANY years, so consider yourself warned.

    Edit: Found this from somebody who knows a bit more:
    (2+3)*(4+5)

    To turn it into an infix to RPN converter,
    just record the pushes onto the operand stack, and the non-parenthesis
    operations that get popped, rather than carrying them out. So the
    second example would record

    push 2
    push 3
    add
    push 4
    push 5
    add
    multiply

    Last edited by Adak; 06-10-2007 at 04:53 PM.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Could I take the stuff poped off the stack as chars and then check if they are operators? If not put them in temp ints and preform math on them? Problem I see with that is I am only checking a very small set of possible inputs. Someone could still enter 34a and my code would try and do it.

    Also, what would somthing like 246-* be in standard notation? Is that even valid? I gues I am confused on what to do if after I pop the top 2 number and the next thing on the stack is a number as well?

    Does anyone know of some sample of code?

    Examples of RPN in c would be great. Sorry I am being dense.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by zensatori View Post
    Could I take the stuff poped off the stack as chars and then check if they are operators? If not put them in temp ints and preform math on them? Problem I see with that is I am only checking a very small set of possible inputs. Someone could still enter 34a and my code would try and do it.

    Also, what would somthing like 246-* be in standard notation? Is that even valid? I gues I am confused on what to do if after I pop the top 2 number and the next thing on the stack is a number as well?

    Does anyone know of some sample of code?

    Examples of RPN in c would be great. Sorry I am being dense.
    There are examples available, just google them up.

    Spacing between numbers is critical in RPN. 2 4 6 - * is NOT the same as 2 46 - *

    It's your program, you can deal with your letters and numbers and operators, pretty much as you want. 2 4 6 - * in infix notation, would be (6 - 4) * 2. Remember, the 2 4 and 6 are on the stack, so the 2 will be the last number you deal with as you pop them back off the stack.

    push 2
    push 4
    push 6
    subtract (6 - 4) (both popped off the stack), push result onto the stack
    multiply (result * 2) (result and 2 both popped off the stack), new result pushed onto the stack (and it's the only thing on the stack, now).

    I'm fuzzy about which number is subtracted from which, but there are on-line applets for RPN that could help you with that. You should spend some time with them to practice thinking in RPN.
    Last edited by Adak; 06-11-2007 at 02:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RPN & Functions
    By Cactus_Hugger in forum Tech Board
    Replies: 3
    Last Post: 09-06-2006, 01:21 PM
  2. Calc age use Date of PC
    By miziri in forum C++ Programming
    Replies: 12
    Last Post: 03-18-2006, 05:01 AM
  3. RPN calc
    By blackswan in forum C Programming
    Replies: 5
    Last Post: 05-15-2005, 01:44 PM
  4. Question regarding RPN and error detection
    By Roule in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2004, 01:12 AM
  5. Basic Calc
    By Universe in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2002, 10:07 PM