Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4.  
  5. #define N 100
  6.  
  7. int8_t compare(char *i_ptr, int8_t size)
  8. {
  9. char c = ' ';
  10. bool error = 0;
  11. int8_t size_round = 0; // ()
  12. int8_t size_square = 0; // []
  13. int8_t size_curly = 0; // {}
  14.  
  15. do {
  16. switch (*i_ptr)
  17. {
  18. case '(':
  19. c = *i_ptr++; size_round++;
  20. break;
  21. case '[':
  22. c = *i_ptr++; size_square++;
  23. break;
  24. case '{':
  25. c = *i_ptr++; size_curly++;
  26. break;
  27. case ')':
  28. ((c == ')' || c == ']' || c == '}' || c == '(') && (size_round > 0)) ? size_round-- : error++;
  29. c = *i_ptr++;
  30. break;
  31. case ']':
  32. ((c == ')' || c == ']' || c == '}' || c == '[') && (size_square > 0)) ? size_square-- : error++;
  33. c = *i_ptr++;
  34. break;
  35. case '}':
  36. ((c == ')' || c == ']' || c == '}' || c == '{') && (size_curly > 0)) ? size_curly-- : error++;
  37. c = *i_ptr++;
  38. break;
  39. default:
  40. i_ptr++;
  41. break;
  42. }
  43.  
  44. } while ((*i_ptr) && (error == 0));
  45.  
  46. size = (error != 0) ? 1 : size_round + size_square + size_curly;
  47.  
  48. return size;
  49. }
  50.  
  51. int main()
  52. {
  53. int8_t size = 0;
  54. char arr[N];
  55. char *i_ptr = arr;
  56. printf("Enter line: ");
  57. gets_s(arr, N);
  58. size = compare(i_ptr, size);
  59. printf(size > 0 ? "Disbalance\n" : "Balance\n");
  60. system("pause");
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement