括號匹配算法求解(用棧實現)


1.括號匹配算法

        //括號匹配算法
        public void pipei()throws Exception{
            char temp,ch;
            int match;    //記錄匹配結果
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            ch=(char) br.read();        //輸入一個字符
            while(ch!='0'){
                if(getTop()==-1){
                    push(ch);
                }else{
                    temp=pop();        //取出棧頂元素
                    match=0;    //判斷是否匹配(默認不匹配)
                    if(temp=='('&&ch==')')
                        match=1;
                    if(temp=='['&&ch==']')
                        match=1;
                    if(temp=='{'&&ch=='}')
                        match=1;
                    if(temp=='<'&&ch=='>')
                        match=1;
                    if(match==0){    //如果不匹配
                        push(temp);    //將原棧頂元素重新入棧
                        push(ch);    //將輸入的括號字符入棧
                    }
                }
                ch=(char) br.read();    //輸入下一個字符
            }
            if(isEmpty()){
                System.out.println("輸入的括號完全匹配!");
            }else{
                System.out.println("輸入的括號不匹配,請檢查!");
            }
        }

2.括號匹配求解示例

package com.cn.datastruct;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class KuoHaoPiPei {
    static class Stack{
        char[] data;  //存放數據
        int MaxSize;    //最大容量
        int top;        //棧頂指針
        //構造方法
        public Stack(int MaxSize){
            this.MaxSize=MaxSize;
            data = new char[MaxSize];
            top = -1;
        }

        public int getMaxSize() {
            return MaxSize;
        }

        public int getTop() {
            return top;
        }

        public boolean isEmpty(){
            return top==-1;
        }
        
        public boolean isFull(){
            return top+1==MaxSize;
        }
        //入棧
        public boolean push(char data){
            if(isFull()){
                System.out.println("棧已滿!");
                return false;
            }
            this.data[++top]=data;
            return true;
        }
        //出棧
        public char pop() throws Exception{
            if(isEmpty()){
                throw new Exception("棧已空!");
            }
            return this.data[top--];
        }        
        //獲得棧頂元素
        public char peek(){
            return this.data[getTop()];
        }
        
        //括號匹配算法
        public void pipei()throws Exception{
            char temp,ch;
            int match;    //記錄匹配結果
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            ch=(char) br.read();        //輸入一個字符
            while(ch!='0'){
                if(getTop()==-1){
                    push(ch);
                }else{
                    temp=pop();        //取出棧頂元素
                    match=0;    //判斷是否匹配(默認不匹配)
                    if(temp=='('&&ch==')')
                        match=1;
                    if(temp=='['&&ch==']')
                        match=1;
                    if(temp=='{'&&ch=='}')
                        match=1;
                    if(temp=='<'&&ch=='>')
                        match=1;
                    if(match==0){    //如果不匹配
                        push(temp);    //將原棧頂元素重新入棧
                        push(ch);    //將輸入的括號字符入棧
                    }
                }
                ch=(char) br.read();    //輸入下一個字符
            }
            if(isEmpty()){
                System.out.println("輸入的括號完全匹配!");
            }else{
                System.out.println("輸入的括號不匹配,請檢查!");
            }
        }
    }
    
    public static void main(String[] args) throws Exception {
        String go;
        Scanner input = new Scanner(System.in);
        Stack stack = new Stack(20);
        System.out.println("括號匹配問題!");
        do{
            System.out.println("請輸入一組括號的組合,以0表示結束。支持的括號包括:{},(),[],<>。");
            stack.pipei();        //匹配算法
            System.out.print("\n繼續匹配嗎(y/n)?");
            go=input.next();
        }while(go.equalsIgnoreCase("y"));
        System.out.println("匹配結束!");
    }

}

程序運行結果如下:

括號匹配問題!
請輸入一組括號的組合,以0表示結束。支持的括號包括:{},(),[],<>。
({[]})<>0
輸入的括號完全匹配!

繼續匹配嗎(y/n)?y
請輸入一組括號的組合,以0表示結束。支持的括號包括:{},(),[],<>。
({])0
輸入的括號不匹配,請檢查!

繼續匹配嗎(y/n)?n
匹配結束!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM