6 algorithms...

Sharky Forums


Results 1 to 9 of 9

Thread: 6 algorithms...

  1. #1
    Tiger Shark puff_1's Avatar
    Join Date
    Nov 2001
    Posts
    878

    6 algorithms...

    Anyone have a good source for the algorithms to convert infix/prefix/postfix?

  2. #2
    Reef Shark bocybo's Avatar
    Join Date
    Sep 2002
    Location
    Texas
    Posts
    280
    What exactly are you referring to? Tree trasversals or string manipulation or what?
    Reed's sister is sooo hot!

  3. #3
    Tiger Shark puff_1's Avatar
    Join Date
    Nov 2001
    Posts
    878
    algorithm using stacks to input an expression in infix/postfix/prefix and output the conversions. I think there is a mistake or a part I don't quite understand for one of my algoritms. I'm particularly looking for prefix to infix.
    Last edited by puff_1; 02-08-2004 at 04:34 PM.

  4. #4
    Tiger Shark
    Join Date
    Oct 2000
    Location
    Erie, PA, USA
    Posts
    693
    Can you post some code maybe? I've done things like this (converting a mathematical string to prefix, postfix, infix etc), but without seeing how you went about it, I can't help you much.
    AMD AthlonXP 2600+ Thoroughbred B @ 200x10.5
    Shuttle AN35N nForce2 Ultra 400
    2x512MB Kingston PC3200 (3-3-3)
    ATI Radeon 9600 Pro
    40GB WD ATA-100 8MB cache
    Creative 12X DVD Drive
    Memorex 52X CD-RW
    Running Windows XP Pro

  5. #5
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    HERE is source written in python that does the conversion as well as some discussion around the code. Is this what you're looking for?


    Also, for those of you who don't know what's going on here, read this.
    Last edited by rock; 02-09-2004 at 02:24 PM.

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

  6. #6
    Tiger Shark puff_1's Avatar
    Join Date
    Nov 2001
    Posts
    878
    I figured it out from my algorithm, it just wasn't very clear because there are no braces and I didn't add a push where is was supposed to be. I simply had to move a brace.

    This was the algorithm

    Code:
            set stack empty
            repeat
                 read(char)
                 if char in operator then
                     push char onto stack
                 else
                     if stacktop is an operand then
                         repeat
                            pop stack to x
                            pop stack to y
                            w = concatenate('(',x,y,char,')')
                            move w to char
                         until ((stack is empty) or (stacktop is an operator))
                     push char onto stack
            until (end of input)
            pop stack to get the final answer
    this is the code

    Code:
        public static void PreToIn(String pre){
            String x, y;
            String newString = "";
            ArrayStack MyStack = new ArrayStack(20);
            String topStack = "", character = "";
            
            int i = 0, j = 1;       
            while(j <= pre.length()){
                character = pre.substring(i,j);
                topStack = MyStack.pop();
                MyStack.push(topStack);
                if(isOperator(character) == true){
                    MyStack.push(character);            
                    topStack = MyStack.pop();
                    MyStack.push(topStack);
                }
                if(isOperator(topStack) == false){
                    while((isOperator(topStack) == false) || topStack == null){
                        x = MyStack.pop();
                        y = MyStack.pop();
                        newString = "(" + "(" + x + y + character + ")" + ")";
                        character = newString;
                        topStack = MyStack.pop();
                    }
                }
                    MyStack.push(character);
                
                i++;
                j++;
            }
            System.out.println(MyStack.pop());
        }
    I may have added a couple things to make it easier for me to understand. This is only 1 method and doesn't show the stack class.
    Last edited by puff_1; 02-09-2004 at 03:35 PM.

  7. #7
    Tiger Shark puff_1's Avatar
    Join Date
    Nov 2001
    Posts
    878
    In case anyone cares, that algorithm and method are both wrong. I fixed them today.

  8. #8
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    Originally posted by puff_1
    In case anyone cares, that algorithm and method are both wrong. I fixed them today.
    I'll the first to admit I didn't read or try your code

    Glad you got it working, though.

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

  9. #9
    Tiger Shark puff_1's Avatar
    Join Date
    Nov 2001
    Posts
    878
    Originally posted by rock
    I'll the first to admit I didn't read or try your code

    Glad you got it working, though.
    That's cool, I typically won't read other people's code either. Someone asked for it though, so I posted it.

Posting Permissions

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