使用堆栈将中缀转换为后缀 [英] Convert Infix to Postfix with Stack
本文介绍了使用堆栈将中缀转换为后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须编写一个程序,将用中缀表示法编写的表达式更改为后缀表示法.当我开始使用括号时,我遇到了问题.例如,当我输入a + (c - h)/(b * d)"时,结果应该是ac+h-b/d*",而结果应该是a c h - b d */+".非常感谢您的帮助.谢谢.
I have to make a program that changes an expression written in Infix notation to Postfix notation. I am running into a problem when I start using parentheses. For example, when I put in "a + (c - h) / (b * d)" is comes out as "ac+h-b/d*" when it should come out as "a c h - b d * / +." Would really appreciate the help. Thanks.
import java.util.Scanner;
import java.util.Stack;
public class PostfixConverter {
static private String expression;
private Stack<Character> stack = new Stack<Character>();
public PostfixConverter(String infixExpression) {
expression = infixExpression;
}
public String infixToPostfix() {
String postfixString = "";
for (int index = 0; index < expression.length(); ++index) {
char value = expression.charAt(index);
if (value == '(') {
} else if (value == ')') {
Character oper = stack.peek();
while (!(oper.equals('(')) && !(stack.isEmpty())) {
stack.pop();
postfixString += oper.charValue();
}
} else if (value == '+' || value == '-') {
if (stack.isEmpty()) {
stack.push(value);
} else {
Character oper = stack.peek();
while (!(stack.isEmpty() || oper.equals(('(')) || oper.equals((')')))) {
stack.pop();
postfixString += oper.charValue();
}
stack.push(value);
}
} else if (value == '*' || value == '/') {
if (stack.isEmpty()) {
stack.push(value);
} else {
Character oper = stack.peek();
while (!oper.equals(('+')) && !oper.equals(('-')) && !stack.isEmpty()) {
stack.pop();
postfixString += oper.charValue();
}
stack.push(value);
}
} else {
postfixString += value;
}
}
while (!stack.isEmpty()) {
Character oper = stack.peek();
if (!oper.equals(('('))) {
stack.pop();
postfixString += oper.charValue();
}
}
return postfixString;
}
public static void main(String[] args) {
System.out.println("Type an expression written in Infix notation: ");
Scanner input = new Scanner(System.in);
String expression = input.next();
PostfixConverter convert = new PostfixConverter(expression);
System.out.println("This expression writtien in Postfix notation is: \n" + convert.infixToPostfix());
}
}
推荐答案
您提供的代码类似于 this
.但该代码也不起作用.
You provided code is similar to this
. But that code also does not work.
我已经更新了您的代码并添加了更改的注释
.
I have updated your code and added the comments
of the changes.
import java.util.Scanner;
import java.util.Stack;
public class PostfixConverter {
static private String expression;
private Stack<Character> stack = new Stack<Character>();
public PostfixConverter(String infixExpression) {
expression = infixExpression;
}
public String infixToPostfix() {
String postfixString = "";
for (int index = 0; index < expression.length(); ++index) {
char value = expression.charAt(index);
if (value == '(') {
stack.push('('); // Code Added
} else if (value == ')') {
Character oper = stack.peek();
while (!(oper.equals('(')) && !(stack.isEmpty())) {
stack.pop();
postfixString += oper.charValue();
if (!stack.isEmpty()) // Code Added
oper = stack.peek(); // Code Added
}
stack.pop(); // Code Added
} else if (value == '+' || value == '-') {
if (stack.isEmpty()) {
stack.push(value);
} else {
Character oper = stack.peek();
while (!(stack.isEmpty() || oper.equals(('(')) || oper.equals((')')))) {
oper = stack.pop(); // Code Updated
postfixString += oper.charValue();
}
stack.push(value);
}
} else if (value == '*' || value == '/') {
if (stack.isEmpty()) {
stack.push(value);
} else {
Character oper = stack.peek();
// while condition updated
while (!oper.equals(('(')) && !oper.equals(('+')) && !oper.equals(('-')) && !stack.isEmpty()) {
oper = stack.pop(); // Code Updated
postfixString += oper.charValue();
}
stack.push(value);
}
} else {
postfixString += value;
}
}
while (!stack.isEmpty()) {
Character oper = stack.peek();
if (!oper.equals(('('))) {
stack.pop();
postfixString += oper.charValue();
}
}
return postfixString;
}
public static void main(String[] args) {
System.out.println("Type an expression written in Infix notation: ");
Scanner input = new Scanner(System.in);
String expression = input.next();
PostfixConverter convert = new PostfixConverter(expression);
System.out.println("This expression writtien in Postfix notation is: \n" + convert.infixToPostfix());
}
}
这篇关于使用堆栈将中缀转换为后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文