0

私が書いているプログラムは、ロードされたテキスト ドキュメントを読み取り、すべての (、[、および { がバランスが取れているかどうかを確認し、"" と // の後にあるものを無視するかどうかを確認する必要があります。最初の 3 つの記号はすべてバランスが取れていますが、最初に出現する行番号を確認するのに問題があります。次の記号が " または // である場合、その行を無視するか、または次の「」を押しましたが、正しい構文がわかりません。助けていただければ幸いです。これまでのところ、次のとおりです。

    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;

                    } 
                }

            }






}
4

1 に答える 1

2

)、、}またはのいずれかに遭遇した場合にのみ、スタックからポップする必要があります]。次に、閉じているものが開いているものと一致するかどうかを確認します。また、比類のない場合の動作を定義する必要があります。つまり、開口部を押し戻すか、ドロップするかです。EOFで、スタックが空かどうかを確認する必要があります。そうでなければ、あなたは何か不均衡を持っています。

于 2012-10-14T02:57:32.820 に答える