0% found this document useful (0 votes)
51 views2 pages

Source Code Infix To Postfixinjava

This document contains code for a program that converts an infix mathematical expression to postfix notation. It uses a stack to evaluate operator precedence as it iterates through the infix string. If the character is an operand, it is added to the postfix string. If it is an operator, it is pushed onto the stack or popped off and added to postfix based on comparing its precedence to the top of the stack. Once complete, the postfix string is printed out.

Uploaded by

Chime Rosu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views2 pages

Source Code Infix To Postfixinjava

This document contains code for a program that converts an infix mathematical expression to postfix notation. It uses a stack to evaluate operator precedence as it iterates through the infix string. If the character is an operand, it is added to the postfix string. If it is an operator, it is pushed onto the stack or popped off and added to postfix based on comparing its precedence to the top of the stack. Once complete, the postfix string is printed out.

Uploaded by

Chime Rosu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package Tugasfix; import java.util.Scanner; import java.util.

Stack; public class InfixToPostfix { private String infix; private String postfix = ""; public void convertString(String a){ String str = ""; infix = a; Stack<String> stack = new Stack<String>(); for(int i = 0; i < infix.length(); i++){ str = infix.substring(i,i+1); if(str.matches("[a-zA-Z]|\\d")) postfix += str; else if (isOperator(str)){ if (stack.isEmpty()){ stack.push(str); } else{ String stackTop = stack.peek(); while (getPrecedence(stackTop,str).equals(stackTop)&& !(stack. isEmpty())){ postfix += stack.pop(); if (!(stack.isEmpty())) stackTop = stack.peek(); } stack.push(str); } } } while(!(stack.isEmpty())) postfix += stack.pop(); System.out.println("Maka Expressi dari bentuk postfix-nya adalah " + postfix); System.out.println();System.out.println(); System.out.println(" created by Amtsal Assakafhy"); } private boolean isOperator(String ch){ String operators = "*/%+-"; if (operators.indexOf(ch) != -1) return true; else return false; } private String getPrecedence(String op1, String op2){ String multiplicativeOps = "*/%"; String additiveOps = "+-"; if ((multiplicativeOps.indexOf(op1) != -1) && (additiveOps.indexOf(o p2) != -1)) return op1; else if ((multiplicativeOps.indexOf(op2) != -1) && (additiveOps.inde xOf(op1) != -1)) return op2; else if((multiplicativeOps.indexOf(op1) != -1) && (multiplicativeOps .indexOf(op2) != -1))

return op1; else return op1; } public static void main(String[] args) { System.out.println("Masukkan expressi dari bentuk infix:"); Scanner scanner = new Scanner(System.in); String expression = scanner.nextLine(); new InfixToPostfix().convertString(expression); } }

You might also like