Results 1 to 3 of 3
  1. #1
    tomtaila is offline Member
    Join Date
    Jul 2011
    Posts
    21
    Rep Power
    0

    Default Analysing a String to call functions

    I was wondering what is the best way to analyse a string and perform functions from it. I'm not sure what the technique itself is actually called so I'll give an example:

    Given the parse tree:

    +
    /\
    + 2
    /\
    3 2

    Which represents the function: (3+2)+2 = 7

    I have managed to obtain the following String to represent the functionality: "++322"

    My question is what is the best way to take strings like this and derive the functionality they represent? Any resources to help with this problem would be a massive help, thanks!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Analysing a String to call functions

    Well, I might do it differently. Given your parse tree I would do a post-order traversal of the tree (left node, right node, root) and get
    32+2+. This is then in Reverse Polish Notation.. Then I would simply implement a stack, pushing the tokens on as I get them. As soon as I see an operator, I would apply the operator to the top two stack elements and push the result back on the stack. Then just keep repeating the process.

    Other approaches may also work.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Analysing a String to call functions

    After some thought I don't believe I answered your original question. It sounds like what you want is a function array where each element represents a different function. In my experience these are referred to as function tables.

    One way to do this would be to use a HashMap to have operators hash to the correct function.

    Java Code:
    interface Operation {
       public double compute(double a, double b);
    }
    
    public class Example {
    
       public static void main(String[] args) {
          Map<String, Operation> Ops = new HashMap<>();
          Ops.put("+", new Operation() {
             public double compute(double a, double b) {
                return a + b;
             }
          });
          Ops.put("*", new Operation() {
             public double compute(double a, double b) {
                return a * b;
             }
          });
          String op = "*";
          System.out.println(Ops.get(op).compute(10, 20)); // should print 200
       }
    }
    To make the code simpler I didn't use generics (otherwise the compiler would complain about passing 10 and 20 to Doubles). This is essentially using the Strategy based design pattern. Also, as of Java 1.7, you can switch on string values in case statements.

    Jim
    Last edited by jim829; 02-26-2013 at 12:54 AM. Reason: Added Java 1.7 Comment
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 7
    Last Post: 01-20-2012, 11:29 AM
  2. Call a public string variable
    By onstock in forum New To Java
    Replies: 3
    Last Post: 11-23-2011, 12:25 PM
  3. Replies: 2
    Last Post: 10-26-2011, 09:01 PM
  4. how to call class using String
    By newbiejava in forum New To Java
    Replies: 5
    Last Post: 08-03-2010, 04:20 PM
  5. Analysing for starvation, deadlock and mutual exclusion
    By scarymovie in forum New To Java
    Replies: 0
    Last Post: 03-07-2009, 06:58 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
  •