Closed Thread
Results 1 to 3 of 3

Thread: New to programming, need REALbasic help.

  1. #1
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    New to programming, need REALbasic help.

    Hello!

    I'm very new to programming and since I'm on a mac and wanted to get started, I downloaded REALbasic. I've been having some fun with it, and now I'm trying to make a calculator program the interface of which includes two edit boxes and a push button. What I want is for the user to be able to enter an order of operations string like "(2+3)/5^2" into the first field and press the button to have the solution appear in the second field. I tried coding the button like so:

    PemdasResults.text = Str(Val(PemdasEnter.text))

    But when the button is pressed only the first entered number is displayed and not the solution. What should I do?

    Thank you!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: New to programming, need REALbasic help.

    You will need to parse the string, character by character, interpret what is supposed to be done, and perform all the calculations by hand.

    Note: there are few, if any, REALbasic programmers on this forum, so you may not get a lot of detailed help.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: New to programming, need REALbasic help.

    I don't speak REALBasic, but I'll help the best I can. What you're going to need is a stack, a parsing tree, and Reverse Polish Notation. Here's how it works:

    First you need to take your input and push the operations onto a stack with the operands first. We want to end up with something like this:
    Code:
    <--top bottom-->
    2 3 + 5 2 ^ /
    From here we would do something like this (in pseudocode):
    Code:
    function doMath() returns float
    {
        var float operand1, operand2, result
        var string operation
    
        operand1 := pop value off the stack
        operand2 := pop value off the stack
        operation := pop value off the stack
    
        if operation is '+' then
            result := add( operand1, operand2)
            push result onto the stack
            return
        else if operation is '^' then
            result := power( operand1, operand2 )
            push result onto the stack
            return
        ... and so on...
    
        once we get here there's going to be one value on the
        stack, which is our result. pop that off and return the
        value.
    
        result := pop the final value off the stack
        return result
    }
    So how do you parse all this onto the stack? (I'm assuming for now there's a stack object in REALBasic. If not, I'll see if I can help you write your own.) Writing a recursive function is great for this. (If you don't know what it is, it's a function that calls itself.)
    For the sake of simplicity I'm going to assume that:
    - Your function is recursive
    - You don't allow implied multiplication like (x + y)(w + z).
    - You don't allow chained operations like x + y + z. It'd have to be written (x + y) + z.
    - You're not going to check for erroneous input. Let's get it working first, then worry about those.Anyway, here's how you want to do it (again in pseudocode):

    Code:
    function parseMath(string input) returns nothing
    {
        var boolean hit_parens := false
        var string operand1, operand2, operator
    
        skip_whitespace()
    
        if input[0] == '(' then
            our left operand is an expression, not a number. we have to recurse and
            pass the expression to ourselves again. NOTE: Make sure to remove the
            parenthesis before calling ourselves so that we don't go into an infinite
            loop.
    
            delete_character(input,0)
            parseMath(input)
        endif
    
        The first character is not a parenthesis, so we can assume it's a number.
        get_number reads a number from the string and then deletes it from the string
        so that we can read the next part of it. same goes for read_operator, which
        reads in an operation and then deletes it from the string.
    
        operand1 := get_number(input)
        operator := get_operation(input)
    
        Now we have to check to see if our right-hand operand is a parenthesis. if it
        is, then we need to recurse and parse that expression.
    
        if input[0] == '(' then
            same as before...
    
            delete_character(input,0);
            parseMath(input)
    
            push    operand1
            push    operator
            return
        else
            push    operand1
            push    operand2
            push    operator
            return
        endif
    
        return
    }
    EDIT: I just realized that this doesn't take order of operations into account. Let's also ignore that for now, try to get it working, and then add features as necessary.

    Hope this helps! If you have any questions, just ask.
    Last edited by dargueta; 03-08-2010 at 07:07 PM. Reason: Fixed bug
    sudo rm -rf /

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Trial User beware - REALBasic (REALStudio)
    By WiiFan2012 in forum Software Development Tools
    Replies: 3
    Last Post: 06-24-2010, 06:34 PM
  2. RealBasic copy delete open
    By thanasis2028 in forum General Programming
    Replies: 2
    Last Post: 03-14-2008, 06:22 AM
  3. Anyone knows how to do this with REALbasic?
    By gcarcass in forum General Programming
    Replies: 3
    Last Post: 08-11-2007, 01:27 AM
  4. RealBasic 2007
    By Jordan in forum General Programming
    Replies: 4
    Last Post: 07-20-2007, 06:50 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts