Java堆栈排序符号平衡

时间:2012-10-14 02:40:47

标签: java sorting stack

我写的程序必须读取一个加载的文本文档并检查是否所有(,[和{均衡以及忽略“之间的任何内容”和之后//。我到目前为止所检查的内容看看前三个符号是否都是平衡的,但是我在检查第一次出现的行号时遇到了麻烦。我知道如果下一个符号是“或//,我必须写,我忽略了或直到我点击下一个“,但我不确定正确的语法。任何帮助将不胜感激。这是我到目前为止:

    import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.util.*;
import java.io.*;

import javax.swing.filechooser.*;
import javax.swing.text.BadLocationException;

public class Sorting extends JFrame implements ActionListener{



    private Stack<Character> symbolStack;



    JTextArea opentextArea;
    JTextArea placetextArea;
    JMenuItem open;
    final JFileChooser fc = new JFileChooser();

    public static void main(String [] args)
    {
        Sorting gui = new Sorting();




    }




    public Sorting()
     {




        JFrame frame = new JFrame();
        setLayout(new GridLayout(1,2));
        setTitle("Stack Sort");

        JMenuBar menuBar = new JMenuBar();
        JMenu menuItems = new JMenu("File");
        open = new JMenuItem("Open");
        open.addActionListener(this);
        JMenuItem reset = new JMenuItem("Reset");


        reset.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e)
                    {
                        opentextArea.setText("");
                        placetextArea.setText("");
                    }   
                }
            );

        menuItems.add(open);
        menuItems.add(reset);

        menuBar.add(menuItems);
        setJMenuBar(menuBar);
        opentextArea = new JTextArea();
        placetextArea = new JTextArea();
        add(opentextArea);
        add(placetextArea);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   pack();
   setVisible(true);


     }




    public void actionPerformed(ActionEvent event)
    {

        int returnVal = fc.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) 
        {

            File file = fc.getSelectedFile();
            FileInputStream is = null;
            try
            {
                is = new FileInputStream(file);
            }
            catch (FileNotFoundException e)
            {
                System.out.println("Exception: " + e.toString());
            }


            byte [] nextChar = new byte[2];

            try
            {

                int value = is.read(nextChar);
                int num = 1;
                opentextArea.append(num + ":" );


                while (value != -1)
                {
                    String newString = new String(nextChar);
                    if (nextChar[0] == '\n' || nextChar[0] == '\r')
                    {
                        num++;
                        opentextArea.append(newString + num + ":" );

                    }
                    else 
                        opentextArea.append(newString);


                    value = is.read(nextChar);

                }


            }
            catch (IOException e)
            {
                System.out.println("Exception: " + e.toString());
            }


            Stack<Character> stack = new Stack<Character>();
            String s = opentextArea.getText();
            int index = 0;
            int numline = 1;
            while(index < s.length()) {
                char ch = s.charAt(index);
                if (ch == '\n' || ch == '\r')
                    {
                        numline++;
                        index++;
                    }
                else
                if(ch == '{' || ch == '[' || ch == '(')
                    {
                        stack.push(ch);
                        index++;
                    } 
                else {
                    if(stack.empty()) 
                    {
                        index++;
                        //placetextArea.append("No balance characters found.");
                        continue;
                    }

                    char ch1 = stack.pop();

                    if(ch1 == '{' && ch == '}' || ch1 == '[' && ch == ']' || ch1 == '(' && ch == ')') 
                    {
                        placetextArea.append(ch1 + " at line " +numline + " matches up with " + ch + " at line " + numline + "\n");
                    } 
                    else
                        if(ch1 == '{' && ch != '}' || ch1 == '[' && ch != ']' || ch1 == '(' && ch != ')')
                        {
                            placetextArea.append("error unmatched " + ch1 + "at line " +numline);
                            break;
                        } 
                    }

                }






    }
}
}

这是我编辑的代码:

    Stack<Character> stack = new Stack<Character>();
        String s = opentextArea.getText();
        int index = 0;
        int numline = 1;
        while(index < s.length()) {
            char ch = s.charAt(index);
            if (ch == '\n' || ch == '\r')
                {
                    numline++;
                    index++;
                }
            else
            if(ch == '{' || ch == '[' || ch == '(')
                {
                    stack.push(ch);
                    index++;
                } 
            else {
                if(stack.empty()) 
                {
                    index++;
                    //placetextArea.append("No balance characters found.");
                    continue;
                }
                // pop an item from stack
                if(ch == '}' || ch == ']' || ch == ')')
                {
                char ch1 = stack.pop();
                // check if it's a matching pair
                if(ch1 == '{' && ch == '}' || ch1 == '[' && ch == ']' || ch1 == '(' && ch == ')') 
                {
                    placetextArea.append(ch1 + " at line " +numline + " matches up with " + ch + " at line " + numline + "\n");
                } 
                else
                    if(ch1 == '{' && ch != '}' || ch1 == '[' && ch != ']' || ch1 == '(' && ch != ')')
                    {
                        placetextArea.append("error unmatched " + ch1 + "at line " +numline);
                        break;

                    } 
                }

            }






}

1 个答案:

答案 0 :(得分:2)

只有在遇到)}]之一时才需要从堆叠中弹出。然后检查关闭的一个是否与开头匹配。此外,您还需要在不匹配时定义行为:向后推开口或放下它。在EOF,你应该检查堆栈是否为空。否则你会有不平衡的东西。