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!
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.
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:
From here we would do something like this (in pseudocode):Code:<--top bottom--> 2 3 + 5 2 ^ /
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.)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 }
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):
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.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 }
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 /
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks