0% found this document useful (0 votes)
1 views

InfixToPostFix

The document presents a Java program that converts infix expressions to postfix notation using a stack data structure. It includes methods to determine operator precedence, check for operators, and process the conversion. The main method prompts the user for an infix expression and displays the corresponding postfix expression.

Uploaded by

invoker26d2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

InfixToPostFix

The document presents a Java program that converts infix expressions to postfix notation using a stack data structure. It includes methods to determine operator precedence, check for operators, and process the conversion. The main method prompts the user for an infix expression and displays the corresponding postfix expression.

Uploaded by

invoker26d2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

/’’Title: Infix to Postfix

‘’@Cabais, Jefren Paul A.


‘’@Yacapin, Neil John
‘’@Madarang, Sebastian
‘’@Lacaden Jeremiah
/
import java.util.Scanner;
import java.util.Stack;

public class infixToPostFix2 {

// Method to determine the precedence of operators


private static int precedence(char operator) {
if (operator == '+' || operator == '-') {
return 1; // Low precedence for addition and subtraction
} else if (operator == '*' || operator == '/' || operator == '%') {
return 2; // Higher precedence for multiplication and division
} else {
return 0; // Default precedence for non-operators
}
}

// Method to check if a character is an operator


private static boolean isOperator(char a) {
return a == '+' || a == '-' || a == '*' || a == '/' || a == '%';
}

// Method to convert infix expression to postfix expression


public static String conversionMethod(String operand) {
Stack<Character> stack = new Stack<>(); // Stack to store operators
StringBuilder output = new StringBuilder(); // StringBuilder to build the
output postfix expression

// Iterate through each character in the infix expression


for (int i = 0; i < operand.length(); i++) {
char token = operand.charAt(i);

// If the token is an operand (letter or digit), add it to output


if (Character.isLetterOrDigit(token)) {
output.append(token);
}
// If the token is '(', push it onto the stack
else if (token == '(') {
stack.push(token);
}
// If the token is ')', pop from stack until '(' is encountered
else if (token == ')') {
while (!stack.isEmpty() && stack.peek() != '(') {
output.append(stack.pop());
}
stack.pop(); // Remove the '(' from the stack
}
// If the token is an operator, process based on precedence
else if (isOperator(token)) {
while (!stack.isEmpty() && precedence(stack.peek()) >=
precedence(token)) {
output.append(stack.pop());
}
stack.push(token); // Push current operator onto stack
}
}

// Pop all remaining operators in the stack and add to output


while (!stack.isEmpty()) {
output.append(stack.pop());
}

return output.toString(); // Return the resulting postfix expression


}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

// Prompt user for an infix expression


System.out.print("Enter an infix: ");
String expression = sc.nextLine();

// Display the converted postfix expression


System.out.println("The Postfix of " + expression + " is " +
conversionMethod(expression));

sc.close(); // Close the scanner


}
}

You might also like