delimitermatch.java

来自「符号匹配」· Java 代码 · 共 59 行

JAVA
59
字号
import java.io.*;
import java.util.*;
public class DelimiterMatch {
    Stack stack = new Stack();
    public void Matching()throws IOException{
        String s = " ";char temp=' ',next=' ';
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            s = br.readLine();
        }catch(IOException e){
        }
        for(int i=0;i<=s.length();i++){
            if(i<s.length()){
                temp = s.charAt(i);
                if (temp == '(' || temp == '[') {
                    stack.push(i);
                    stack.push(temp);
                }
                if (temp == ')' && stack.isEmpty())
                    System.out.println("不匹配信息为:  ) in colum " + i +
                                       " is not matching");
                if (temp == ']' && stack.isEmpty())
                    System.out.println("不匹配信息为:  ] in colum " + i +
                                       " is not matching");
                if (temp == ')' && !stack.isEmpty()) {
                    next = (Character) stack.pop();
                    int m = (Integer) stack.pop();
                    if (next == '(')
                        continue;
                    else
                        System.out.println("不匹配信息为: " + next + " in colum " + m +
                                           " is not matching with ) in colum " +
                                           i);
                }
                if (temp == ']' && !stack.isEmpty()) {
                    next = (Character) stack.pop();
                    int m = (Integer) stack.pop();
                    if (next == '[')
                        continue;
                    else
                        System.out.println("不匹配信息为: " + next + " in colum " + m +
                                           " is not matching with ] in colum " +i);
                }
            }
            if(i==s.length()&&!stack.isEmpty())
                for (; !stack.isEmpty(); ) {
                    next = (Character) stack.pop();
                    int m = (Integer) stack.pop();
                    System.out.println("不匹配信息为: " + next + " in colum " + m +
                                       " is not matching ");
                }
        }
        }
    public static void main(String[] args)throws IOException {
        DelimiterMatch delimitermatch = new DelimiterMatch();
        delimitermatch.Matching();
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?