这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.baeldung.infixpostfix;

import java.util.Stack;

public class InfixToPostFixExpressionConversion {

private int getPrecedenceScore(char ch) {
switch (ch) {
case '^':
return 3;

case '*':
case '/':
return 2;

case '+':
case '-':
return 1;
}
return -1;
}

private boolean isOperand(char ch) {
return (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9');
}

private char associativity(char ch) {
if (ch == '^')
return 'R';
return 'L';
}

public String infixToPostfix(String infix) {
StringBuilder result = new StringBuilder();
Stack<Character> stack = new Stack<>();

for (int i = 0; i < infix.length(); i++) {
char ch = infix.charAt(i);

if (isOperand(ch)) {
result.append(ch);
} else if (ch == '(') {
stack.push(ch);
} else if (ch == ')') {
while (!stack.isEmpty() && stack.peek() != '(') {
result.append(stack.pop());
}
stack.pop();
} else {
while (!stack.isEmpty() && (operatorPrecedenceCondition(infix, i, stack))) {
result.append(stack.pop());
}
stack.push(ch);
}
}

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

return result.toString();
}

private boolean operatorPrecedenceCondition(String infix, int i, Stack<Character> stack) {
return getPrecedenceScore(infix.charAt(i)) < getPrecedenceScore(stack.peek()) || getPrecedenceScore(infix.charAt(i)) == getPrecedenceScore(stack.peek()) && associativity(infix.charAt(i)) == 'L';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.baeldung.infixpostfix;

import org.junit.Test;
import org.junit.jupiter.api.Assertions;

public class InfixToPostfixExpressionUnitTest {

@Test
public void givenSimpleOp_whenNoParenthesis_thenProduceValidPostfix() {
String infix = "a+b*c-d";
String postfix = "abc*+d-";

InfixToPostFixExpressionConversion obj = new InfixToPostFixExpressionConversion();

Assertions.assertEquals(postfix, obj.infixToPostfix(infix));
}

@Test
public void givenSimpleOp_whenWithParenthesis_thenProduceValidPostfix() {
String infix = "(a+b)*(c-d)";
String postfix = "ab+cd-*";

InfixToPostFixExpressionConversion obj = new InfixToPostFixExpressionConversion();

Assertions.assertEquals(postfix, obj.infixToPostfix(infix));
}

@Test
public void givenComplexOp_whenInputIsInfix_thenProduceValidPostfix() {
String infix = "a^b*(c^d-e)^(f+g*h)-i";
String postfix = "ab^cd^e-fgh*+^*i-";

InfixToPostFixExpressionConversion obj = new InfixToPostFixExpressionConversion();

Assertions.assertEquals(postfix, obj.infixToPostfix(infix));
}
}