簡體   English   中英

Java堆棧排序符號平衡

[英]Java Stack Sort Symbol Balancing

我寫的程序必須讀取一個加載的文本文檔並檢查是否所有(,[,和{是平衡的以及忽略“之間的任何內容”和之后//。我到目前為止檢查是否前三個符號都是平衡的,但是我在檢查第一次出現的行號時遇到了麻煩。我知道如果下一個符號是“或//,我忽略該行或直到我點擊了下一個“,但我不確定正確的語法。任何幫助都會受到贊賞。這是我到目前為止所擁有的:

    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;

                    } 
                }

            }






}

只有在遇到)}]之一時才需要從堆棧中彈出。 然后檢查關閉的一個是否與開頭匹配。 此外,您還需要在不匹配時定義行為:向后推開口或放下它。 在EOF,你應該檢查堆棧是否為空。 否則你會有不平衡的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM