1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Urgent Help Needed with Stacks Assignment!

Discussion in 'C' started by CWannabe, Nov 8, 2010.

?

How well is your knowledge of Stack ADTs.

  1. Very good

    0 vote(s)
    0.0%
  2. Fair

    0 vote(s)
    0.0%
  3. I have a clean logic of it.

    0 vote(s)
    0.0%
  4. Excellent - Professional

    0 vote(s)
    0.0%
  5. Newbie

    0 vote(s)
    0.0%
  1. CWannabe

    CWannabe New Member

    Joined:
    Oct 15, 2010
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Hello Thanks for Volunteering, I need to finish this assignment in the Fastest time possible, and would like all the help in completing it.

    Compilers check your source code for syntax errors, but many times a lack of one symbol (especially a missing brace) will cause your compiler to report errors. A useful tool would be one that checks to ensure that every right brace, bracket and parenthesis corresponds to its left counterpart.

    So Here:
    CSI 221 Assignment Two
    You are required to derive an algorithm, that uses a stack, to check for the balancing of parentheses, brackets and braces while ignoring any other character that appears. An implementation of this algorithm is then required.
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
    Parse the string of characters and algorithm should be

    if character is {
    push in stack.

    else if character is }
    check stack top to see { and if it is pop {

    At the end if Stack is empty things are ok
     
  3. CWannabe

    CWannabe New Member

    Joined:
    Oct 15, 2010
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Should we follow the assignment we need to Create an Algorithm to Accept String input per line and traverse each string to find characters matching the operators, when found it shoud push to stack... and pop where necessary.

    In this Option ill think of a test run on the Hello World Code, so the algorithm should traverse this code to determine whether its operators n operands, parent, braces, etc are inplace.
    Maybe Like reading and a hello world text file... and testing it.. then output results;
    " Great! Your Code is Ready for Compilation".
     
  4. CWannabe

    CWannabe New Member

    Joined:
    Oct 15, 2010
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Ok so far I've derived a simple Algorithm process:

    Algorithm:
    1) Traverse text file reading Character String Input per Line.
    2) Check each Character to determine where the operands, parents or braces, etc. are located
    3) If character is “{“ (or any other , ie, {}[]"", what ever may come in pairs).
    4) Push to stack
    5) Else if character is “}”
    6) Check the stack top to see { and if it is pop {
    7) Pop
    So if Stack is empty, the Code is Ok.
    --------------------------------
    So I just confirmed that to do this Assignment, i'll need to Create a text file, externally.
    The text file will have Code, lets say the simple "Hello World" code.

    File: hello.txt
    In this you will see (within the lines below):
    -----------------------------------
    int main()
    {
    printf(" Hello World\n");

    system("Pause");
    return 0;
    }
    ----------------------------------------

    So My concern is how will I Traverse a txt File above to check for characters to build a mini Code Proof Reader. Please Help
     
  5. CWannabe

    CWannabe New Member

    Joined:
    Oct 15, 2010
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    I need to understand the process of Character Strings and Files, Using Stacks..
     
  6. CWannabe

    CWannabe New Member

    Joined:
    Oct 15, 2010
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Goo so I got a Code here and Im puzzled, cause its not giving me my desired answer, HELP PLEASE! Where did I go wrong.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void push(char);
    int main(int argc, char *argv[])
    {
      int a;
      int i=0;
      int k=0;
      int j=0;
      int l=0;
      int m=0;
      int o=0;
      int p=0;
      char thestring[100];
      struct stuff
      {
          char asc;
          struct stuff * next;
      };
      struct stuff * ptr,*temp,*start;
    
          char tempString[255];
          printf("Enter Your file path\n ");
         printf("\n\nExample C:\Users\user\Documents\j.txt\n ");
          scanf("%s", tempString);
    
          FILE *fp;
    
          fp = fopen(tempString, "r"); // open the existing file for reading only ("r")
          fgets(tempString, 255, fp ); // gets a line of text at a time with a limit of 255 characters and puts it into variable 'tempString'
          fclose(fp); // closes file
          a= strlen(tempString);
    
    
          while(tempString[i]!= '\0')
               {
                   if (tempString[i] == '(' || tempString[i] =='{' || tempString[i] == '[')
                     {
                       ptr = (struct stuff*) malloc(sizeof (struct stuff));//start stack thingy
                       ptr->asc = tempString[i];
                       ptr->next= NULL;
                       break;
             }
                       i++;
    
             }
    
          for(i; i<a; i++)
               {
                   if (tempString[i]=='(' || tempString[i]=='{' || tempString[i]=='['||tempString[i]==')' ||tempString[i]=='}' || tempString[i]==']')
                    {
                      temp = (struct stuff*) malloc(sizeof (struct stuff));//start stack thingy
                      temp->asc=tempString[i];
                      temp->next=ptr;
                      ptr=temp;
                    }
           }
    
           temp=ptr;
           while (temp != '\0')
                   {
                    printf("%c\n", temp->asc);
                    temp=temp->next;
                   }
    
           printf("up to here workds");
    
    
    
          temp=ptr;
          while (temp != NULL)
               {
                   if (temp->asc == '{')
                     {
                        k++;
                     }
                   else if (temp->asc == '}')
                     {
                        j++;
                     }
                   if (temp->asc == '(')
                     {
                        l++;
                     }
                   else if (temp->asc == ')')
                     {
                        m++;
                     }
    
                   if (temp->asc == '[')
                     {
                       o++;
                     }
                   else if (temp->asc == ']')
                     {
                       p++;
                     }
    
    
                   temp=temp->next;
    
             }
    
          printf("%d %d %d %d %d %d" ,j, k,l,m,o,p);
               if(k!=j)
               {
          printf("Parenthesis Missing '{' not closed\n");
               }
          if(l!=m)
               {
          printf("Parenthesis Missing'(' not closed\n");
          }
           if(o!=p)
               {
          printf("Parenthesis Missing '[' not closed\n");
          }
    
    
      getch();
      return 0;
    }
    
    
    So i greated a text file called hello.txt but can't work it...
     
  7. CWannabe

    CWannabe New Member

    Joined:
    Oct 15, 2010
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    It's my first Test run on this code and i was most concern about compilation. I will implement the Stack.h when I get this phase of it solved.. I NEED URGENT GUIDANCE
     
  8. go4expert

    go4expert Moderator

    Joined:
    Aug 3, 2004
    Messages:
    306
    Likes Received:
    7
    Trophy Points:
    0
    What you are trying and what is your desired answer?
     
  9. CWannabe

    CWannabe New Member

    Joined:
    Oct 15, 2010
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    ok thanks, all the information is above. The Code above, What im trying to do is create a Simple Code Proof Reader using Stacks. The Program should prompt the user to Input a file location, e.g. C:\USER\Damion\Documents\somefile.txt

    ...or Possibly without the back slashes "\" however simple it can be done.. as I tried above.

    The program should then Open this file to search for Incorrect Pairs, or missing pairs in a C Program Code..its should push only characters to stack like ( then pop if ) is entered and such forth but for {[(" four character pairs.
    The text file would be a C Source Code.. pretty much like the hello world file. but as a .txt file, after code finds no errors it Prints "You Can Now Compile, your code is error free" or an "Error Warning Missing ')' ", if there is a fault.
     
  10. CWannabe

    CWannabe New Member

    Joined:
    Oct 15, 2010
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    A Simple Parenthesis (AND OTHER CHARACTER PAIRS) Checker in C Program Using Stacks.
     

Share This Page