使用Stack来检查多行代码的平衡

时间:2014-10-22 20:45:39

标签: java stack

我正在尝试编写代码来检查任何文件(包含多行)中{},()和[]的余额。如果文件不平衡,则报告发生错误的位置(行和字符编号) 这是我的代码。

import java.io.*;
import java.util.Stack;
public class CppJavaParser {
    public static class Symbol{
        public char symbol;
        public int lineFound;
        public int colFound;

        public Symbol( char symb, int line, int charIndex){
            symbol = symb;
            lineFound = line;
            colFound = charIndex;
        }
    }

    public static Stack<Symbol> symbolStack = new Stack<Symbol>();

    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream("HelloWorld.java"))));
        String line;
        int lineNum = 1;
        while( (line = br.readLine()) != null){
            balanceChecker(line, lineNum);
            lineNum++;
        }
        br.close();
        if (symbolStack.isEmpty()) {
            System.out.println("Success");
        }
        else
        {
            System.out.println("Failure");
        }
    }

    public static void balanceChecker(String line, int lineNum){
        int charNum = 1;
        Symbol top;
        for(char chr : line.toCharArray()){
            switch(chr) {
            case '{':
            case '(':
            case '[':
                symbolStack.push(new Symbol(chr, lineNum, charNum));
                break;

            case ']':
                if (symbolStack.isEmpty())
                    System.err.println("Unmatched parentheses ");
                else
                {
                    top = symbolStack.peek();
                    if (top.symbol != '[') {
                        System.err.println("Fail at line: " + top.lineFound
                                + " Column: " + top.colFound);
                    }else{
                        symbolStack.pop();
                    }
                }
                break;
            case ')':
                if (symbolStack.isEmpty())
                    System.err.println("Unmatched parentheses ");
                else {
                    top = symbolStack.peek();
                    if (top.symbol != '(') {
                        System.err.println("Fail at line: " + top.lineFound
                                + " Column: " + top.colFound);
                    }else{
                        symbolStack.pop();
                    }
                }
                break;
            case '}':
                if (symbolStack.isEmpty())
                    System.err.println("Unmatched parentheses ");
                else {
                    top = symbolStack.peek();
                    if ((top.symbol != '{')) {
                        System.err.println("Fail at line: " + top.lineFound
                                + " Column: " + top.colFound);
                    }else{
                        symbolStack.pop();
                    }
                }
                break;
            }
            charNum++;
        }
    }
}

这是我正在使用的测试文件:

public class HelloWorld {

public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}
}//This is where unmatched parenthesis error occurs

我的程序打印到控制台后面:

Unmatched parentheses 
Success

怎么打印成功?它不应该打印出来。

2 个答案:

答案 0 :(得分:2)

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new DataInputStream(new FileInputStream("HelloWorld.java")))); // Reading
                                                                                // the
                                                                                // test
                                                                                // file
        String line;
        int lineNum = 1;
        while ((line = br.readLine()) != null) {
            balanceChecker(line, lineNum);
            lineNum++;
        }
        br.close();
        if (symbolStack.isEmpty()) {
            System.out.println("Success");
        }
        else
        {
            System.out.println("Failure");
        }
    }

    public static void balanceChecker(String line, int lineNum) {
        int charNum = 1;
        Symbol top;
        for (char chr : line.toCharArray()) {
            switch (chr) {
            case '{':
            case '(':
            case '[':
                symbolStack.push(new Symbol(chr, lineNum, charNum));
                break;

            case ']':
                if (symbolStack.isEmpty())
                    System.err.println("Unmatched parentheses ");
                else
                {
                    top = symbolStack.pop();
                    if (top.symbol != '[') {
                        System.err.println("Fail at line: " + top.lineFound
                                + " Column: " + top.colFound);
                    }
                }
                break;
            case ')':
                if (symbolStack.isEmpty())
                    System.err.println("Unmatched parentheses ");
                else {
                    top = symbolStack.pop();

                    if (top.symbol != '(') {
                        System.err.println("Fail at line: " + top.lineFound
                                + " Column: " + top.colFound);
                    }
                }
                break;
            case '}':
                if (symbolStack.isEmpty())
                    System.err.println("Unmatched parentheses ");
                else {
                    top = symbolStack.pop();

                    if ((top.symbol != '{')) {
                        System.err.println("Fail at line: " + top.lineFound
                                + " Column: " + top.colFound);
                    }
                }
                break;
            }
            charNum++;
        }
    }
}

答案 1 :(得分:2)

检查您的代码行:  if((symbolStack.isEmpty())||(top.symbol!=&#39; [&#39;)){===&gt;打印失败

所以你检查堆栈是否为空(在弹出之后),如果堆栈上有一个与你的close标签匹配的正确符号条目,那么堆栈是emty并且它将打印失败

你应该在弹出之前检查一个空堆栈然后如果空打印错误,如果没有弹出并检查符号....