Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: RPM Calculation

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default RPM Calculation

    I have a program that I wrote a long time ago that converts an algebraic expression in RPM. How would I go about doing the actual calculation? Here is the source

    import java.util.Stack;
    import java.io.*;
    import javax.swing.*;
     
    public class RPN{
        public static boolean precedence(char a,char b){
            int pos[]=new int[2];
            pos[0]=-1;
            pos[1]=-1;
            for(int i=0;i<s.length;i++){
                if(s[i].indexOf(b)==1 && s[i].indexOf(a)!=0) return false;
            }
            for(int i=0;i<oper.length;i++){
                if(oper[i].indexOf(a)>-1) pos[0]=i;
                if(oper[i].indexOf(b)>-1) pos[1]=i;
            }
            if(pos[0]<pos[1]) return true;
            else if(pos[0]>=pos[1] && pos[0]!=-1 && pos[1]!=-1) return false;
            return true;
        }
     
        public static String prepare(String infix){
            infix=infix.replaceAll("[ ]+","");
            for(int i=0;i<oper.length;i++){
                for(int k=0;k<oper[i].length();k++){
                    infix=infix.replaceAll("\\"+oper[i].charAt(k)," \\"+oper[i].charAt(k)+" ");
                }
            }
     
            for(int i=0;i<s.length;i++){
                for(int k=0;k<s[i].length();k++){
                    infix=infix.replaceAll("\\"+s[i].charAt(k)," \\"+s[i].charAt(k)+" ");
                }
            }
     
            infix=infix.replaceAll("[ ]+"," ");
            if(infix.charAt(0)==' ') infix=infix.replaceFirst(" ","");
            return infix;
        }
     
        public static String toPrefix(String infix){
            infix=prepare(infix);
            Stack<Character> op_stack=new Stack();
            String[] token=infix.split(" ");
            StringBuffer out=new StringBuffer();
     
            for(int i=0;i<token.length;i++){
                boolean operator=false;
                for(int k=0;k<oper.length;k++){
                    if(oper[k].indexOf(token[i].charAt(0))>-1) operator=true;
                }
                for(int k=0;k<s.length;k++){
                    if(s[k].indexOf(token[i].charAt(0))>-1) operator=true;
                }
     
                if(operator==false){
                    out.append(token[i]);
                    out.append(" ");
                }
                else{
                    boolean empty=op_stack.isEmpty();
                    char op=token[i].charAt(0);
                    char peek;
                    boolean operand=false;
     
                    while(!(empty=op_stack.isEmpty()) && (!precedence(peek=op_stack.peek(),op))){
     
                        out.append(op_stack.pop());
                        out.append(" ");
                    }
     
                    if(!(empty=op_stack.isEmpty())){
                        for(int k=0;k<s.length;k++){
                            if(s[k].indexOf(op_stack.peek())==0 && s[k].indexOf(op)==1) operand=true;
                        }
                    }
     
                    if(empty || operand==false)
                        op_stack.push(op);
                    else op_stack.pop();
                }
            }
            while(!op_stack.isEmpty()){
                out.append(op_stack.pop());
                out.append(" ");
            }
            return out.toString();
        }
     
        public static void main(String[] args){
            JFrame frame=new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JOptionPane Pane=new JOptionPane();
            frame.setContentPane(Pane);
            String infix=Pane.showInputDialog("Enter String");
            Pane.showMessageDialog(frame,toPrefix(infix),infix,JOptionPane.ERROR_MESSAGE);
            frame.dispose();
        }
     
        private static final String [] oper={"+-","*/","^"};
        private static final String [] s={"()","[]","{}"};
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: RPM Calculation

    How would I go about doing the actual calculation?
    Can you explain in more detail what you want to do?
    The posted code has NO comments explaining what it is doing.
    Is RPM revoltions per minute?

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: RPM Calculation

    I think you mean RPN (Reverse polish notation)?

    Take a look at Wikipedia:Reverse Polish Notation for a simple algorithm for evaluating RPN expressions.

Similar Threads

  1. SQL Time Calculation
    By r12ki in forum JDBC & Databases
    Replies: 3
    Last Post: July 31st, 2009, 03:54 AM