Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define STACK_SIZE 100
- #include <stdio.h>
- char stack[STACK_SIZE];
- int sp=0;
- /*
- * Pushes character c onto the stack.
- * Returns 0 if successful.
- * Returns -1 if an error occurs. (Stack full).
- */
- int push (char c)
- {
- if(sp < STACK_SIZE){
- stack[sp] = c;
- sp++;
- return 0;
- }
- else{
- return -1;
- }
- // TODO
- }
- /*
- * Pops next character off the stack.
- * Returns the char if successful.
- * Returns -1 if an error occurs. (Stack empty).
- */
- int pop ()
- {
- if(sp == 0){
- return -1;
- }
- else{
- sp--;
- return stack[sp];
- }
- // TODO
- }
- /*
- * Returns the current size of the stack.
- */
- int stackSize()
- {
- return sp;
- // TODO
- }
- /*
- * Tests if the stack is correctly implemented
- */
- int main ()
- {
- int good = 0;
- int linecount = 0;
- int charcount = 0;
- int currentchar;
- int line = 0;
- int position = 0;
- char c = getChar();
- while(c != EOF){
- currentchar = push(c);
- if(c == "(" || c == "[" || c == "{"){
- if(currentchar == -1){
- printf("Error: Stack Full!");
- pop();
- good = 1;
- break;
- }
- }
- else if(c == ")" && pop() == -1){
- printf("Line %d, Char %d: Found ). No matching character.");
- break;
- good = 1;}
- else if(c == "}" && pop() == -1){
- printf("Line %d, Char %d: Found }. No matching character.");
- break;
- good = 1;}
- else if(c == "]" && pop() == -1){
- printf("Line %d, Char %d: Found ]. No matching character.");
- break;}
- else if(c == '\0' && currentchar == "("){
- printf("Error: Expecting ), found end of input");
- break;}
- else if(c == '\0' && currentchar == "{"){
- printf("Error: Expecting }, found end of input");
- break;
- good = 1;}
- else if(c == '\0' && currentchar == "["){
- printf("Error: Expecting ], found end of input");
- break;
- good = 1;}
- else if(c == '\n')
- linecount++;
- else if(c == ")" && currentchar == "("){
- pop();
- charcount++;}
- else if(c == ")" && currentchar == "{"){
- printf("Line: %d, Char %d: Found ), expected }", linecount, charcount);
- pop();
- break;
- good = 1;}
- else if(c == ")" && currentchar == "["){
- printf("Line: %d, Char %d: Found ), expected ]", linecount, charcount);
- pop();
- break;
- good = 1;}
- else if(c == "}" && currentchar == "{"){
- pop();
- charcount++;}
- else if(c == "}" && currentchar == "("){
- printf("Line: %d, Char %d: Found }, expected )", linecount, charcount);
- pop();
- break;
- good = 1;}
- else if(c == "}" && currentchar == "["){
- printf("Line: %d, Char %d: Found }, expected ]", linecount, charcount);
- pop();
- break;
- good = 1;}
- else if(c == "]" && currentchar == "["){
- pop();
- charcount++;}
- else if(c == "]" && currentchar == "("){
- printf("Line: %d, Char %d: Found ], expected )", linecount, charcount);
- pop();
- break;
- good = 1;}
- else if(c == "]" && currentchar == "{"){
- printf("Line: %d, Char %d: Found ], expected }", linecount, charcount);
- pop();
- break;
- good = 1;}
- c = getChar();
- }
- if(good == 0)
- printf("Well formatted input.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement