Thread: Bracket Checker

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    112

    Bracket Checker

    i dont want anybody to solve this problem for me ,, i jus want you guys to help me understanding the problem,, i didnt really get which sequence is valid or not,,How should i start solving the problem etc,,any hints regarding member functions n data members!!



    Correct Sequence of brackets is always of primary importance both for written programs and mathematical expressions. By correct sequence we mean that for every opening bracket there is a closing bracket. The problem is to write a program that takes a random sized (size max 15) input of brackets from user, as a string, and can validate the sequence for e.g.
    {([]{()})} valid
    {[]}(} invalid
    []{{{} invalid
    (((}))) invalid
    {{{}}}} invalid
    Reminder:
    • Use stack to solve the problem.
    • Use Stack class in some other class that uses stack to validate the sequence.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    For example:
    - read one character at a time
    - for each opening bracket, push the corresponding closing bracket on the Stack
    - for each closing bracket: if the Stack is empty or the top of the Stack is not the same symbol then error, else pop the Stack
    - when no more input the Stack should be empty, or there were unmatched opening brackets.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    - for each opening bracket, push the corresponding closing bracket on the Stack??
    Dint really get it,,


    plus here is the code for the stack class the compiler shows no error but output is not right

    Code:
    #include<iostream.h>
    #include<conio.h>
    
    class Stack
    {
    int array[5];
    int top;
    
    public:
    
     Stack():top(0)
     {}
    
     void push(int a)
     {
     if (0<=top<=6)
     array[top]=a;
     top++;
     }
    
     int pop()
     {
     if (top>=0)
     top--;
     return array[top];
     }
     };
     int main()
     {
     clrscr();
     Stack b;
     int c;
    
     for (int i=0; i<=6;i++)
     {
     cin>>c;
     b.push(c);
     }
     for (int j=0; j<=6;j++)
     {
     cout<<b.pop();
     }
     getch();
     return 0;
     }

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    First of all, 'top' should be less than or equal to 4, not 6 (remember: array indexes start at 0). Or even more simply, less than 5. The second problem is that you are reading an int instead of a char.

    EDIT: Also, you should check that 'top' is greater than (not greater than or equal to) 0 before decrementing.
    Last edited by Sebastiani; 11-20-2009 at 07:02 AM.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    The second problem is that you are reading an int, not a char.

    dint get you??

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    And you said that you need to compare the braces and a string why your stack is having int's i think you should have string instead of int in array of stack class as the data member.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Fatima Rizwan View Post
    The second problem is that you are reading an int, not a char.

    dint get you??
    The user input - it's declared as an integer. When you pass an integer to the 'cin' object, it expects an integral number, eg: 2012, -3114, etc. To get it to read a literal character, eg: '{', '(', etc, you need to declare the variable as a char.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    112

    Stack

    i have created a class of stack which pushes the integers in and when the stack is loaded pops them out..
    The compiler shows no errors but there is some logical errori guess,,!!



    Code:
    #include<iostream.h>
    #include<conio.h>
    
    const int MAX=6;
    
    class Stack
    {
    int array[5];
    int top;
    
    public:
    
     Stack()
     {
     top=0;
     }
    
     void push(int a)
     {
     if (top<MAX)
     array[top]=a;
     top++;
     }
    
     int pop()
     {
     if (top>0)
     top--;
     return array[top];
     }
     };
     int main()
     {
     clrscr();
     Stack b;
     int c;
    
     for (int i=0; i<=5;i++)
     {
     cin>>c;
     b.push(c);
     }
     for (int j=0; j<=6;j++)
     {
     cout<<b.pop();
     }
     getch();
     return 0;
     }

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    i have created a class of stack which pushes the integers in and when the stack is loaded pops them out..
    The compiler shows no errors but there is some logical errori guess,,!!
    You still haven't changed the declaration to the 'char' type, as suggested.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    first i want to learn how to make int stack class ,, then i will move to the real problem,, thats why i dint changed the type to char,,,

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I see. Well, one thing I just noticed is that the 'push' function still increments 'top' if the maximum is exceeded, since the increment is not on the same line as the assignment before it. If you want to associate multiple statements with an 'if' condition, you have to put brackets around it. Anyway, saying simply that "it doesn't work" is really not sufficent. You need to state what you expect the program to do and why, versus what it actually does when you run it.

    EDIT: Ah, you are also comparing 'top' with 'MAX', which is not the actual arrays size. Also, in the part of the code where you are printing the stack contents, you are comparing less than or equal to 6, when it should be less than the array length (which should be 'MAX', but don't forget to change that in the array declaration, as well).
    Last edited by Sebastiani; 11-20-2009 at 08:12 AM.

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    ohk i have amended my program and now its working fine!!


    Code:
    #include<iostream.h>
    #include<conio.h>
    
    const int MAX=7;
    
    class Stack
    {
    int array[MAX];
    int top;
    
    public:
    
     Stack()
     {
     top=0;
     }
    
     void push(int a)
     {
     if (top<MAX)
     { array[top]=a;
     top++;}
     }
    
     int pop()
     { if (top==0)
     { cout<<"Stack is Empty";
     return 0;}
     top--;
     return array[top];
     }
     };
     int main()
     {
     clrscr();
     Stack b;
     int c;
     for (int i=0; i<7;i++)
     {
     cin>>c;
     b.push(c);
     }
     for (int j=0; j<2;j++)
     {
     cout<<b.pop()<<endl;
     }
     getch();
     return 0;
     }

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Coming back to the real problem ,,

    Use Stack class in some other class that uses stack to validate the sequence."

    what does this mean??

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Fatima Rizwan View Post
    ohk i have amended my program and now its working fine!!
    Good. But you do need to indent your code properly. Otherwise, it's very difficult to follow.

    Code:
    #include<iostream.h>
    #include<conio.h>
    
    const int MAX=7;
    
    class Stack
    {
        int array[MAX];
        int top;
    
        public:
    
        Stack()
        {
            top=0;
        }
    
        void push(int a)
        {
            if (top<MAX)
            { 
                array[top]=a;
                top++;
            }
        }
    
        int pop()
        { 
            if (top==0)
            { 
                cout<<"Stack is Empty";
                return 0;
            }
            top--;
            return array[top];
        }
     };
     
    int main()
    {
        clrscr();
        Stack b;
        int c;
        for (int i=0; i<7;i++)
        {
            cin>>c;
            b.push(c);
        }
        for (int j=0; j<2;j++)
        {
            cout<<b.pop()<<endl;
        }
        getch();
        return 0;
    }
    Coming back to the real problem ,,

    Use Stack class in some other class that uses stack to validate the sequence."

    what does this mean??
    Go back and read Anon's reply (post #2) to get a good idea of how to do it. Let's say you have the following input:

    Code:
    class A
    {
        class B
        {
            void foo( )
            {
            
            }
                    
            class C
            {
                void bar( )
                {
                
                }
            }
        }
    }
    So basically, when you encounter an 'opening' symbol, simply push the corresponding 'closing' symbol onto the stack. If you come across a 'closing' symbol, just check the stack. If it isn't what you expected, then there is an error, otherwise, discard the current symbol (pop the stack) and proceed. If there is an attempt to pop an empty stack, or the stack is not empty when you are done, then it is ill-formed.
    Last edited by Sebastiani; 11-20-2009 at 09:15 AM.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    Coming back to the real problem ,,
    
    Use Stack class in some other class that uses stack to validate the sequence."
    
    what does this mean??
    It most probably means that you have a stack class (with push, pop, and perhaps top and empty methods) and then you have a class called BracketChecker, perhaps with a method:

    Code:
    bool validate(const std::string& expression);
    that uses the Stack to get the job done (why it has to be a class is somewhat unclear).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MISRA C Checker open source
    By huwan in forum Tech Board
    Replies: 8
    Last Post: 10-27-2009, 08:13 AM
  2. code checker crap
    By KIBO in forum C Programming
    Replies: 11
    Last Post: 09-11-2008, 02:00 AM
  3. Spell checker poem.
    By adrianxw in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-13-2004, 10:49 AM
  4. spell checker in c needs help
    By madmax in forum C Programming
    Replies: 3
    Last Post: 03-13-2003, 09:36 AM
  5. Sorting through delimiting characters
    By Aluvas in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2002, 01:42 PM