Thread: Parentheses checker with stacks

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    48

    Parentheses checker with stacks

    I have to use stacks to check an input file for parentheses matches.
    I have a skeleton...but I am having some problems
    Do I have to use a class or struct to do this?
    Here is what I have in main
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>   // for exit(..)
    
    
    #include <stdio.h>
    #include "stack.h"
    
    int main()
    {
       char c;
       int k;
    ifstream f;
      f.open( "input.txt" );
    
      if( ! f ){
        cout << "Error opening file. Quitting.\n";
        exit(-1);
      }
    
       //initialize
    
       stk_init();
          c = getchar();
          putchar(c);
    
       // loop through file one char at a time
       while (c!=EOF)
       {
         // process one character
         // push (,  pop if ),
         // check stack empty if \n,
         // ignore others
         // if error, move to next line
           if (c == '(')
           {
             stk_push(c);
    
           }
           else if (c == ')')
           {
                if (c !== '\n')    
                stk_pop(c);
                else if (c == '(')
                stk_push (c);
    // pop stack, if empty have missing (
           }
           else if (c == '\n')
           {
             // check on missing )
             // set up for next line
             stk_init();
           }
           if (stk_error()) //skip rest of line
    
           else // get next char
           {
           }
         } // endwhile
       return 0;
    }
    my specific question first is
    how do I check for the top of the stack?
    I know i need to do this for the ')' first but I'm not sure how. Do I need to declare a variable for the top of stack?
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cannsyl
    Do I have to use a class or struct to do this?
    You can use std::stack after you #include <stack>

    Incidentally, you should #include <iostream>, <fstream> and <cstdlib> instead of <iostream.h>, <fstream.h> and <stdlib.h> respectively (though <stdlib.h> is acceptable).
    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 2008
    Posts
    48
    if std::stack is like <list> and uses the compiler to do something for me...my professor won't let us do that. She wants us to do things the long way first. But thanks for the other tips I kept getting a huge error
    Code:
    #ifdef __DEPRECATED
    #warning This file includes at least one deprecated or antiquated header. \
    Please consider using one of the 32 headers found in section 17.4.1.2 of the \
    C++ standard. Examples include substituting the <X> header for the <X.h> \
    header for C++ includes, or <iostream> instead of the deprecated header \
    <iostream.h>. To disable this warning use -Wno-deprecated.
    #endif
    
    #endif

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cannsyl
    if std::stack is like <list> and uses the compiler to do something for me...my professor won't let us do that. She wants us to do things the long way first.
    In that case, study the basic interface that std::stack provides and mimic it. Your current stk_init(), stk_push(), etc library of functions is not very reusable.

    Quote Originally Posted by cannsyl
    But thanks for the other tips I kept getting a huge error
    Yes, though if you looked carefully into the warning you would see that it did suggest using <iostream> too
    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

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    our instructor gave us a .h file
    what does #define STK_ERROR '?' mean?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cannsyl
    our instructor gave us a .h file
    As in your instructor specified the declarations of those tk_init(), stk_push(), etc library of functions?

    Quote Originally Posted by cannsyl
    what does #define STK_ERROR '?' mean?
    It just defines a macro named STK_ERROR. The significance of that depends on context.
    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

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    yes she wrote the .h file and gave it to us this is it
    Code:
    /* stack.h: Declarations for a stack of chars */
    #define STK_ERROR '?' 
    
    void stk_init();
    void  stk_push(char);
    char  stk_pop(void);
    char  stk_top(void);
    int  stk_size(void);
    int  stk_error(void);
    and this is the .cpp
    Code:
    /* stack.c: implementation */
    #include "stack.h"
    
    /* Private data: */
    #define MAX 50 
    static int error;   /* error flag */
    static char data[MAX];   /* the stack */
    static int ptr;         /* stack pointer */
    
    /* Function implementation */
    
    void stk_init() {
       ptr = 0;
       error = 0;
    }
    
    void stk_push(char x) {
        if (ptr < MAX) {
            data[ptr++] = x;
            error = 0;
        }
        else
            error = 1;
    }
    
    char stk_pop(void) {
        if (ptr > 0) {
            int x = data[--ptr];
            error = 0;
            return x;
        } else {
            error = 1;
            return STK_ERROR;
        }
    }
    
    char stk_top(void) {
        if (ptr > 0) {
            error = 0;
            return data[ptr-1];
        } else {
            error = 1;
            return STK_ERROR;
        }
    }
    
    int stk_size(void) {
        return ptr;
    }
    
    int stk_error(void) {
        return error;
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Um, is this C or C++?
    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

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    I know its alot, but could you comment out this code so I know what it means?
    Code:
    char stk_top(void) {
        if (ptr > 0) {
            error = 0;
            return data[ptr-1];
        } else {
            error = 1;
            return STK_ERROR;
        }
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That basically says: if there is something in the stack, return the top, otherwise set an error condition and return an error code.

    This is the C++ programming forum. Although your teacher's code contains bad practice either way (the unnecessary use of variables with static storage duration), I suspect that you are actually supposed to code in C since the implementation file is apparently named "stack.c".
    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

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    I am in a c++ class (actually my first real c++ class) but we are at the end of the semester and they are introducing data structures and I think this program is mostly in c but compiling with dev? I don't know that is just what I am told...so should i find a C forum?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If it really is supposed to be C++ and hence C++ constructs are allowed, then fine.
    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. Please Help Me With This Code Of Implementing Stacks
    By raghu_equinox in forum C Programming
    Replies: 3
    Last Post: 10-19-2006, 07:22 AM
  2. ...multiplication using stacks
    By iiwhitexb0iii in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 01:28 AM
  3. Help With Stacks
    By penance in forum C Programming
    Replies: 7
    Last Post: 10-09-2005, 02:47 PM
  4. Avioding Stacks
    By LoafOfBread34 in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 06:20 AM
  5. Stacks stacks stacks
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-06-2002, 02:01 AM