properly nested

This is a discussion on properly nested within the C Programming forums, part of the General Programming Boards category; Hey all, Ive been working on this program for a while and keep changing it around. Im trying to have ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    22

    properly nested

    Hey all, Ive been working on this program for a while and keep changing it around. Im trying to have it verify that the user inputted braces are properly nested. I keep coming up with the outcome that they are not, even if they are. any input on where im going wrong is appreciated.
    Code:
    #include<stdbool.h>
    #include<stdio.h>
    
    
    #define STACK_SIZE 100
    
    
    char contents[STACK_SIZE];
    int top = 0;
    
    
    void stack_overflow(void)
    {
        printf("invalid under");
    }
    
    
    void stack_underflow(void)
    {
        printf("invalid over");
    }
    
    
    void make_empty(void)
    {
        top = 0;
    }
    
    
    bool is_empty(void)
    {
        return top==0;
    }
    
    
    bool is_full(void)
    {
        return top==STACK_SIZE;
    }
    
    
    char push(char ch)
    {
        if(is_full())
            stack_overflow();
        else
            contents[top++]=ch;
    }
    
    
    char pop(char contents[])
    {
       
        if(is_empty())
            stack_underflow();
        else
            return contents[--top];
    }
    
    
    int main(void)
    {
        char  ch,fst,lst;           
                 
        int i = 0;         
    
    
    
    
        printf( "Enter parenthesesor braces:\n" );
    
    
    
    
        while ( ( i < STACK_SIZE ) && ( ( ch = getchar() ) != '\n' ) ) {
           contents[i++] = ch;
    
    
        if(('('==ch)||('{'==ch))
                 {push(ch);
                fst = ch;
            }
            else if((')'==ch)||('}'==ch))
                  {
                    pop(contents);
                    lst = ch;
                    }
        }
    
    
            if ( fst == lst )
            printf("\nParenthesis/brackets nested properly....");
            else printf("\nParenthesis/brackets not nested properly...");
    
    
        return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Could you please give a sample input?

    Do not use global variables , see the last post here
    Variables on Specific ids (MPI)

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    22
    sample:
    Code:
    {(())}
    - should be nested properly
    Code:
    {{})
    - not nested properly

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    You check for overflow, why not check for underflow?

    You have for example
    Code:
    char pop(char contents[])
    {
        
        if(is_empty())
            stack_underflow();
        else
            return contents[--top];
    }
    and then when you call pop in main you do not collect the return value.This was made by accident?

    EDIT: Moreover, you check only the last character given by the user.Shouldn't you use a loop to test all the data that your stack holds?
    Last edited by std10093; 11-10-2012 at 01:07 PM.

  5. #5
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Code:
    pop(contents);
    Passing a global variable to the function?

    From what i can tell it would never return true as this will never evaluate as true
    Code:
    if(' (' == ')')
    Where are you checking if they are opposing brackets of equivalent type?

    If there are three different brackets how are you storing that information in a single spot in a char? It looks like you are over writing the old value.
    Last edited by Lesshardtofind; 11-10-2012 at 01:41 PM.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    22
    Yes to the external variables. Unfortunately.
    I changed to final statement to
    Code:
    if ( fst == '(' && lst == ')' || fst == '{' && lst == '}')
     printf("\nParenthesis/brackets nested properly....");
            else printf("\nParenthesis/brackets not nested properly...");
    but now it always runs as true....

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    22
    heres other changes that i made, but still not working:

    Code:
    #include<stdbool.h>
    #include<stdio.h>
     
     
    #define STACK_SIZE 100
     
     
    char contents[STACK_SIZE];
    int top = 0;
     
     
    void stack_overflow(void)
    {
        printf("invalid under");
    }
     
     
    void stack_underflow(void)
    {
        printf("invalid over");
    }
     
     
    void make_empty(void)
    {
        top = 0;
    }
     
     
    bool is_empty(void)
    {
        return top==0;
    }
     
     
    bool is_full(void)
    {
        return top==STACK_SIZE;
    }
     
     
    int push(int i)
    {
        if(is_full())
            stack_overflow();
        else
            contents[top++]=i;
    }
     
     
    int pop(void)
    {
        
        if(is_empty())
            stack_underflow();
        else
            return contents[--top];
    }
     
     
    int main(void)
    {
        char  ch,fst,lst;           
                  
        int i = 0;         
     
     
     
     
        printf( "Enter parenthesesor braces:\n" );
     
     
     
     
        while ( ( i < STACK_SIZE ) && ( ( ch = getchar() ) != '\n' ) ) {
           contents[i++] = ch;
     
     
        if(('('==ch)||('{'==ch))
                 {push(i);
                fst = contents[i];
            }
            else if((')'==ch)||('}'==ch))
                  {
                    pop();
                    lst = contents[i];
                    }
        }
     
     
             if ( fst == '(' && lst == ')' || fst == '{' && lst == '}')
            printf("\nParenthesis/brackets nested properly....");
            else printf("\nParenthesis/brackets not nested properly...");
     
     
        return 0;
    }

  8. #8
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    look at your push statement i think
    Code:
    contents[top++]=i;
    does not do what you think it does... just simulate numbers in your head and follow the program this line overwrites what

    Code:
    contents[i++] = ch;
    does

  9. #9
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    I changed three lines and got it to evaluate properly so you are close. When you figure that out then you'll have to figure out why
    Code:
     ()(
    would evaluate as true )

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    22
    Thanks for the encouragement! lol I know why its going to always be true- its because the lst is always going to be a right brace and fst will always be a left brace. Still messing with the push statement.

  11. #11
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    all push needs to do is increase top as long as it hasn't exceeded the limit. The first statement under the while saves the char into contents. By passing the int into the function and then contents you are just saving the number of times the loop has run over the actual parenthesis type you initially saved. So by time you get to comparing fst and lst you will not even have chars in the fst data type. fst will be an integer and last will be a char. Maybe you only need to do top++?

    Then you could have two integers NumOfOpen and NumOfClosed that count up when you find each. These numbers also should evaluate as true when compared. After that works you need to maybe use an array to save the parenthesis char types so you can evaluate if they properly closed each other.

    Remember your last opening parenthesis type should match your first closing parenthesis type and second to last matching second... Ect
    Last edited by Lesshardtofind; 11-10-2012 at 04:38 PM.

  12. #12
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Oh and by doing I++ during the first part of the while loop. You are saving char in data space one whole saying the top is one so the first data position would be 0 rather than one. Are you meaning to leave the first space of your array blank permanently? You could I++ at the end of the loop if that wasn't designed with a larger purpose in mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot call properly?
    By cmadore in forum C Programming
    Replies: 3
    Last Post: 03-27-2011, 12:12 PM
  2. malloc and how properly to use-it
    By c_lady in forum C Programming
    Replies: 10
    Last Post: 12-13-2010, 02:31 PM
  3. How To Bitshift Properly
    By pobri19 in forum C Programming
    Replies: 4
    Last Post: 07-26-2009, 05:32 AM
  4. Help on how to properly return a value
    By dld333 in forum C++ Programming
    Replies: 12
    Last Post: 12-06-2005, 11:18 PM
  5. Am I using *this properly?
    By Sebastiani in forum C++ Programming
    Replies: 3
    Last Post: 12-20-2001, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21