括号匹配、进制转换与堆栈
定义堆栈接口:
?
package stack;public class Client {public static void main(String[] args) {//baseConversion(8);System.out.println(bracketMatch("{[wf(wf)]}"));}// 进制转换算法public static void baseConversion(int val) {Stack s = new StackSLinked();while (val > 0) {s.push(val % 8 + "");val = val / 8;}while (!s.isEmpty())try {System.out.print((String) s.pop());} catch (StackEmptyException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// 括号匹配算法public static boolean bracketMatch(String str) {Stack s = new StackSLinked();for (int i = 0; i < str.length(); i++) {char c = str.charAt(i);switch (c) {case '{':case '[':case '(':s.push(Integer.valueOf(c));break;case '}':try {if (!s.isEmpty() && ((Integer) s.pop()).intValue() == '{')break;elsereturn false;} catch (StackEmptyException e) {// TODO Auto-generated catch blocke.printStackTrace();}case ']':try {if (!s.isEmpty() && ((Integer) s.pop()).intValue() == '[')break;elsereturn false;} catch (StackEmptyException e) {// TODO Auto-generated catch blocke.printStackTrace();}case ')':try {if (!s.isEmpty() && ((Integer) s.pop()).intValue() == '(')break;elsereturn false;} catch (StackEmptyException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}if (s.isEmpty())return true;elsereturn false;}}?