Results 1 to 9 of 9

Thread: Loop operators!

  1. #1
    aRTx is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Question Loop operators!

    Hi all,

    I'm trying to do a java program to solve these problems like it

    Problem:
    Try in middle of numbers arithmetic operators (+,-,*,/) to get this
    5 5 5 5 = 30

    is there any way to do it with java (loop )

    thnx...

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default OT - ECMAScript

    Quote Originally Posted by Fubarable View Post
    Looks like I'll have to learn some JavaScript after all.
    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.

  7. #7
    aRTx is offline Member
    Join Date
    Mar 2009
    Posts
    27
    Rep Power
    0

    Default

    if anyone can show any code for that pls do it...

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

    Quote Originally Posted by aRTx View Post
    ... code for that ...
    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.

  9. #9
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    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

  1. Java Operators
    By Ash-infinity in forum New To Java
    Replies: 6
    Last Post: 12-02-2008, 12:51 PM
  2. Using the bitwise operators
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:39 PM
  3. Demonstration of the mathematical operators
    By Java Tip in forum java.util
    Replies: 0
    Last Post: 04-17-2008, 12:02 AM

Posting Permissions

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