算法分析:判断算术表达式中 小括号,中括号,大括号的顺序是否正确匹配。
如:{ a + [ b +( c * a) / (d * e)] } 匹配正确
{ a + [ b +( c * a) / (d * e) } 匹配错误
使用栈结构来完成,如果遇见右括号则入栈(push),遇到左边括号则出栈(pop),进行从左到右的遍历,扫描完成后,如果栈为空则,匹配成功,如果不为空则匹配失败!
代码实现如下
/**
* 定义栈操作接口
* Created by xinyang on 2016-12-13.
*/
public interface Stack {
//入栈
public void push(Object obj) throws Exception;
//出栈
public Object pop() throws Exception;
//获得栈顶元素
public Object getTop() throws Exception;
//判断栈是否为空
public boolean isEmpty();
}
/**
* StackImpl
* 栈操作实现
* @author xinyang
* @date 2016-12-13
*/
public class StackImpl implements Stack {
Object[] stack;//对象数
final int deafaultSize = 10;//默认长度
int top;//栈顶位置
int maxSize;//最大长度
public StackImpl(){
//默认方法初始化
init(deafaultSize);
}
private void init(int size){
this.maxSize = size;
top = 0 ;
stack = new Object[size];
}
/**
* 入栈
* @param obj
* @throws Exception
*/
public void push(Object obj) throws Exception {
//判断栈是否已满
if(top == maxSize){
throw new Exception("堆栈已满");
}
//入栈
stack[top] = obj;
top++;
}
//出栈
public Object pop() throws Exception {
//判断栈是否为空
if(isEmpty()){
throw new Exception("堆栈为空!");
}
//因为在入栈之后默认将top值进行了++ 所以导致不提示当前位置
top--;
return stack[top];
}
//获取栈顶元素
public Object getTop() throws Exception {
if(isEmpty()){
throw new Exception("堆栈为空");
}
//单纯获取栈顶元素
return stack[top - 1];
}
public boolean isEmpty() {
return top == 0;
}
}
/** * MainTest * 测试类 * @author xinyang * @date 2016-12-13 */ public class MainTest { public static void main(String[] args)throws Exception{ String str = "{ a + [ b +( c * a) / (d * e)] }"; String str2 = "{ a + ( a * B)+ [a -1]+ }"; signCheck(str); } public static void signCheck(String str) throws Exception{ StackImpl stack = new StackImpl() ; String[] arr = expToStringArray(str); for(int i = 0 ; i < arr.length; i++){ //如果数组中有这三种左括号元素那么直接进行入栈操作; if(arr[i].equals("(") || arr[i].equals("[") || arr[i].equals("{") ){ stack.push(arr[i]); }else if(arr[i].equals(")") && !stack.isEmpty() && stack.getTop().equals("(")){ //当我们遇到右括号时,发现当前位置栈顶的是左括号,那么此时可以出栈了 stack.pop(); }else if(arr[i].equals(")") && !stack.isEmpty() && !stack.getTop().equals("(")){ System.out.println("左右括号匹配次序不成功!"); return; } //遇到中口号时 else if(arr[i].equals("]") && !stack.isEmpty() && stack.getTop().equals("[")){ //当我们遇到右括号时,发现当前位于栈顶的是左括号则可以出栈了 stack.pop(); }else if(arr[i].equals("]") && !stack.isEmpty() && !stack.getTop().equals("[")){ System.out.println("左右口号匹配次序不成功!"); return; } //大括号匹配 else if(arr[i].equals("}") && !stack.isEmpty() && stack.getTop().equals("{")){ stack.pop(); }else if (arr[i].equals("}") && !stack.isEmpty() && !stack.getTop().equals("{")){ System.out.println("左右括号匹配次序不成功!"); return; } //右边括号多于左括号 else if(arr[i].equals(")")|| arr[i].equals("]") || arr[i].equals("}") && stack.isEmpty()){ System.out.println("右括号多于左边括号"); return; } } //经历完一趟循环后,如果堆栈不为空,那么左括号就多了 if(!stack.isEmpty()){ System.out.println("左括号多于右边括号"); }else { System.out.println("匹配正确"); } } //将字符串转化为字符串数组 public static String[] expToStringArray(String exp){ //字符串数组长度 int n = exp.length(); String[] arr = new String[n]; for(int i = 0; i < n ; i++){ arr[i] = exp.substring(i,i + 1); } return arr; } }
平时对算法的积累很少,觉得一听算法,小编脑子就开始转蒙圈了,但是静下心来实际的写一遍,其实这里的逻辑很简单。遇到问题不要复杂化,而是一步步的拆分成多个小问题,小到你能理解,并可以实现。