中缀表达式转后缀表达式
思路分析
将中缀表达式“1+((2+3)×4)-5”转换为后缀表达式=>1 2 3 + 4 × + 5 -
步骤
初始化两个栈:运算符栈s1和储存中间结果的栈s2;
从左至右扫描中缀表达式;
遇到操作数时,将其压s2;
遇到运算符时,比较其与s1栈顶运算符的优先级:
如果s1为空,或栈顶运算符为左括号“(”,则直接将此运算符入栈;
否则,若优先级比栈顶运算符的高,也将运算符压入s1;
否则,将s1栈顶的运算符弹出并压入到s2中,再次转到(4. 1)与s1中新的栈顶运算符相比较;
遇到括号时:
如果是左括号“(”,则直接压入s1
如果是右括号“)”,则依次弹出s1栈顶的运算符,并压入s2,直到遇到左括号为止,此时将这一对括号丢弃
重复步骤2至5,直到表达式的最右边
将s1中剩余的运算符依次弹出并压入s2
依次弹出s2中的元素并输出,结果的逆序即为中缀表达式对应的后缀表达式
扫描到的元素 |
s2(栈底->栈顶) |
s1 (栈底->栈顶) |
说明 |
1 |
1 |
空 |
数字,直接入栈 |
+ |
1 |
+ |
s1为空,运算符直接入栈 |
( |
1 |
+ ( |
左括号,直接入栈 |
( |
1 |
+ ( ( |
同上 |
2 |
1 2 |
+ ( ( |
数字 |
+ |
1 2 |
+ ( ( + |
s1栈顶为左括号,运算符直接入栈 |
3 |
1 2 3 |
+ ( ( + |
数字 |
) |
1 2 3 + |
+ ( |
右括号,弹出运算符直至遇到左括号 |
× |
1 2 3 + |
+ ( × |
s1栈顶为左括号,运算符直接入栈 |
4 |
1 2 3 + 4 |
+ ( × |
数字 |
) |
1 2 3 + 4 × |
+ |
右括号,弹出运算符直至遇到左括号 |
- |
1 2 3 + 4 × + |
- |
-与+优先级相同,因此弹出+,再压入- |
5 |
1 2 3 + 4 × + 5 |
- |
数字 |
到达最右端 |
1 2 3 + 4 × + 5 - |
空 |
s1中剩余的运算符 |
代码
主要方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| public static List<String> toInfixList ( String s ) { List<String> ls=new ArrayList<String> ( ) ; int i = 0; String str; char c; do { if ( (c=s.charAt ( i ))<48 ||(c=s.charAt ( i ))>57&&(c=s.charAt ( i ))!='.' ){ ls.add ( "" + c ); i++; }else{ str = ""; while ((i<s.length ()&&((c=s.charAt ( i ))>=48&&(c=s.charAt ( i ))<=57)) ){ str += c; i++; } ls.add ( str );
}
} while (i < s.length ( ));
return ls; }
public static List<String> turnSuffixExpression ( List<String> ls ) { Stack<String> s1 = new Stack<String> ( ); List<String> s2 = new ArrayList<String> ( );
for ( String i:ls ){ if ( i.matches ( "\\d+" ) ) { s2.add ( i ); } else if ( i.equals ( "(")) { s1.push ( i ); } else if (i.equals ( ")" )){ while (!s1.peek ( ).equals ( "(" )) { s2.add ( s1.pop ( ) ); } s1.pop (); }else { while (s1.size ()!=0&& Operation.getValue ( s1.peek () )>=Operation.getValue ( i)){ s2.add ( s1.pop ( ) ); } s1.push ( i );
} } while (s1.size ( ) != 0) { s2.add ( s1.pop() ); }
return s2; }
|
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| public class PolandNotation { public static void main ( String[] args ) {
String expression = "1+((222+3)*4)-5"; List<String> list = toInfixList ( expression ); System.out.println ("中缀表达式List="+ list ); List<String> suffixlist = turnSuffixExpression ( list ); System.out.println ( "后缀表达式List="+ suffixlist ); }
public static List<String> toInfixList ( String s ) { List<String> ls=new ArrayList<String> ( ) ; int i = 0; String str; char c; do { if ( (c=s.charAt ( i ))<48 ||(c=s.charAt ( i ))>57&&(c=s.charAt ( i ))!='.' ){ ls.add ( "" + c ); i++; }else{ str = ""; while ((i<s.length ()&&((c=s.charAt ( i ))>=48&&(c=s.charAt ( i ))<=57)) ){ str += c; i++; } ls.add ( str );
}
} while (i < s.length ( ));
return ls; }
public static List<String> turnSuffixExpression ( List<String> ls ) { Stack<String> s1 = new Stack<String> ( ); List<String> s2 = new ArrayList<String> ( );
for ( String i:ls ){ if ( i.matches ( "\\d+" ) ) { s2.add ( i ); } else if ( i.equals ( "(")) { s1.push ( i ); } else if (i.equals ( ")" )){ while (!s1.peek ( ).equals ( "(" )) { s2.add ( s1.pop ( ) ); } s1.pop (); }else { while (s1.size ()!=0&& Operation.getValue ( s1.peek () )>=Operation.getValue ( i)){ s2.add ( s1.pop ( ) ); } s1.push ( i );
} } while (s1.size ( ) != 0) { s2.add ( s1.pop() ); }
return s2; }}
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: System.out.println ("运算符有误" ); } return result; } }
|
结果