Thread: paretheses checking using stacks

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

    paretheses checking using stacks

    edited
    Last edited by cannsyl; 12-05-2008 at 08:55 PM. Reason: too much code

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    Ok so too much code.
    Simple question
    which of my headers is deprecated or antiquated?
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>   // for exit(..)
    
    
    #include <cstdio>
    #include "stack.h"
    #define STK_ERROR '?'
    #include "stack.h"
    
    /* Private data: */
    #define MAX 50
    and what is STK_ERROR'?'
    and how does stk_top work?
    Thanks

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, you fixed cstdio.h (which just plain doesn't exist) and got back to cstdio, so that's good. What do you mean "how to use stk_top"? I would type the symbols "s", "t", "k", "_", "t", "o", and "p" in order. It gives you the top of the stack, i.e., the symbol that will come off the stack next.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    First I would like to thank tabstop for the very helpful reply. We haven't gone over this in class yet and you were able to clear it right up. I never would have thought to
    type the symbols "s", "t", "k", "_", "t", "o", and "p" in order.
    If someone else could help with my next question?
    I was wondering if anyone sees a problem with this block...I have been looking so long I don't see anything and dev is telling me I am missing a ; somewhere.
    thanks
    Code:
    else if (c == ')')
           {
                
                if( c == ')' && stk_top() == '(' )
                {
                stk_pop();
                }
                else if ( c == ')' && stk_top() == '\n' )
                {
                stk_pop();
                }
                else
                {
                stk_push(c);
                
                    
                // pop stack, if empty have missing (
           }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cannsyl View Post
    First I would like to thank tabstop for the very helpful reply. We haven't gone over this in class yet and you were able to clear it right up. I never would have thought to
    If someone else could help with my next question?
    I was wondering if anyone sees a problem with this block...I have been looking so long I don't see anything and dev is telling me I am missing a ; somewhere.
    thanks
    Code:
    else if (c == ')')
           {
                
                if( c == ')' && stk_top() == '(' )
                {
                stk_pop();
                }
                else if ( c == ')' && stk_top() == '\n' )
                {
                stk_pop();
                }
                else
                {
                stk_push(c);
                } /*missing close curly here? */
                    
                // pop stack, if empty have missing (
           }
    Problem I can see marked in red.

    And I realize that my suggestion was unserious, but I still don't know what your question was.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    Sorry
    I am just in a crunch and have been working on this for a while...with little instruction in class.
    Here is my question now...Can you read in a .txt file in C the way you do in C++?
    and I need to process all expressions in the input file, echo the expression,
    and correctly report one of the following for each expression:
    • parentheses match
    • missing )
    • missing (

    I think I am to the point whereI need to echo the expression but where should I cout the .cpp or the main? do you cout in C?

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>   // for exit(..)
    
    
    #include <cstdio>
    #include "stack.h"
    
    int main()
    {
       char c;
       int k;
      
    
       //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 == ')' && stk_top() == '(' )
                {
                stk_pop();
                }
                else if ( c == ')' && stk_top() == '\n' )
                {
                stk_pop();
                }
                else
                {
                stk_push(c);
                }
                    
                // pop stack, if empty have missing (
           }
           else if (c == '\n')
           {
                if ( stk_top() == ')' )
                {
                stk_pop();
                }
             // check on missing )
             // set up for next line
                stk_init();
           }
           if (stk_error()) //skip rest of line
           {
              stk_init();
           }
    
           else // get next char
           {
                
           }
         } // endwhile
       return 0;
    }
    and thank you for your help.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You printf in C. The f stands for formatted, which means you have to provide a format string (what to print and how to print it). Man printf will get you perhaps too much information.

    I have no idea what "the .cpp or the main" means. Since this is C you don't have a .cpp anyway. I would print it out as/directly after you read it in.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    how do you read in a txt file into a c program?
    I have
    Code:
    int main()
    {
       char c;
       int k;
      
    
       //initialize
    
    stk_init();
          c = getchar();
          putchar(c);
          when I put printf here I get an error
    
       // 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
         when I put printf (c); here I get an error
           if (c == '(')
           {
             stk_push(c);
    
           }
           else if (c == ')')
           {
                
                if( c == ')' && stk_top() == '(' )
                {
                stk_pop();
                }
                else if ( c == ')' && stk_top() == '\n' )
                {
                stk_pop();
                }
                else
                {
                stk_push(c);
                }
                    
                // pop stack, if empty have missing (
           }
           else if (c == '\n')
           {
                if ( stk_top() == ')' )
                {
                stk_pop();
                }
             // check on missing )
             // set up for next line
                stk_init();
           }
           if (stk_error()) //skip rest of line
           {
              stk_init();
           }
    
           else // get next char
           {
                
           }
         } // endwhile
       return 0;
    }
    How would I use printf?

    I am in a c++ class and I don't know why we are doing c...
    I have to read in a txt file, match the parentheses, echo each character, and then say whether it matches or not.

    I don't know what I am doing?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's why you type "man printf" to find out more about printf (or look it up in the back of the book, etc.) Again, printf requires a format string that says what it is printing and how to print it. So to print a character you would do
    Code:
    printf("%c", character_variable_to_be_printed);
    In C, you open a file with fopen and close it with fclose and can read in from it with fgetc.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    So if I have
    Code:
    int main()
    {
       char c;
       int k;
      FILE *fp;
    
    
    fp=fopen("input.txt", "r");
    int fgetc (FILE *fp);
       //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
           printf("%c", c);
           if (c == '(')
    why doesn't it print anything when it runs? It compiles fine but runs a blank screen.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Given that you've managed to make several dozen function calls correctly, writing this
    Code:
    int fgetc (FILE *fp);
    makes no sense. Did you specify a return type when you called fopen? stk_init? getchar? putchar? printf? stk_pop? stk_push? Why do so here? Did you specify the types of the parameters when you called getchar? putchar? printf? etc.? Why do so here?

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    I don't know...I was following the template I was given...Like I said this was a c++ class and this is the only C I have ever done. I have no idea why we were thrown this curve at the end of the semester.

    So I just need
    Code:
    int fgetc ();
    and that should read in one character at a time.
    and shouldn't
    Code:
    if (c == '(')
           {
             stk_push(c);
             printf("%c", c);
    
           }
    read out one char at a time?
    I still get a blank running screen

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, are you aware of functions in general, and how they work? I have a feeling that your instructor thinks the answer is "no", hence the assignment, where you are literally given every function you could ever need, if only you knew how to call them. If I tell you that there is a function called "fgetc", which requires a parameter that is a FILE*, and returns an int that is the next character in the file pointed to by the parameter, could you use that function?

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    I honestly don't think so.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Actively cross-posting, eh? Bad form.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  4. ...multiplication using stacks
    By iiwhitexb0iii in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 01:28 AM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM