利用栈结构进行括号的匹配
2014-04-12 17:12
399 查看
package none003分隔符匹配; //利用栈结构进行括号的匹配 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BracketApp { /** * @param args */ public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub Stack3 s3=new Stack3(60); System.out.println("please input sentences you want to test the bracket:"); System.out.flush(); String sin=getInput(); int flag=0; for(int i=0;i<sin.length();i++){ char k=sin.charAt(i); if((k=='['||k=='{'||k=='(')&&flag==0){ s3.push(k); flag=1; } if((k==')'||k=='}'||k==']')&&flag==0){ System.out.println("*******括号匹配不正确!!!**********"); System.out.print(i); break; } if(flag==1&&(k=='['||k=='{'||k=='(')){ s3.push(k); } if(flag==1&&(k==')'||k=='}'||k==']')){ if(s3.peek()=='('&&k==')'){ s3.pop(); }else if(s3.peek()=='{'&&k=='}'){ s3.pop(); }else if(s3.peek()=='['&&k==']'){ s3.pop(); }else{ System.out.println("*******括号匹配不正确!!!**********"); System.out.print(i); break;} } } } public static String getInput() throws IOException{ InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); return br.readLine(); } } class Stack3{ int items; char[] arrayStack; int s; Stack3(int max){ s=max; arrayStack=new char[s]; items=-1; } public void push(char value){ if(items==s-1) System.out.println("栈已经等于最大值了。"); else arrayStack[++items]=value; } public char pop(){ return arrayStack[items--]; } public boolean isEmpty(){ return items==-1; } public char peek(){ if(items==-1) return 'F'; else return arrayStack[items]; } public boolean isFull(){ return items==s-1; } }
相关文章推荐
- 考研数据结构与算法之堆栈的使用(三)利用堆栈匹配括号
- 利用栈结构判断括号匹配问题
- C语言数据结构之利用栈进行括号匹配的检验
- 数据结构之---C语言实现括号匹配(栈实现)
- 数据结构课程设计:括号匹配问题(实现检验匹配并输出不匹配的位置)
- SDUT 2134 数据结构实验之栈四:括号匹配
- 数据结构之括号匹配
- [2134]数据结构实验之栈四:括号匹配 sdutOJ
- 利用栈判断括号是否匹配
- C++,利用链式栈实现括号匹配,界面友好,操作方便,运行流畅
- poj 1141 动态规划进行括号匹配
- 利用js 进行输入框自动匹配字符的小例子
- 利用Rational Rose进行C++代码和数据库结构分析
- 数据结构——括号匹配
- 利用特征点(Brief,ORB,SIFT)进行图像匹配,模板匹配
- OpenCv学习之:利用vs2008的IDE工具对IplImage、Mat、CvMat结构进行解析解析
- 数据结构——顺序栈及其应用(括号匹配)
- 【数据结构——栈】括号匹配
- as3.0利用DES和AES对数据进行加密和解密并且与php端加密匹配!
- 数据结构(三)栈结构模拟编译器判断括号是否匹配