fork(2) download
  1.  
  2. #include<stdio.h>
  3. #include<malloc.h>
  4. #include<string.h>
  5.  
  6. struct Stack
  7. {
  8. int t, c;
  9. char* arr;
  10. };
  11.  
  12. struct Stack* create(int c)
  13. {
  14. struct Stack *S = (struct Stack*) malloc(sizeof(struct Stack));
  15. S->t = -1;
  16. S->c = c;
  17. S->arr = (char*)malloc(sizeof(char)* S->c);
  18. return S;
  19. }
  20.  
  21. int isEmpty(struct Stack *S)
  22. {
  23. return S->t == -1;
  24. }
  25.  
  26. char pop(struct Stack *S)
  27. {
  28. if (!isEmpty(S))
  29. return S->arr[S->t--];
  30. }
  31.  
  32. void push(struct Stack *S, char op)
  33. {
  34. S->arr[++S->t] = op;
  35. }
  36.  
  37. int match(char a, char b)
  38. {
  39. if (a == '(' && b == ')')
  40. return 1;
  41. else if (a == '{' && b == '}')
  42. return 1;
  43. else if (a == '[' && b == ']')
  44. return 1;
  45. else
  46. return 0;
  47. }
  48.  
  49.  
  50. int fun(char* exp)
  51. {
  52. int n = strlen(exp);
  53. struct Stack* S = create(n);
  54. if (!S)
  55. return 0;
  56. int i;
  57. while(exp[i])
  58. {
  59. if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
  60. push(S, exp[i]);
  61.  
  62. if (exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
  63. {
  64. if (S == NULL)
  65. return 0;
  66.  
  67. else if (! match(exp[i], pop(S)))
  68. return 0;
  69. }
  70. i++;
  71. }
  72. if (S == NULL) return 1;
  73. else return 0;
  74. }
  75.  
  76.  
  77. int main()
  78. {
  79. char exp[] = "{()}[]";
  80. int a=fun(exp);
  81. printf("%d",a);
  82. if (fun(exp))
  83. printf("Balanced");
  84. else
  85. printf("Not Balanced");
  86.  
  87. return 0;
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
0Not Balanced