public void Run() { string[] testArray = null; // testArray =new string[] { "(", ")", "[", "{", "}" }; // testArray = new string[] { "(", ")", "]", "{", "}"}; testArray = new string[] { "(", ")", "[", "}" }; string[] openElement = { "(", "[", "{" }; string[] closeElement = { ")", "]", "}" }; IStack <string> stack = new StackByArray <StringNode, string>(); for (int i = 0; i < testArray.Length; i++) { string symbol = testArray[i]; //判断是开放元素还是封闭元素 if (openElement.Contains(symbol)) { stack.Push(symbol); } else { if (stack.IsEmpty()) { System.Console.WriteLine("错误,封闭元素遇上栈空"); break; } else { string left = stack.Pop().Value; if (IsCouple(left, symbol)) { } else { System.Console.WriteLine("错误,开放元素对不上"); break; } } } } if (!stack.IsEmpty()) { System.Console.WriteLine("错误,文件读完栈不为空"); } }
public void GetTopTest() { Assert.IsTrue(stack.IsEmpty()); stack.Push(3); stack.Push(4); stack.Push(5); Assert.AreEqual(3, stack.GetLength()); Assert.AreEqual(5, stack.GetTop()); Assert.AreEqual(3, stack.GetLength()); }