简体   繁体   中英

Testing if Parenthesis are balanced, (a{[]b({})}c)[] returns false, but should be true

The point of this code is to test if the parenthesis in each set of strings are balanced. there are 12 tests that must all pass, currently, i have 11 of them working, however this last one will not work. currently, i have three static bools that are clarifying what classifies as a parenthesis, and if they match or not.

 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 == '}');
        }

below, i have the bool which actually checks if they are matching or not, this is where my error lies.

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;
            }

        }

final bit of code, these are all the tests that are being checked. I am currently struggling with Test number 9 / TestFLongMatch.

public void TestFLongMatch()
        {
            Assert.That(ParenthesisMatcher.IsBalanced("(a{[]b({})}c)[]"), Is.True);
        }

below is the full file with all the tests

/* 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);
        }
    }
}

when i ran the specific test through a debugger, the code would work and run through the string until it reached until it reached the first closing bracket. once the closing bracket came into play, and the opening and closing brackets were removed from the test, and the program stopped returning false. However it is supposed to continue on through the string and return True. Im honestly lost on this code as other matching strings very similar to this one passed, and did not quit after the first closing point.

Any advice on how to tackle this problem would be greatly appreciated. If anything else is needed to help with this please let me know, i tried to include as much info as possible.

From looking at the code, I think the problem is that your IsBalanced method returns false right away as soon as it runs into a closing parenthesis.

In your code, when a closing parenthesis is encountered, these are the options:

  1. There are no items on the stack: return false
  2. The first item on the stack is the same closing parenthesis (which is impossible): return true
  3. return false

To fix this, you might want to make use of your Matches method to check if the closing parenthesis is a match for the character popped from the stack:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM