栈的应用(一)-括号匹配
2015-05-07 10:46
295 查看
package chapter4;
import java.util.Scanner;
public class Bracket {
/**
* @括号匹配a{b[c(d)e]f}
*/
public static void main(String[] args) {
String input;
while(true){
Scanner sc = new Scanner(System.in);
input = sc.nextLine();
if(input.equals(""))
break;
BraketChecker theChecker = new BraketChecker(input);
theChecker.check();
}
}
}
class StackII{
private int maxSize;
private char[] stackArray;
private int top;
public StackII(int maxSize){
this.maxSize = maxSize;
stackArray = new char[this.maxSize];
top = -1;
}
public void push(char value){
stackArray[++top] = value;
}
public char pop(){
return stackArray[top--];
}
public char peek(){
return stackArray[top];
}
public boolean isEmpty(){
return (top==-1);
}
}
class BraketChecker{
private String input;
public BraketChecker(String in){
input = in;
}
public void check(){
int maxSize = input.length();
StackII theStack = new StackII(maxSize);
for(int i=0;i<maxSize;i++){
char ch = input.charAt(i);
switch(ch){
case '{':
case '(':
case '[':
theStack.push(ch); break;
case '}':
case ')':
case ']':
if(!theStack.isEmpty()){
char temp = theStack.pop();
if( (ch=='{' && temp!='}') || (ch=='(' && temp!=')') || (ch=='[' && temp!=']') )
System.out.println("error:"+ch+"at"+i);
}
else
System.out.println("error:"+ch+"at"+i); break;
default:break;
}
}
if(!theStack.isEmpty())
System.out.println("error:missing right delimiter");
}
}
import java.util.Scanner;
public class Bracket {
/**
* @括号匹配a{b[c(d)e]f}
*/
public static void main(String[] args) {
String input;
while(true){
Scanner sc = new Scanner(System.in);
input = sc.nextLine();
if(input.equals(""))
break;
BraketChecker theChecker = new BraketChecker(input);
theChecker.check();
}
}
}
class StackII{
private int maxSize;
private char[] stackArray;
private int top;
public StackII(int maxSize){
this.maxSize = maxSize;
stackArray = new char[this.maxSize];
top = -1;
}
public void push(char value){
stackArray[++top] = value;
}
public char pop(){
return stackArray[top--];
}
public char peek(){
return stackArray[top];
}
public boolean isEmpty(){
return (top==-1);
}
}
class BraketChecker{
private String input;
public BraketChecker(String in){
input = in;
}
public void check(){
int maxSize = input.length();
StackII theStack = new StackII(maxSize);
for(int i=0;i<maxSize;i++){
char ch = input.charAt(i);
switch(ch){
case '{':
case '(':
case '[':
theStack.push(ch); break;
case '}':
case ')':
case ']':
if(!theStack.isEmpty()){
char temp = theStack.pop();
if( (ch=='{' && temp!='}') || (ch=='(' && temp!=')') || (ch=='[' && temp!=']') )
System.out.println("error:"+ch+"at"+i);
}
else
System.out.println("error:"+ch+"at"+i); break;
default:break;
}
}
if(!theStack.isEmpty())
System.out.println("error:missing right delimiter");
}
}
相关文章推荐
- 【学习点滴-数据结构-栈&队列】 栈的应用之二:括号匹配的检测
- c语言栈的应用之括号匹配问题
- 数据结构笔记之——括号匹配(栈的应用之一)(SDOJ 2134)
- 栈应用一(括号匹配问题)
- 栈的应用-括号的匹配
- 栈操作和应用—括号匹配
- 数据结构-C语言 栈的简单应用——括号匹配
- 栈的应用:括号匹配
- 九度OJ 1153 括号匹配问题(栈的应用)
- 栈的应用举例:括号的匹配
- 栈的应用--括号匹配的检验
- 栈的应用之括号的匹配
- c语言:括号匹配检测(栈的应用)
- <数据结构>栈的应用一:括号匹配检测
- 3.2栈的应用举例----数制转换、括号匹配及行编辑
- 20.Valid Parentheses 栈的应用:括号匹配
- 第三章:顺序栈及其应用之二---括号匹配
- 括号匹配问题-栈的应用
- 栈的应用之括号匹配
- 栈的应用---括号匹配问题