can any body help me


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: can any body help me

Hybrid View

  1. #1
    Join Date
    Mar 2007
    Posts
    2

    can any body help me

    i have this code it's convert infix to postfix
    but i wantto build apostfix tree can u help me
    package untitled10;
    import java.*;
    import java.util.*;
    import javax.swing.tree.*;
    import javax.*;
    import javax.swing.*;
    public class InfixConverter {



    public static void main(String[] args){





    InfixConverter in = new InfixConverter();
    Stack s = in.conversion("A*B+C*D");



    }


    public static Stack conversion(String infix){
    InfixConverter t = new InfixConverter();
    ArrayList tokens = t.tokenizeString(infix);
    System.out.println(tokens.toString());
    Iterator it = tokens.iterator();
    Stack stack = new Stack();
    Stack remember = new Stack();
    while(it.hasNext()){
    String s = ((String)it.next());
    System.out.println("s="+s+";");
    char c = s.charAt(0);
    stack.push(s);
    if(it.hasNext()){
    System.out.println("s="+s+"ddd");
    String s1 = ((String)it.next());
    char c1 = s1.charAt(0);
    int check=IsOperand(c1);
    if (check==1){

    stack.push(s1);
    }
    else if(remember.empty()){
    // System.out.println("sdsd");
    remember.push(s1);
    System.out.println("sdsd"); }
    else{
    String s2=(String)remember.pop();
    // System.out.println("sg");
    char c2=s2.charAt(0);
    if (TakesPrecedence(c1,c2)==1){
    // System.out.println("ss");
    //stack.push(s1);
    remember.push(s2);
    remember.push(s1);
    System.out.println("s=gg"+s1);

    }
    else {

    System.out.println("s="+s+";"+s1);
    stack.push(s2);
    System.out.println("sdsd1");
    remember.push(s1);
    System.out.println("sdsd2"); }
    //what i have to use for loop end alone


    }



    // default: stack.push(s);
    }}
    do {
    //String x =(String)remember.pop();
    // System.out.println("s="+s+";"+x);
    // System.out.println("snn");
    stack.push(remember.pop());
    //System.out.println("kj");
    }while (!remember.empty());

    System.out.println(stack.toString());

    return stack;
    }
    //[(, 1, 2,(,*, 3, 4]
    public ArrayList tokenizeString(String s){
    ArrayList tokens = new ArrayList();
    String number="";
    for(int i =0;i<s.length();i++){
    char c = s.charAt(i);
    if(Character.isDigit(c)){
    number+=c;
    }
    else{
    if(!number.equals("")){
    tokens.add(number);
    number="";
    }
    tokens.add(c+"");
    }
    }
    if(!number.equals("")){
    tokens.add(number);
    }
    return tokens;
    }
    public static int IsOperand(char ch)
    {
    if (((ch>='a')&&(ch<='z'))||
    ((ch>='A')&&(ch<='Z')) ||
    ((ch >= '0') && (ch <= '9')))
    return 1;
    else
    return 0;
    }
    public static int TakesPrecedence(char OperatorA,char OperatorB)
    {
    if (OperatorA == '(')
    return 0;
    else if (OperatorB == '(')
    return 0;
    else if (OperatorB == ')')
    return 1;
    else if ((OperatorA == '^') && (OperatorB == '^'))
    return 0;
    else if (OperatorA == '^')
    return 1;
    else if (OperatorB == '^')
    return 0;
    else if ((OperatorA == '*') || (OperatorA == '/'))
    return 1;
    else if ((OperatorB == '*') || (OperatorB == '/'))
    return 0;
    else
    return 1;}




    }

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Do you have the whole "precedence" thing down ... do you know how to use a stack to set the order in which you read values into the tree?

    With a postfix tree, you would have each subtree being the operands in the leaves (leafs) and the root of the subtree being the operator. An operand [or a value which is the result of operation(s) lower in the tree] will always be in a child when it comes time to "visit" that node.

    To build such a tree, if the value to be stored is an operator, it must be a parent node - try adding to left of the current node and, if left child is already occupied, then try right, if occupied then move to current node's parent and keep testing for right, if no move to parent or, if not occupied, add to right, then expect that new parent node take two children [as long as the operator is a binary and not a unary operator - if unary it will take one child]. If the value is a numerical value, then try left and if occupied already, add as right. If that is occupied, move up and test for non-existence of a right child until there is no right child and then add that value at that location.

    You visit in a "postfix" manner, where you move to the left node as far as you can, then to the right, and only when you must move back up to the parent do you "visit" the node by reading the value at the node or, if an operator, performing the operation on the operands which are the node's children.
    Last edited by nspils; 03-25-2007 at 03:57 AM.

  3. #3
    Join Date
    Mar 2007
    Posts
    2
    thank u but i need code
    thank u again

  4. #4
    Join Date
    Mar 2007
    Posts
    4
    Quote Originally Posted by didostar
    thank u but i need code
    thank u again
    How come you say that?
    You need the code??? If you really need it, you have to do something for it.
    It's wrong to help someone who says that...

Similar Threads

  1. Formatting the body of Email using CDOSYS
    By Edwin in forum Database
    Replies: 12
    Last Post: 04-08-2007, 10:27 AM
  2. USER BACKUP -What is wrong on the script?
    By kabucek in forum VB Classic
    Replies: 0
    Last Post: 10-04-2006, 11:49 AM
  3. Replies: 0
    Last Post: 03-27-2006, 03:42 PM
  4. Replies: 0
    Last Post: 11-14-2000, 03:15 PM
  5. Replies: 0
    Last Post: 11-14-2000, 03:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center