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.
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
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".
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
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...
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
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.