Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdint.h>
- #define N 100
- int8_t compare(char *i_ptr, int8_t size)
- {
- char c = ' ';
- bool error = 0;
- int8_t size_round = 0; // ()
- int8_t size_square = 0; // []
- int8_t size_curly = 0; // {}
- do {
- switch (*i_ptr)
- {
- case '(':
- c = *i_ptr++; size_round++;
- break;
- case '[':
- c = *i_ptr++; size_square++;
- break;
- case '{':
- c = *i_ptr++; size_curly++;
- break;
- case ')':
- ((c == ')' || c == ']' || c == '}' || c == '(') && (size_round > 0)) ? size_round-- : error++;
- c = *i_ptr++;
- break;
- case ']':
- ((c == ')' || c == ']' || c == '}' || c == '[') && (size_square > 0)) ? size_square-- : error++;
- c = *i_ptr++;
- break;
- case '}':
- ((c == ')' || c == ']' || c == '}' || c == '{') && (size_curly > 0)) ? size_curly-- : error++;
- c = *i_ptr++;
- break;
- default:
- i_ptr++;
- break;
- }
- } while ((*i_ptr) && (error == 0));
- size = (error != 0) ? 1 : size_round + size_square + size_curly;
- return size;
- }
- int main()
- {
- int8_t size = 0;
- char arr[N];
- char *i_ptr = arr;
- printf("Enter line: ");
- gets_s(arr, N);
- size = compare(i_ptr, size);
- printf(size > 0 ? "Disbalance\n" : "Balance\n");
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement