未验证 提交 25b3ff47 编写于 作者: ishellhub's avatar ishellhub 提交者: GitHub

Merge pull request #1964 from TheAlgorithms/infix2posfix

Infix2posfix
...@@ -20,6 +20,7 @@ jobs: ...@@ -20,6 +20,7 @@ jobs:
- name: Commit Format changes - name: Commit Format changes
if: failure() if: failure()
run: | run: |
git diff
git config --global user.name github-actions git config --global user.name github-actions
git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com' git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com'
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
......
...@@ -75,6 +75,7 @@ ...@@ -75,6 +75,7 @@
* Stacks * Stacks
* [BalancedBrackets](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/BalancedBrackets.java) * [BalancedBrackets](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/BalancedBrackets.java)
* [DecimalToAnyUsingStack](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/DecimalToAnyUsingStack.java) * [DecimalToAnyUsingStack](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/DecimalToAnyUsingStack.java)
* [InfixToPostfix](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/InfixToPostfix.java)
* [NodeStack](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/NodeStack.java) * [NodeStack](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/NodeStack.java)
* [StackArray](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/StackArray.java) * [StackArray](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/StackArray.java)
* [StackArrayList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/StackArrayList.java) * [StackArrayList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/StackArrayList.java)
......
package DataStructures.Stacks;
import java.util.Stack;
public class InfixToPostfix {
public static void main(String[] args) throws Exception {
assert "32+".equals(infix2PostFix("3+2"));
assert "123++".equals(infix2PostFix("1+(2+3)"));
assert "34+5*6-".equals(infix2PostFix("(3+4)*5-6"));
}
public static String infix2PostFix(String infixExpression) throws Exception {
if (!BalancedBrackets.isBalanced(infixExpression)) {
throw new Exception("invalid expression");
}
StringBuilder output = new StringBuilder();
Stack<Character> stack = new Stack<>();
for (char element : infixExpression.toCharArray()) {
if (Character.isLetterOrDigit(element)) {
output.append(element);
} else if (element == '(') {
stack.push(element);
} else if (element == ')') {
while (!stack.isEmpty() && stack.peek() != '(') {
output.append(stack.pop());
}
stack.pop();
} else {
while (!stack.isEmpty() && precedence(element) <= precedence(stack.peek())) {
output.append(stack.pop());
}
stack.push(element);
}
}
while (!stack.isEmpty()) {
output.append(stack.pop());
}
return output.toString();
}
private static int precedence(char operator) {
switch (operator) {
case '+':
case '-':
return 0;
case '*':
case '/':
return 1;
case '^':
return 2;
default:
return -1;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册