[英]Testing if Parenthesis are balanced, (a{[]b({})}c)[] returns false, but should be true
這段代碼的重點是測試每組字符串中的括號是否平衡。 有 12 個測試必須全部通過,目前,我有 11 個在工作,但是最后一個不起作用。 目前,我有三個靜態布爾值,它們闡明了括號的分類,以及它們是否匹配。
private static bool IsOpeningParenthesis(char c)
{
return c == '(' || c == '[' || c == '{';
}
private static bool IsClosingParenthesis(char c)
{
return c == ')' || c == ']' || c == '}';
}
private static bool Matches(char a, char b)
{
return (a == '(' && b == ')') || (a == '[' && b == ']') ||
(a == '{' && b == '}');
}
下面,我有實際檢查它們是否匹配的 bool,這就是我的錯誤所在。
public static bool IsBalanced(string s)
{
Stack<char> myStack = new Stack<char>();
foreach (char c in s)
{
if (IsOpeningParenthesis(c))
{
myStack.Push(c);
}
if (IsClosingParenthesis(c))
{
if (myStack.Count == 0) //takes care of closing parenthesis before adding char d
{
return false;
}
char d = myStack.Pop();
if (c == d)
{
return true;
}
else
{
myStack.Push(d);
return false;
}
}
}
if(myStack.Count == 0)
{
return true;
}
else
{
return false;
}
}
最后一點代碼,這些是正在檢查的所有測試。 我目前正在為測試編號 9 / TestFLongMatch 苦苦掙扎。
public void TestFLongMatch()
{
Assert.That(ParenthesisMatcher.IsBalanced("(a{[]b({})}c)[]"), Is.True);
}
以下是包含所有測試的完整文件
/* ParenthesisMatcherTests.cs
* Author: Rod Howell
*/
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ksu.Cis300.Parentheses;
namespace Ksu.Cis300.Parentheses.Tests
{
/// <summary>
/// A unit test class for the class library Ksu.Cis300.Parentheses.
/// </summary>
[TestFixture]
public class ParenthesisMatcherTests
{
/// <summary>
/// Checks the empty string, which is balanced.
/// </summary>
[Test, Timeout(1000)]
public void TestAEmptyString()
{
Assert.That(ParenthesisMatcher.IsBalanced(""), Is.True);
}
/// <summary>
/// Checks the string "abcdefg", which is balanced.
/// </summary>
[Test, Timeout(1000)]
public void TestBNoParentheses()
{
Assert.That(ParenthesisMatcher.IsBalanced("abcdefg"), Is.True);
}
/// <summary>
/// Checks the string "[", which is not balanced.
/// </summary>
[Test, Timeout(1000)]
public void TestCOpeningParenthesis()
{
Assert.That(ParenthesisMatcher.IsBalanced("["), Is.False);
}
/// <summary>
/// Checks the string ")", which is not balanced.
/// </summary>
[Test, Timeout(1000)]
public void TestDClosingParenthesis()
{
Assert.That(ParenthesisMatcher.IsBalanced(")"), Is.False);
}
/// <summary>
/// Tests the string "{{}", which is not balanced.
/// </summary>
[Test, Timeout(1000)]
public void TestEMissingClose()
{
Assert.That(ParenthesisMatcher.IsBalanced("{{}"), Is.False);
}
/// <summary>
/// Tests the string "[[]", which is not balanced.
/// </summary>
[Test, Timeout(1000)]
public void TestEExtraClose()
{
Assert.That(ParenthesisMatcher.IsBalanced("[]]"), Is.False);
}
/// <summary>
/// Tests the string "[}", which is not balanced.
/// </summary>
[Test, Timeout(1000)]
public void TestEMismatch()
{
Assert.That(ParenthesisMatcher.IsBalanced("[}"), Is.False);
}
/// <summary>
/// Tests the string "[}]", which is not balanced.
/// </summary>
[Test, Timeout(1000)]
public void TestEMismatch2()
{
Assert.That(ParenthesisMatcher.IsBalanced("[}]"), Is.False);
}
/// <summary>
/// Tests the string "(a{[]b({})}c)[]", which is balanced.
/// </summary>
[Test, Timeout(1000)]
public void TestFLongMatch()
{
Assert.That(ParenthesisMatcher.IsBalanced("(a{[]b({})}c)[]"), Is.True);
}
/// <summary>
/// Tests the string "(a{[]b({})}c)[](", whose last parenthesis is not matched.
/// </summary>
[Test, Timeout(1000)]
public void TestFLongMissingClose()
{
Assert.That(ParenthesisMatcher.IsBalanced("(a{[]b({})}c)[]("), Is.False);
}
/// <summary>
/// Tests the string "(a{[]b({})}c)[]())", whose last parenthesis is not matched.
/// </summary>
[Test, Timeout(1000)]
public void TestFLongExtraClose()
{
Assert.That(ParenthesisMatcher.IsBalanced("(a{[]b({})}c)[]())"), Is.False);
}
/// <summary>
/// Tests the string "(a{[]b({})}c}[]", whose first character is paired with the last '}', and
/// hence is mismatched.
/// </summary>
[Test, Timeout(1000)]
public void TestFLongMismatch()
{
Assert.That(ParenthesisMatcher.IsBalanced("(a{[]b({})}c}[]"), Is.False);
}
}
}
當我通過調試器運行特定測試時,代碼將工作並運行字符串,直到它到達第一個右括號為止。 一旦結束括號起作用,並且開始和結束括號從測試中刪除,程序停止返回false。 但是,它應該繼續通過字符串並返回 True。 老實說,我迷失在這段代碼中,因為其他與此非常相似的匹配字符串通過了,並且在第一個結束點后沒有退出。
任何關於如何解決這個問題的建議將不勝感激。 如果需要其他任何幫助,請告訴我,我嘗試包含盡可能多的信息。
從查看代碼來看,我認為問題在於您的IsBalanced
方法在IsBalanced
右括號時立即返回false
。
在您的代碼中,當遇到右括號時,有以下選項:
return false
return true
return false
要解決此問題,您可能需要使用Matches
方法來檢查右括號是否與從堆棧中彈出的字符匹配:
public static bool IsBalanced(string input)
{
var stack = new Stack<char>();
foreach (var chr in input)
{
if (IsOpeningParenthesis(chr))
{
stack.Push(chr);
}
else if (IsClosingParenthesis(chr))
{
if (stack.Count == 0) return false;
if (!Matches(stack.Pop(), chr)) return false;
}
}
return stack.Count == 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.