Results 1 to 9 of 9
Thread: Loop operators!
- 05-09-2009, 03:21 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 27
- Rep Power
- 0
- 05-09-2009, 03:39 PM #2
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
You could either use regex, or use a loop, using the String.substring method combined with some others, like charAt . Also you could use the split method.
I die a little on the inside...
Every time I get shot.
-
It can be done, but I think that it would take some work. I'd consider using an enum with an abstract (int, int) method here to represent the binary operations, though you may need to use a stack in order to handle operator precedence.
- 05-10-2009, 12:22 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Are you trying to find a solution like 5*(5+(5/5)) - assuming parens are available? Or are you trying to evaluate such an expression after it has been entered by the user?
For the first you could use for loops that brute force their way through every possibility. Or more generally a recursive approach to the same thing.
For the second you can avoid the work mentioned by Fubarable by using Java's Rhino engine and simply evaluating the string.
-
Thanks pbrockway2, I keep forgetting about that. Looks like I'll have to learn some JavaScript after all.For the second you can avoid the work mentioned by Fubarable by using Java's Rhino engine and simply evaluating the string.
- 05-10-2009, 01:18 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
OT - ECMAScript
Me too! My approach is to make believe that it's Java and deal with the problems as they arise. But that's (inefficiently) lazy, bug prone and only feasible for the simplest uses. ECMA-262 (aka ECMAScript 5thEdition aka ECMAScript 3.1) has just passed its final development milestone so that might be the spur I need to read the standard for a language that doesn't look like it's disappearing anytime soon.
- 05-11-2009, 07:42 AM #7
Member
- Join Date
- Mar 2009
- Posts
- 27
- Rep Power
- 0
if anyone can show any code for that pls do it...
- 05-11-2009, 11:42 PM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Finished composing this post to find it was something of a rant. So feel free to ignore it. But of you're offended by it, you misunderstand its intention.
Code for what?
Supamagier addressed up the problem of building up candidate strings with chatAt() ad substring().
Fubarable adressed the question of actually evaluating a candidate expression by treating each operator as an enum. So you might have PLUS, TIMES etc and this enum would have a method for evaluating a term. 3*4+5 would end up being something like PLUS.eval(TIMES.eval(3, 4), 5).
He went on the address the problem of precedence: 3*4+5 is quite a different beast from 3+4*5. It's not so much a matter of so-called "order" of operations, as it is of "binding". In the first expression 4 "binds to" 3 while in the second it binds to 5. He suggested using a stack for this. The traditional name for operators that go in between the things they operate on is "infix". Wikipedia is your friend.
Finally I posed a question: so what are you stuck on? The systematic enumeration of the expressions? or the calculation of their value? Not so much because I was desperately keen to know, but because precision is a good thing.
And I posed an approach to the second question that let the library writers worry about infix, stacks, reflection and the rest: use JavaScript.
Sorry to rehearse all this: but it seems to me you were given a lot to go on. And no questions about what was meant nor answer to explain what precisely was being sought. Just a plea for teh codez: teh codez, indeed, for that.
I'm sure you have been trying things: possibly you have looked at Wikipedia for stacks and stuff, or your favourite snippet site to see how the ScriptEngine business works. You have written code with a specific intent that has failed to meet the aim you had for it. And that's what a good question would be at this point: an account of your strategy (and your research that gave rise to it), your code and its shortcomings (runtime or compile time).
YA request for teh codez gives all together the wrong impression.
- 05-12-2009, 04:47 PM #9
Look into infix/prefix/postfix notation and solving them using stacks. It's really simple once you've been exposed to it and gain a basic understanding. You can convert that to 5555+-*/ and then use charAt() and isDigit() to seperate the numbers from the operands. Use 2 stacks(numbers, operands) and then just toss a loop(probably need a case statement also, writing this code in my head) StringBuffer.append(numbers.pop()) and StringBuffer.append(operands.pop()) will give you 5+5-5*5/
so of course you'll need to check that your numbers stack has 1 more item than your operands stack.
And as for using Rhino and Scripting I have no idea what their talking about so I'm going to look that up :)
Happy coding.Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
Similar Threads
-
Java Operators
By Ash-infinity in forum New To JavaReplies: 6Last Post: 12-02-2008, 11:51 AM -
Using the bitwise operators
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:39 PM -
Demonstration of the mathematical operators
By Java Tip in forum java.utilReplies: 0Last Post: 04-16-2008, 11:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks