Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to convert infix to postfix and prefix in java

import java.util.Stack;
public class InfixToPreFix {
    static int precedence(char c){
        switch (c){
            case '+':
            case '-':
                return 1;
            case '*':
            case '/':
                return 2;
            case '^':
                return 3;
        }
        return -1;
    }

    static StringBuilder infixToPreFix(String expression){

        StringBuilder result = new StringBuilder();
        StringBuilder input = new StringBuilder(expression);
        input.reverse();
        Stack<Character> stack = new Stack<Character>();

        char [] charsExp = new String(input).toCharArray();
        for (int i = 0; i < charsExp.length; i++) {

            if (charsExp[i] == '(') {
                charsExp[i] = ')';
                i++;
            }
            else if (charsExp[i] == ')') {
                charsExp[i] = '(';
                i++;
            }
        }
        for (int i = 0; i <charsExp.length ; i++) {
            char c = charsExp[i];

            //check if char is operator or operand
            if(precedence(c)>0){
                while(stack.isEmpty()==false && precedence(stack.peek())>=precedence(c)){
                    result.append(stack.pop());
                }
                stack.push(c);
            }else if(c==')'){
                char x = stack.pop();
                while(x!='('){
                    result.append(x);
                    x = stack.pop();
                }
            }else if(c=='('){
                stack.push(c);
            }else{
                //character is neither operator nor "("
                result.append(c);
            }
        }

        for (int i = 0; i <=stack.size() ; i++) {
            result.append(stack.pop());
        }
        return result.reverse();
    }

    public static void main(String[] args) {
        String exp = "A+B*(C^D-E)";
        System.out.println("Infix Expression: " + exp);
        System.out.println("Prefix Expression: " + infixToPreFix(exp));
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: sort list of list java 
Java :: stream.concat 
Java :: how to set java path in windows 10 
Java :: android text change java debounce 
Java :: infinite value total 
Java :: leer XML java 
Java :: quebra de linha java 
Java :: video compression with java 
Java :: java foreach method 
Java :: thread priorities in java 
Java :: boolean 
Java :: how to convert string hashcode to color in java android 
Java :: https://www.baeldung.com/hibernate-inheritance 
Java :: java Detect Cycle in a Directed Graph 
Java :: export java path in ubuntu 
Java :: intellij set target bytecode version 
Java :: JFrame change border 
Java :: . Java Program to Delete the Specified Integer from an Array 
Java :: show all spring boot beans 
Java :: java regular expressions 
Java :: final method and abstract method 
Java :: horizontalAlignment center jlabel 
Java :: java calendar to date 
Java :: mongodb spring boot index usage 0 
Java :: greatest of three in java 
Java :: java check if string appears twice in arraylist 
Java :: Salary example in method in java 
Java :: remove duplicates from list java 
Java :: android recyclerview pull to refresh 
Java :: easter egg 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =