括号匹配是计算机程序设计经常遇到的问题。为了简化问题,假设表达式只允许有2种括号:(),【】。 匹配格式为:(()【】)和【()【()】【】】视为正确匹配。具体实现部分见下图代码:衷心希望网友提出改进意见,大家共同进步。

 1SeqStack<char> list=new SeqStack<char>(10);
 2           
 3            //栈的使用:括号匹配
 4            char[] str = new char[]{'[',']','(',')'};
 5            int len = str.Length;
 6            for (int i = 0; i < len; i++)
 7            
 8               //如果栈为空,将括号入栈
 9                if (list.IsEmpty())
10                {
11                    list.Push(str[i]);
12                }

13                else   //如果括号与栈顶的括号匹配,将栈顶括号出栈
14                {
15                    if (((list.GetTop() == '('&& (str[i] == ')')) || ((list.GetTop() == '['&& (str[i] == ']')))
16                    {
17                       
18                        Console.WriteLine(list.Pop());
19                    }

20                    else  //如果括号与栈顶的括号不匹配,将括号入栈
21                    {
22                        list.Push(str[i]);
23                    }

24             
25                }

26        
27            }

28            //如果括号序列为空,栈为空则括号匹配,否则不匹配
29            if (list.IsEmpty())
30            {
31                Console.WriteLine("匹配!");
32            }

33            else
34            {
35                Console.WriteLine("不匹配!");
36            }

37 

 

完整代码如下:

 

  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4
  5namespace 栈和队列
  6{
  7    class Program
  8    {
  9        public interface IStack<T>
 10        
 11           //求栈的长度
 12             int GetLength();
 13            //判断是否为空
 14            bool IsEmpty(); 
 15            //判断是否已满
 16            bool IsFull();
 17            //清空操作
 18            void Clear();
 19            //入栈
 20            void Push(T item);
 21            //出栈
 22            T Pop();
 23            //取栈顶元素
 24            T GetTop();
 25           
 26            
 27
 28        }

 29        public class SeqStack<T> : IStack<T>
 30        
 31           //栈的容量
 32            private int maxsize;
 33           //存储栈中的数据
 34            private T[] data;
 35           //指定栈的栈顶
 36            private int top;
 37
 38            //索引器
 39            public T this[int index]
 40            {
 41                get
 42                
 43                   return data[index];
 44                }

 45                set
 46                {
 47                    data[index] = value;
 48                }

 49            }

 50
 51            //容量属性
 52            public int Maxsize
 53            {
 54                get
 55                {
 56                    return maxsize;
 57                }

 58                set
 59                {
 60                    maxsize = value;
 61                }

 62            }

 63
 64            //栈顶属性
 65            public int Top
 66            {
 67                get
 68                {
 69                    return top;
 70                }

 71            }

 72
 73            //构造器
 74            public SeqStack(int size)
 75            {
 76                data = new T[size];
 77                maxsize = size;
 78                top = -1;
 79            }

 80
 81            //求栈的长度
 82            public int GetLength()
 83            {
 84                return top + 1;
 85            }

 86
 87            //清空栈
 88            public void Clear()
 89            {
 90                top = -1;
 91            }

 92
 93            //判断是否为空
 94            public bool IsEmpty()
 95            {
 96                if (top == -1)
 97                {
 98                    return true;
 99                }

100                else
101                {
102                    return false;
103                }

104            }

105
106            //判断是否以满
107            public bool IsFull()
108            {
109                if (top == maxsize - 1)
110                {
111                    return true;
112                }

113                else
114                {
115                    return false;
116                }

117            }

118
119            //入栈
120            public void Push(T item)
121            {
122                if (IsFull())
123                {
124                    Console.WriteLine("栈满啦,要清空啦!");
125                    return;
126                }

127                data[++top] = item;
128                
129            }

130            //出栈
131            public T Pop()
132            
133               T tmp=default(T);
134               if (IsEmpty())
135               {
136                   Console.WriteLine("栈已空!");
137                   return tmp;
138               }

139               tmp = data[top];
140               --top;
141               return tmp;
142            }

143            //获取栈顶数据元素
144            public T GetTop()
145            {
146                if (IsEmpty())
147                {
148                    Console.WriteLine("表已空!");
149                    return default(T);
150                }

151                return data[top];
152            }

153            
154            
155
156        }

157
158        static void Main(string[] args)
159        {
160           
161            SeqStack<char> list=new SeqStack<char>(10);
162           
163            //栈的使用:括号匹配
164            char[] str = new char[]{'[',']','(',')'};
165            int len = str.Length;
166            for (int i = 0; i < len; i++)
167            
168               //如果栈为空,将括号入栈
169                if (list.IsEmpty())
170                {
171                    list.Push(str[i]);
172                }

173                else   //如果括号与栈顶的括号匹配,将栈顶括号出栈
174                {
175                    if (((list.GetTop() == '('&& (str[i] == ')')) || ((list.GetTop() == '['&& (str[i] == ']')))
176                    {
177                       
178                        Console.WriteLine(list.Pop());
179                    }

180                    else  //如果括号与栈顶的括号不匹配,将括号入栈
181                    {
182                        list.Push(str[i]);
183                    }

184             
185                }

186        
187            }

188            //如果括号序列为空,栈为空则括号匹配,否则不匹配
189            if (list.IsEmpty())
190            {
191                Console.WriteLine("匹配!");
192            }

193            else
194            {
195                Console.WriteLine("不匹配!");
196            }

197 
198          }

199
200        }

201    }

202
203
posted on 2009-03-10 11:57  甲乙丙丁  阅读(2563)  评论(0编辑  收藏  举报