finding that expression has balanced parenthese, brackets, braces

This is a discussion on finding that expression has balanced parenthese, brackets, braces within the C Programming forums, part of the General Programming Boards category; Another stack problem. Now this is not my code. HINT: This one actually works. (ha ha) I would like to ...

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    finding that expression has balanced parenthese, brackets, braces

    Another stack problem.

    Now this is not my code. HINT: This one actually works. (ha ha)
    I would like to change from using all functions that pass pointers to having only the PUSH function passing a value. Would this work?? And I think I want the functions to match my stack functions from my original STACK program. (which still ain't working -- but will be soon)

    Oh, and I would like there to be a better confirmation output instead of just "Correct" when the program finds that the brackets, and braces and parentheses all are balanced for whatever expression is input.

    ex. input : {6*5 [y*5] / (a*8)}
    output : your expression has balanced symbols

    or something like that. What seems like a good output so user knows they input a correct looking expression ??

    Also, my instructor said something about using the ASCII codes for the r/l brackets, braces and parentheses. If I did that, would I have to write something like:

    instead of

    Code:
    while (a[i] != NULL)
    
      {if (a[i] == '(')
        push (&top, '(');
       if (a[i] == ')')
       { if (is_empty(&top))
         {printf("Incorrect\n");
          exit(0);
         }
          pop (&top);
       }
       i++;
      }
    write this

    Code:
    while (a[i] != NULL)
       {if (a[i] == '\40' )        /*   \40 is the character  (  */ 
            push (&top, '\40');
         if (a[i] == '\41' )           /*  \41 is the character  )  */
           {  if (is_empty(&top))
               printf("Incorrect\n");
               exit(0);
           }
          pop (&top);
       }
       i++;
      }

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define N 100
    
    typedef struct {
                    char sa[N];
                    int count;
    }Stack;
    
    void intialize (Stack *);
    int is_empty (Stack *);
    void push (Stack *, char);
    char pop (Stack *);
    void cleara(Stack *, int);
    
    int main ()
    {
      int i = 0;
      char a[N];
      Stack top;
     
      printf("Please enter a string to be evaluated: ");
     gets(a);
    
      intialize (&top);
      while (a[i] != NULL)
    
      {if (a[i] == '(')
        push (&top, '(');
       if (a[i] == ')')
       { if (is_empty(&top))
         {printf("Incorrect\n");
          exit(0);
         }
          pop (&top);
       }
       i++;
      }
    
      if (!is_empty(&top))
      {printf("Incorrect\n");
       exit(0);
      }
      cleara(&top, N);
      i=0;
      while (a[i] != NULL)
      {if (a[i] == '[')
        push (&top, '[');
       else
       if (a[i] == ']')
       { if (is_empty(&top))
         {printf("Incorrect\n");
          exit(0);
         }
          pop (&top);
       }
    
       i++;
     }
      if (!is_empty(&top))
      {printf("Incorrect\n");
       exit(0);
      }
      cleara(&top, N);
      i=0;
      while (a[i] != NULL)
      {if (a[i] == '{')
        push (&top, '{');
       if (a[i] == '}')
       { if (is_empty(&top))
         {printf("Incorrect\n");
          exit(0);
         }
          pop (&top);
       }
    
       i++;
      }
    if (!is_empty(&top))
      {printf("Incorrect\n");
       exit(0);
      }
      printf("Correct\n");
      return 0;
    }
    
    void intialize (Stack *ptop)
    {
      ptop->count = 0;
    }
    
    int is_empty (Stack *ptop)
    {
      if (ptop->count == 0)
       return 1;
    
      return 0;
    }
    
    void push (Stack *ptop, char p)
    {
      ptop->sa[ptop->count] = p;
      (ptop->count)++;
    }
    
    char pop (Stack *ptop)
    {
      char temp;
      temp = ptop->sa[ptop->count-1];
      (ptop->count)--;
      return temp;
    }
    
    void cleara(Stack *ptop, int n)
    { int i;
      for (i=0; i<n; i++)
       ptop->sa[i] = '\0';
    }
    Last edited by sballew; 11-30-2001 at 09:32 PM.
    Sue B.

    dazed and confused


  2. #2
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    ok I changed most of what I could think of to change to be in the same format as my earlier stack program in another thread.

    I am getting a compilation error (surprise surprise !!)
    and don't have a clue as to what it means nor what to fix.

    here's the compilation error I get when compiling

    28 /accounts/student2/srbkpk/STACK a.out
    Please enter a string to be evaluated: {6/(3*4)+(4/2*[45-2])}
    40 deleted successfully.
    40 deleted successfully.
    Segmentation Fault (core dumped)



    I don't understand what the cleara function is supposed to do, so I may not have recoded it right to fit my program's spec/variable setup.

    What I understand this program to do is have user input a math expression with parentheses, brackets and braces (known as PBB from here on out) and the program only evaluates these symbols. Pushing the open PBB symbol one at a time and if the next PBB symbol is the complementary close PBB, then that is considered balanced.

    ex. expression enter = {6/4(2*456)-[46/7]}
    first comes across a {
    second comes across a (
    then comes across a ) so it pops the ( and ) and still
    has the { in stack
    then comes across a [
    then comes across a ] so it pops the [ and ] and still
    has the { in stack
    then comes across a } so it pops the { and } and
    now stack is empty, thus the expression was balanced
    correctly.

    So is this the correct interruptation of the program and what it is doing??



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX 100
    
    struct node{
       int data;
       struct node * next;
    };
    
    struct node *first;
    
    void make_empty(void);
    int is_empty (void);
    void push (int value);
    int pop (void);
    void cleara(int);
    
    /***************************************************/
    /*      START MAIN                                 */
    /***************************************************/
    
    main ()
    {
      int i = 0;
      char array[MAX];
    
      printf("Please enter a string to be evaluated: ");
      gets(array);
    
      make_empty ();
    
      while (array[i] != NULL)
      {if (array[i] == '(')
        push ( '(');
       if (array[i] == ')')
       { if (is_empty())
         {printf("Incorrect\n");
          exit(0);
         }
          pop ();
       }
       i++;
      }
    
    
      if (!is_empty())
      {printf("Incorrect\n");
       exit(0);
      }
    
      cleara(MAX);
      i=0;
    
      while (array[i] != NULL)
      {if (array[i] == '[')
        push ('[');
        else if (array[i] == ']')
       { if (is_empty())
         {printf("Incorrect\n");
          exit(0);
         }
          pop ();
       }
    
       i++;
     }
    
      if (!is_empty())
      {printf("Incorrect\n");
       exit(0);
      }
    
      cleara(MAX);
      i=0;
    
      while (array[i] != NULL)
      {if (array[i] == '{')
        push ('{');
       if (array[i] == '}')
       { if (is_empty())
         {printf("Incorrect\n");
          exit(0);
         }
          pop ();
       }
    
       i++;
      }
    
    if (!is_empty())
      {printf("Incorrect\n");
       exit(0);
      }
      printf("Correct\n");
    
      return 0;
    }
    
    /**************************************************/
    /*        END MAIN                                */
    /**************************************************/
    
    void make_empty(void)
    {
      first  = NULL ;
    }
    
    int is_empty (void)
    {
       return first == NULL;
    }
    
    void push(int value)
    {
       struct node * new_node;
    
       new_node=malloc(sizeof(struct node));
       if (new_node == NULL)  {
          printf ("Error in push: stack is full.\n");
          exit (EXIT_FAILURE);
       }
    
       new_node->data = value;
       new_node->next = first;
       first = new_node;
    }
    
    int pop (void)
    {
       struct node * top;
       int i;
    
       if (is_empty())  {
          printf("Error in pop: stack is empty.\n");
          exit (EXIT_FAILURE);
       }
    
       printf("%d deleted successfully.\n",first->data);
       top = first;
       i = first->data;
       first = first->next;
       free(top);
       return i;
    }
    
    void cleara(int n)
    { int i;
      for (i=0; i<n; i++)
       first->data = '\0';
    }
    Last edited by sballew; 12-01-2001 at 06:07 PM.
    Sue B.

    dazed and confused


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem on finding frequency of an expression
    By c_idiot in forum C Programming
    Replies: 5
    Last Post: 12-29-2007, 04:17 AM
  2. Help with making a Math Expression DLL
    By MindWorX in forum C Programming
    Replies: 19
    Last Post: 07-20-2007, 12:37 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM

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