栈应用 , 判断表达式是否正确
来源:互联网 发布:python requests post 编辑:程序博客网 时间:2024/06/03 03:57
一,模拟栈
package com.zf.test;public class StackX {private int maxLengh ;private int top ;private Character[] array ;public StackX(int maxLengh){this.maxLengh = maxLengh ;this.array = new Character[maxLengh];this.top = -1 ;}public int size(){return this.top ;}public void put(char entry){if(isFull())throw new ArrayIndexOutOfBoundsException("array is fully!");array[++top] = entry ;} public char pop(){if(isEmpty()) throw new ArrayIndexOutOfBoundsException("array is empty!");return array[top--];}public boolean isEmpty(){return top == -1 ;}public boolean isFull(){return top == maxLengh - 1;}}
2,应用
package com.zf.test;public class ExpressionMatcher {private final static int right = 0 ;private final static int left = 1 ; public static boolean matcher(String expStr){StackX sx = new StackX(100);for (int i = 0; i < expStr.length(); i++) { char c = expStr.charAt(i);if(obtainFh(c) == left){sx.put(c);}else if(obtainFh(c) == right){if(sx.isEmpty())throw new RuntimeException("没找到匹配项:" + c);char l = sx.pop(); if(!matchers(l, c)){ throw new RuntimeException(l + " 与 " + c + "不匹配");}}}return sx.isEmpty();}private static int obtainFh(char c){switch(c){case '(' : case '[' :case '{' :return left ;case ')' :case ']' :case '}' :return right ;default : return -1 ; }} private static boolean matchers(char left , char right){if((left == '(' && right == ')')|| (left == '[' && right == ']')|| (left == '{' && right == '}') ) return true ;return false;} public static void main(String[] args) {System.out.println(matcher( "212(1(21)1){4(5)4{5}}") );}}
- 栈应用 , 判断表达式是否正确
- 判断一个表达式是否正确
- “顺序栈”判断表达式中的括号是否正确配对
- 正则表达式判断IP格式是否正确
- 正则表达式判断邮箱、身份证..是否正确
- 正则表达式判断邮箱、身份证..是否正确
- 正则表达式判断邮箱格式是否正确
- 正则表达式判断手机号码是否正确
- 判断mail格式是否正确--正则表达式
- C#正则表达式判断输入日期格式是否正确
- C#正则表达式判断输入日期格式是否正确
- C#正则表达式判断输入日期格式是否正确
- 用正则表达式判断邮箱格式是否正确
- 用正则表达式判断运算数格式是否正确
- C#正则表达式判断输入日期格式是否正确
- ios正则表达式判断邮箱格式是否正确
- jquery 正则表达式判断是否为正确邮箱格式
- android 正则表达式 判断邮箱格式是否正确
- PHP中echo、print、print_r、printf、sprintf
- 二叉树的遍历
- html 5获取GPS位置,Google地图显示
- MyEclipse6.0快捷键大全
- Servlet页面跳转实现方法的区别
- 栈应用 , 判断表达式是否正确
- 黑客秘密:12种类型黑客 你见过哪种
- C#WinForm程序常用函数设计
- 工资新规定,未来的八种人将会被淘汰
- GT-S7230E的手机阻止了某人的短信,如何恢复的问题
- 你幸福吗?
- Java面试题大全
- apache整合tomcat
- sql面试题目汇总(1)