profram postfix ke infix dengan java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PostIn;

/**
 *
 * @author
 */
import java.util.Stack;
import java.util.Scanner;
import java.io.*;

public class postfix_infix {
    public static void main(String[]args){
        Stack operands = new Stack();
        Stack operators = new Stack();
        Scanner input = new Scanner (System.in);
        String input_postfix;
        int prec2=0;
                int prec1=0;
                char c=0;
                Object a=null,b=null;
                System.out.print("masukan notasi postfix : ");
                input_postfix = input.next();
               
                for(int i=0;i<input_postfix.length();i++){
                    c = input_postfix.charAt(i);
                   
                    if (c =='+'|| c=='*'||c=='/') {
                        prec1 = 1;
                       
                        if (c =='+'||c=='-') {
                            prec2 = 1;
                        }else{
                            prec2 = 2;
                        }
                           
                            if (prec2<prec1){
                                a = operands.pop();
                                operands.push(c);
                                operands.push(a);
                               
                            }else{
                                b = operands.pop();
                                operands.push(c);
                                operands.push(b);
                            }
                           
                        }else{
                            operands.push(c);
                        }
                    }
                       
    System.out.println("hasil infix : "+operands);
                    }
                }



good luck^_^ sQ

Komentar

Postingan Populer