Thread: Parentheses Checker

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    33

    Parentheses Checker

    Code:
    • Program is supposed to check for balanced parentheses which are (), {}, [] using a vector
      • First read in the number of lines user wishes to test
      • Return Yes if balanced, No if unbalanced
    • Missing conditions to check for unbalanced parentheses
      • Program doesn't catch left parentheses returns Yes
      • Pairs wrong parentheses together, (} returns Yes, should be No
    • Cases with incorrect output
      • (}, (], ))), }}}, ]]], just a space or nothing entered
    • Looking for answers to how I can correctly catch the cases I've missed
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "char_vector.h"
    
    
    int main( int argc, char * argv [])
    {
      int i, num;
      char c;
      scanf("%d", &num);
      scanf("%c", &c);
    
    
      for(i = 0; i < num; i++)
        {
          MY_VECTOR vector = my_vector_init_default();
          do
            {
              scanf("%c", &c);
                if (c == '(' || c == '[' || c == '{')
                  {
                    char_my_vector_push_back(vector, c);
                  }
    
    
                if (c == ')' || c == ']' || c == '}')
                  {
                    my_vector_pop_back(vector);
                  }
              }while(c != '\n');
    
    
          if (my_vector_size(vector) == 0)
            {
              printf( "Yes\n");
            }
          else
            {
              printf( "No\n");
            }
          my_vector_destroy(&vector);
        }
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What cases have you missed, or more generally, how does it not work?

    EDIT:
    Oh wait, you provided that in what I thought was a description of how it currently works.

    Well, what you seem to be missing is a check to see if what was popped matches what was entered. Furthermore, even before you pop from the vector, you should check to see if the vector is empty, i.e., if it is empty, then you cannot pop thus there is a mismatch.
    Last edited by laserlight; 02-21-2014 at 11:50 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    33
    Thank you, laserlight, for responding I'm still having some trouble taking these concepts and putting them into code. My check to see if the vector is empty is still off as reading in a space still outputs Yes. Also, could you elaborate on how to go about checking if what was popped matches what was entered. This my current idea for checking if the vector is empty:

    Code:
    // only pop back when a left parenthesis is read and the vector is not empty 
     if (c == ')' || c == ']' || c == '}' && my_vector_size(vector) != '\0')
        {
             my_vector_pop_back(vector);
        }
    Last edited by whiteboyfly; 02-21-2014 at 11:07 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by whiteboyfly View Post
    My check to see if the vector is empty is still off as reading in a space still outputs Yes.
    This is as it should be. If you neither push nor pop into the vector, then the size will remain at 0.

    And if you've written pop_back at all correctly, it will give you the character that is popped off, so you can compare with the just-read-in character and see what's what.

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    33
    Thanks for the response I know pop back is written correctly as my professor gave it to us and we tested it in lecture.

    This being said I clearly don't have as full a grasp on the concepts of pushing and popping as I should.

    My understanding: Push is when the character read in is put onto the stack. Pop is when the character on top of the stack is taken off the stack. These functions work as LIFO last in first out. If I enter ()))) then ) would be the first character popped.

    My confusion: Where does the character that is popped go? How can I compare it to other characters on the stack? If I enter ()(} I should immediately get a mismatch when ( is encountered but I'm confused on how to make this comparison.

    Thanks in advance.
    Last edited by whiteboyfly; 02-23-2014 at 01:45 PM.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by whiteboyfly View Post
    My understanding: Push is when the character read in is put onto the stack. Pop is when the character on top of the stack is taken off the stack. These functions work as LIFO last in first out. If I enter ()))) then ) would be the first character popped.

    My confusion: Where does the character that is popped go? How can I compare it to other characters on the stack? If I enter ()(} I should immediately get a mismatch when ( is encountered but I'm confused on how to make this comparison.
    Thanks in advance.
    You shouldn't be pushing the the end brackets at all, at least not how I understand the problem. You should be pushing every open bracket to the stack and then whenever you encounter a ending bracket you simply pop a character from the stack and make sure it is the corresponding open bracket.

    A lot of people say to imagine a stack of plates as an analogy for a stack, you usually only ever put a plate on top and take a plate from the bottom. That said I don't really like this approach because I feel like the bottom plates will build up dust over time and so I'd need to wash them before use if I had a dinner party.

    EDIT: Also I'm incredibly sick of this 'code must be in code tags' rule which tells me off for posting 6 character literals in my english text .

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    33
    Thanks for the reply, maybe I should be more clear how do I go about making the comparison of popped characters to corresponding characters? This is my best attempt at a guess:

    Code:
    if (my_vector_pop_back(vector) == '(' || my_vector_pop_back(vector) == '[' || my_vector_pop_back(vector) == '{' )
      {
        //balanced return Yes                                                                                                                                                          
      }
     else
       {
         //unbalanced return No                                                                                                                                                        
       }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should pop once and store the result in a variable, then compare using the variable. Furthermore, what you compare depends on what is the input, i.e., if the input is a ')', then you compare with '(' to ensure a match.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parentheses Checker Problem!!
    By chakolate in forum C Programming
    Replies: 4
    Last Post: 11-03-2010, 06:46 PM
  2. parentheses solver
    By yangss in forum C Programming
    Replies: 27
    Last Post: 09-01-2010, 02:41 PM
  3. Parentheses checker with stacks
    By cannsyl in forum C++ Programming
    Replies: 11
    Last Post: 12-05-2008, 02:17 PM
  4. Parentheses in IF statements.
    By thetinman in forum C++ Programming
    Replies: 11
    Last Post: 11-30-2005, 04:08 PM
  5. Nesting Parentheses
    By XZSNPP in forum C++ Programming
    Replies: 6
    Last Post: 01-18-2003, 10:01 PM