目录
什么是后缀表达式
也叫逆波兰表达式,将运算符写在操作数之后
中缀形式:(10+20/2*3)/2+8
后缀形式:10 20 2 / 3 * + 2 / 8 +
方便计算机计算,但对用户不友好
后缀表达式示例图
栈实现后缀表达式
代码实现中缀表达式——后缀表达式
package p2.线性结构;
public class InfixToSuffix {
public static void main(String[] args) {
String expression = "(10+20/2*3)/2+8";
expression = infixToSuffix(expression);
System.out.println(expression);
}
public static String infixToSuffix(String expression) {
ArrayStack<String> opStack = new ArrayStack<>();
ArrayList<String> suffixList = new ArrayList<>();
expression = insertBlanks(expression);
String[] tokens = expression.split(" ");
for(String token : tokens) {
if (token.length() == 0) {
continue;
}
if (isOperator(token)) {
while (true) {
if (opStack.isEmpty() || opStack.peek().equals("(") ||
priority(opStack.peek()) < priority(token)) {
opStack.push(token);
break;
}
suffixList.add(opStack.pop());
}
}else if(token.equals("(")){
opStack.push(token);
}else if(token.equals(")")){
while (!opStack.peek().equals("(")){
suffixList.add(opStack.pop());
}
opStack.pop();
}else if(isNumber(token)){
suffixList.add(token);
}else {
throw new IllegalArgumentException("wrong cha! " + expression);
}
}
while(!opStack.isEmpty()){
suffixList.add(opStack.pop());
}
StringBuilder sb = new StringBuilder();
for(int i = 0; i < suffixList.size(); i++){
sb.append(suffixList.get(i));
sb.append(' ');
}
return sb.toString();
}
private static int priority(String token) {
if(token.equals("+")||token.equals("-")){
return 0;
}
if(token.equals("*")||token.equals("/")){
return 1;
}
return -1;
}
private static boolean isNumber(String token) {
return token.matches("\\d+");
}
private static boolean isOperator(String token) {
return token.equals("+")|| token.equals("-")||token.equals("*")||token.equals("/");
}
private static String insertBlanks(String expression) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < expression.length(); i++) {
char c = expression.charAt(i);
if (c == '(' || c == ')' || c == '+' || c == '-' || c == '*' || c == '/') {
sb.append(' ');
sb.append(c);
sb.append(' ');
} else {
sb.append(c);
}
}
return sb.toString();
}
}
后缀表达式计算器的定义
package p2.线性结构;
public class SuffixCalculator {
public static void main(String[] args) {
String infixExpression = "(10+20/2*3)/2+8";
String suffixExpression = InfixToSuffix.infixToSuffix(infixExpression);
int result = evaluateSuffix(suffixExpression);
System.out.println(result);
}
private static int evaluateSuffix(String expression) {
ArrayStack<Integer> stack = new ArrayStack<>();
String [] tokens = expression.split(" ");
for(String token : tokens){
if(token.length() == 0){
continue;
}
if(isNumber(token)){
stack.push(new Integer(token));
}else{
processAnOperator(stack,token);
}
}
return stack.pop();
}
private static void processAnOperator(ArrayStack<Integer> stack, String token) {
int num1 = stack.pop();
int num2 = stack.pop();
if(token.equals("+")){
stack.push(num2+num1);
}else if(token.equals("-")){
stack.push(num2-num1);
}else if(token.equals("*")){
stack.push(num2*num1);
}else if(token.equals("/")){
stack.push(num2/num1);
}
}
private static boolean isNumber(String token) {
return token.matches("\\d+");
}
}