#include #include #include #include #include #include #define BRACKETS_NUM 100 typedef struct Node{ char data; struct Node* next; } LinkedNode; LinkedNode* createNode(char data){ LinkedNode* node = (LinkedNode*)malloc(sizeof(LinkedNode)); node->data = data; node->next = NULL; return node; } void push(LinkedNode** head, char data){ if(*head == NULL){ LinkedNode* node = createNode(data); *head = node; return; } LinkedNode* cur = *head; while(cur->next){ cur = cur->next; } cur->next = createNode(data); } bool compare(char data1, char data2){ if(data1 == '(' && data2 == ')'){ return true; } else if(data1 == '<' && data2 == '>'){ return true; } else if (data1 == '[' && data2 == ']'){ return true; } else if (data1 == '{' && data2 == '}'){ return true; } return false; } char pop(LinkedNode** head){ LinkedNode* prev = NULL; LinkedNode* ptr = *head; while (ptr->next) { prev = ptr; ptr = ptr->next; } char res = ptr->data; if(prev){ prev->next = NULL;} else{ *head = NULL; } free(ptr); return res; } void read_input(LinkedNode** head){ char buff[BRACKETS_NUM]; fgets(buff, 100, stdin); int i =0; while (buff[i]) { if(buff[i] == '[' || buff[i] == '<' || buff[i] == '{' || buff[i] == '('){ push(head, buff[i]);} if(buff[i] == '>' || buff[i] == '}' || buff[i] == ']' || buff[i] == ')'){ if(*head == NULL){ printf("Read: %sUnexpected closing bracket %c in %d\n", buff, buff[i], i); return; } char popped = pop(head); if (!compare(popped, buff[i])) { if (popped == '[' && buff[i] == '>') { printf("Read: %s\n", buff); printf("Crossed bracket %c", buff[i]); printf(" in %d,", i); printf(" expected ]\n"); return; } if (popped == '(' && buff[i] == '>') { printf("Read: %s\n", buff); printf("Crossed bracket %c", buff[i]); printf(" in %d,", i); printf(" expected )\n"); return; } if (popped == '{' && buff[i] == '>') { printf("Read: %s\n", buff); printf("Crossed bracket %c", buff[i]); printf(" in %d,", i); printf(" expected }\n"); return; } if (popped == '<' && buff[i] == ']') { printf("Read: %s\n", buff); printf("Square bracket %c", buff[i]); printf(" in %d,", i); printf(" expected >\n"); return; } if (popped == '(' && buff[i] == ']') { printf("Read: %s\n", buff); printf("Square bracket %c", buff[i]); printf(" in %d,", i); printf(" expected )\n"); return; } if (popped == '{' && buff[i] == ']') { printf("Read: %s\n", buff); printf("Square bracket %c", buff[i]); printf(" in %d,", i); printf(" expected }\n"); return; } if (popped == '<' && buff[i] == ')') { printf("Read: %s\n", buff); printf("Round bracket %c", buff[i]); printf(" in %d,", i); printf(" expected >\n"); return; } if (popped == '[' && buff[i] == ')') { printf("Read: %s\n", buff); printf("Round bracket %c", buff[i]); printf(" in %d,", i); printf(" expected ]\n"); return; } if (popped == '{' && buff[i] == ')') { printf("Read: %s\n", buff); printf("Round bracket %c", buff[i]); printf(" in %d,", i); printf(" expected }\n"); return; } if (popped == '<' && buff[i] == '}') { printf("Read: %s\n", buff); printf("Curly bracket %c", buff[i]); printf(" in %d,", i); printf(" expected >\n"); return; } if (popped == '(' && buff[i] == '}') { printf("Read: %s\n", buff); printf("Curly bracket %c", buff[i]); printf(" in %d,", i); printf(" expected )\n"); return; } if (popped == '[' && buff[i] == '}') { printf("Read: %s\n", buff); printf("Curly bracket %c", buff[i]); printf(" in %d,", i); printf(" expected ]\n"); return; } } } i++; } if(*head != NULL){ printf("Read: %sMissing closing brackets: ", buff); LinkedNode* ptr = *head; while(ptr){ if(ptr->data == '['){ putchar(']'); } if(ptr->data == '('){ putchar(')'); } if(ptr->data == '{'){ putchar('}'); } if(ptr->data == '<'){ putchar('>'); } ptr = ptr->next; } putchar('\n'); return; } printf("Read: %sAll brackets OK\n", buff); } int main(void){ LinkedNode* head = NULL; read_input(&head); return 0; }