栈的三种表达式:前缀、中缀和后缀表达式,后缀也叫逆波兰表达式
前缀(波兰表达式)
中缀(对人来讲很好理解,对于计算机来讲就方便了,一般会把中缀表达式转换成后缀表达式)
后缀(逆波兰表达式)
计算过程
例字
这里就用java自带的栈了Stack(多位数也可以)(自己写个栈也可以,这里为了方便)
(这里输入的是后缀表达式,中缀转后缀往下看)
package stack; import java. util. ArrayList;
import java. util. List;
import java. util. Stack; public class PolandNotation { public static void main ( String [ ] args) { String suffixExpression = "3 4 + 5 * 6 -" ; List< String> list = getListString ( suffixExpression) ; int res = calculate ( list) ; System. out. println ( res) ; } public static List< String> getListString ( String suffixExperssion) { String[ ] split = suffixExperssion. split ( " " ) ; ArrayList< String> list = new ArrayList < > ( ) ; for ( String ele : split) { list. add ( ele) ; } return list; } public static int calculate ( List< String> ls) { Stack< String> stack = new Stack < > ( ) ; for ( String item : ls) { if ( item. matches ( "\\d+" ) ) { stack. push ( item) ; } else { int num2 = Integer. parseInt ( stack. pop ( ) ) ; int num1 = Integer. parseInt ( stack. pop ( ) ) ; int res = 0 ; if ( item. equals ( "+" ) ) { res = num1+ num2; } else if ( item. equals ( "-" ) ) { res = num1 - num2; } else if ( item. equals ( "*" ) ) { res = num1 * num2; } else if ( item. equals ( "/" ) ) { res = num1 / num2; } else { throw new RuntimeException ( "运算符有误" ) ; } stack. push ( res + "" ) ; } } return Integer. parseInt ( stack. pop ( ) ) ; }
}
中缀表达式 转 后缀表达式
1)操作步骤:
2)例
代码几乎每行都有注释,根据思路一步一步分析来自己实现一下。
1)中缀转后缀代码
public static List< String> parseSuffixExpressionList ( List< String> ls) { Stack< String> s1 = new Stack < > ( ) ; ArrayList< String> s2 = new ArrayList < String> ( ) ; for ( String item : ls) { if ( item. matches ( "\\d+" ) ) { s2. add ( item) ; } else if ( item. equals ( "(" ) ) { s1. push ( item) ; } else if ( item. equals ( ")" ) ) { while ( ! s1. peek ( ) . equals ( "(" ) ) { s2. add ( s1. pop ( ) ) ; } s1. pop ( ) ; } else { while ( s1. size ( ) != 0 && Operation. getValue ( s1. peek ( ) ) >= Operation. getValue ( item) ) { s2. add ( s1. pop ( ) ) ; } s1. push ( item) ; } } while ( s1. size ( ) != 0 ) { s2. add ( s1. pop ( ) ) ; } return s2; }
2)判断符号优先级
class Operation { private static int ADD = 1 ; private static int SUB = 1 ; private static int MUL = 2 ; private static int DIV = 2 ; public static int getValue ( String operation) { int result = 0 ; switch ( operation) { case "+" : result = ADD; break ; case "-" : result = SUB; break ; case "*" : result = MUL; break ; case "/" : result = DIV; break ; default : result = 0 ; break ; } return result; }
}
完整代码(以及上面的运算)
package stack; import java. util. ArrayList;
import java. util. List;
import java. util. Stack; public class PolandNotation { public static void main ( String [ ] args) { String expression = "1+((2+3)*4)-5" ; List< String> list = toInfixExpressionList ( expression) ; System. out. println ( "中缀表达式:" + list) ; List< String> parseSuffixExpressionList = parseSuffixExpressionList ( list) ; System. out. println ( "后缀表达式:" + parseSuffixExpressionList) ; int calculate = calculate ( parseSuffixExpressionList) ; System. out. println ( "后缀表达式计算结果:" + calculate) ; } public static List< String> parseSuffixExpressionList ( List< String> ls) { Stack< String> s1 = new Stack < > ( ) ; ArrayList< String> s2 = new ArrayList < String> ( ) ; for ( String item : ls) { if ( item. matches ( "\\d+" ) ) { s2. add ( item) ; } else if ( item. equals ( "(" ) ) { s1. push ( item) ; } else if ( item. equals ( ")" ) ) { while ( ! s1. peek ( ) . equals ( "(" ) ) { s2. add ( s1. pop ( ) ) ; } s1. pop ( ) ; } else { while ( s1. size ( ) != 0 && Operation. getValue ( s1. peek ( ) ) >= Operation. getValue ( item) ) { s2. add ( s1. pop ( ) ) ; } s1. push ( item) ; } } while ( s1. size ( ) != 0 ) { s2. add ( s1. pop ( ) ) ; } return s2; } public static List< String> toInfixExpressionList ( String s) { ArrayList< String> list = new ArrayList < > ( ) ; int i = 0 ; String str; char c; do { if ( ( c = s. charAt ( i) ) < 48 || ( c= s. charAt ( i) ) > 57 ) { list. add ( "" + c) ; i++ ; } else { str = "" ; while ( i < s. length ( ) && ( c= s. charAt ( i) ) >= 48 && ( c= s. charAt ( i) ) <= 57 ) { str += c; i++ ; } list. add ( str) ; } } while ( i < s. length ( ) ) ; return list; } public static List< String> getListString ( String suffixExperssion) { String[ ] split = suffixExperssion. split ( " " ) ; ArrayList< String> list = new ArrayList < > ( ) ; for ( String ele : split) { list. add ( ele) ; } return list; } public static int calculate ( List< String> ls) { Stack< String> stack = new Stack < > ( ) ; for ( String item : ls) { if ( item. matches ( "\\d+" ) ) { stack. push ( item) ; } else { int num2 = Integer. parseInt ( stack. pop ( ) ) ; int num1 = Integer. parseInt ( stack. pop ( ) ) ; int res = 0 ; if ( item. equals ( "+" ) ) { res = num1+ num2; } else if ( item. equals ( "-" ) ) { res = num1 - num2; } else if ( item. equals ( "*" ) ) { res = num1 * num2; } else if ( item. equals ( "/" ) ) { res = num1 / num2; } else { throw new RuntimeException ( "运算符有误" ) ; } stack. push ( res + "" ) ; } } return Integer. parseInt ( stack. pop ( ) ) ; }
}
class Operation { private static int ADD = 1 ; private static int SUB = 1 ; private static int MUL = 2 ; private static int DIV = 2 ; public static int getValue ( String operation) { int result = 0 ; switch ( operation) { case "+" : result = ADD; break ; case "-" : result = SUB; break ; case "*" : result = MUL; break ; case "/" : result = DIV; break ; default : result = 0 ; break ; } return result; }
}
结果:
中缀表达式:[ 1 , + , ( , ( , 2 , + , 3 , ) , * , 4 , ) , - , 5 ]
后缀表达式:[ 1 , 2 , 3 , + , 4 , * , + , 5 , - ]
后缀表达式计算结果:16