views:

95

answers:

3

I am having an issue solving Bracket Checker .
i cant seem to solve the problem if user input this sequence of bracket then my program must print its not a right sequence

Input:

({}[)

Output:

Not a Right Sequence

My code is below

        Stack s = new Stack();

        Queue q = new Queue();

        bool isok = true;

        string FinalData = "0";            

        Console.WriteLine("Enter Brackets");

        string data = Console.ReadLine();

        for (int i = 0; i < data.Length; i++)
        {
            if (data.Substring(i, 1) == "{"
             || data.Substring(i, 1) == "["
             || data.Substring(i, 1) == "("
                )
            {
                s.Push(data.Substring(i, 1));
            }
            else
            {
                q.Enqueue(data.Substring(i, 1));
            }
        }
        while (s.Count > 0 && q.Count > 0)
        {
            FinalData = (String)s.Pop();
            string value = (String)q.Dequeue();
            if (FinalData == value)
            {
                isok = false;
                break;
            }                 
        }

        if (isok)
            Console.WriteLine(data + " is a Right Sequence.");
        else
            Console.WriteLine(data + " is Not a Right Sequence.");
        Console.ReadLine();
    }
+4  A: 

I'l give you a few hints and the basic idea:

  • you don't need the Queue, just a Stack<char>.
  • read the input, for each char:
    • if the char is an open brace, push it
    • if the char is a close brace, pop the stack and compare.
    • discard other chars
Henk Holterman
@Henk Holterman: i have an assignment regarding using stack and queue i have to solve this problem by using both of them .
Pro_Zeck
I'll add the homework tag first.
Henk Holterman
@Prozek, you can solve that by calling a Stack a LIFO queue. Look it up.
Henk Holterman
@Henk Holterman: i have solved this problem by stack also now i am solving this problem by both stack an queue
Pro_Zeck
Well, good luck.
Henk Holterman
A: 
public bool CheckBraces(string data)
{
    Stack<char> stack = new Stack<char>();
    foreach(char c in data){
        switch(c){
            case '(':
            case '[':
            case '{':
                stack.Push(c);
                break;
            case ')':
            case ']':
            case '}':
                if(!CheckMatch(stack.Pop(),c)){
                    return false;
                }
                break;
        }
   }
   return true;
}

private bool CheckMatch(char a, char b){
   return a=='(' && b==')' || 
          a=='[' && b==']' ||
          a=='{' && b=='}';
}
Tor
This is what I described half an hour ago. You are lacking a Queue too.
Henk Holterman
And what about: `bool CheckMatch(char a, char b) { return a==b; }`
Henk Holterman
A: 

Thannks Guys I have Solved The Problem Using stack And Queue Both AT the Same time. Here's the code

        Stack s = new Stack();

        Queue q = new Queue();

        bool isRight = true;

        char OpeningBracket = ' ';

        char closingBracket = ' ';

        Console.WriteLine("Enter Brackets");
        string data = Console.ReadLine();
        char[] character = data.ToCharArray();
        for (int i = 0; i < character.Length; i++)
        {
            if (character[i] == '(' || character[i] == '{' ||
                character[i] == '[')
            {
                s.Push(character[i]);
            }
            else
                q.Enqueue(character[i]);
        }

        if (s.Count == 0 || q.Count == 0)            
            isRight = false;

        while (s.Count > 0 && q.Count > 0)
        {
            OpeningBracket = (char)s.Pop();
            closingBracket = (char)q.Dequeue();
            if ((OpeningBracket == '(' && closingBracket != ')')
           ||   (OpeningBracket == '[' && closingBracket != ']')
           ||   (OpeningBracket == '{' && closingBracket != '}')
                )
            {
                isRight = false;
            }

        }

        if (isRight)
            Console.WriteLine(data + " is a Right Sequence.");
        else
            Console.WriteLine(data + " is Not Right Sequence.");
        Console.ReadLine();
    }
Pro_Zeck
And did you notice how illogical this is?
Henk Holterman
@Henk : What Do you MEAN by How ILLogical is this ??
Pro_Zeck
Using a Queue and a Stack for exactly the same task (and doing that task twice).
Henk Holterman
@Henk your Right!bUT i was expermenting on this problem so thats why i used queue and stack :)
Pro_Zeck