Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.23 KB | None | 0 0
  1. #define STACK_SIZE 100
  2. #include <stdio.h>
  3.  
  4. char stack[STACK_SIZE];
  5. int sp=0;
  6.  
  7.  
  8. /*
  9.  * Pushes character c onto the stack.
  10.  * Returns 0 if successful.
  11.  * Returns -1 if an error occurs.  (Stack full).
  12.  */
  13. int push (char c)
  14. {
  15.   if(sp < STACK_SIZE){
  16.     stack[sp] = c;
  17.     sp++;
  18.     return 0;
  19.   }
  20.   else{
  21.     return -1;
  22.   }
  23.     // TODO
  24. }
  25.  
  26. /*
  27.  * Pops next character off the stack.
  28.  * Returns the char if successful.
  29.  * Returns -1 if an error occurs.  (Stack empty).
  30.  */
  31. int pop ()
  32. {
  33.   if(sp == 0){
  34.     return -1;
  35.   }
  36.   else{
  37.     sp--;
  38.     return stack[sp];
  39.   }
  40.   // TODO
  41. }
  42.  
  43. /*
  44.  * Returns the current size of the stack.
  45.  */
  46. int stackSize()
  47. {
  48.   return sp;
  49.     // TODO
  50. }
  51.  
  52. /*
  53.  * Tests if the stack is correctly implemented
  54.  */
  55. int main ()
  56. {
  57.   int good = 0;
  58.   int linecount = 0;
  59.   int charcount = 0;
  60.   int currentchar;
  61.   int line = 0;
  62.   int position = 0;
  63.   char c = getChar();
  64.   while(c != EOF){
  65.     currentchar = push(c);
  66.     if(c == "(" || c == "[" || c == "{"){
  67.       if(currentchar == -1){
  68.     printf("Error: Stack Full!");
  69.     pop();
  70.     good = 1;
  71.     break;
  72.       }
  73.     }
  74.     else if(c == ")" && pop() == -1){
  75.       printf("Line %d, Char %d: Found ). No matching character.");
  76.       break;
  77.       good = 1;}
  78.     else if(c == "}" && pop() == -1){
  79.       printf("Line %d, Char %d: Found }. No matching character.");
  80.       break;
  81.       good = 1;}
  82.     else if(c == "]" && pop() == -1){
  83.       printf("Line %d, Char %d: Found ]. No matching character.");
  84.       break;}
  85.     else if(c == '\0' && currentchar == "("){
  86.       printf("Error: Expecting ), found end of input");
  87.       break;}
  88.     else if(c == '\0' && currentchar == "{"){
  89.       printf("Error: Expecting }, found end of input");
  90.       break;
  91.       good = 1;}
  92.     else if(c == '\0' && currentchar == "["){
  93.       printf("Error: Expecting ], found end of input");
  94.       break;
  95.       good = 1;}
  96.     else if(c == '\n')
  97.       linecount++;
  98.     else if(c == ")" && currentchar == "("){
  99.       pop();
  100.       charcount++;}
  101.     else if(c == ")" && currentchar == "{"){
  102.       printf("Line: %d, Char %d: Found ), expected }", linecount, charcount);
  103.       pop();
  104.       break;
  105.       good = 1;}
  106.     else if(c == ")" && currentchar == "["){
  107.       printf("Line: %d, Char %d: Found ), expected ]", linecount, charcount);
  108.       pop();
  109.       break;
  110.       good = 1;}
  111.     else if(c == "}" && currentchar == "{"){
  112.       pop();
  113.       charcount++;}
  114.     else if(c == "}" && currentchar == "("){
  115.       printf("Line: %d, Char %d: Found }, expected )", linecount, charcount);
  116.       pop();
  117.       break;
  118.       good = 1;}
  119.     else if(c == "}" && currentchar == "["){
  120.       printf("Line: %d, Char %d: Found }, expected ]", linecount, charcount);
  121.       pop();
  122.       break;
  123.       good = 1;}
  124.     else if(c == "]" && currentchar == "["){
  125.       pop();
  126.       charcount++;}
  127.     else if(c == "]" && currentchar == "("){
  128.       printf("Line: %d, Char %d: Found ], expected )", linecount, charcount);
  129.       pop();
  130.       break;
  131.       good = 1;}
  132.     else if(c == "]" && currentchar == "{"){
  133.       printf("Line: %d, Char %d: Found ], expected }", linecount, charcount);
  134.       pop();
  135.       break;
  136.       good = 1;}
  137.     c = getChar();
  138.   }
  139.   if(good == 0)
  140.     printf("Well formatted input.");
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement