#include #include #define MAX 101 // vracia zodpovedajúcu zatváraciu zátvorku char expected_close(char open) { switch (open) { case '(': return ')'; case '{': return '}'; case '[': return ']'; case '<': return '>'; default: return 0; } } int main(void) { char line[MAX]; char stack[MAX]; int pos[MAX]; // pozície otvorených zátvoriek (0-indexed) int top = -1; if (fgets(line, MAX, stdin) == NULL) return 0; // odstrániť newline ak je prítomné line[strcspn(line, "\n")] = '\0'; printf("Read: %s\n", line); int ok = 1; int length = strlen(line); for (int i = 0; i < length; i++) { char ch = line[i]; if (ch == '(' || ch == '{' || ch == '[' || ch == '<') { top++; stack[top] = ch; pos[top] = i; // uložíme 0-indexovanú pozíciu } else if (ch == ')' || ch == '}' || ch == ']' || ch == '>') { if (top < 0) { // prázdny zásobník -> neočakávané zatvorenie, pozícia i (0-index) printf("Unexpected closing bracket %c in %d\n", ch, i); ok = 0; break; } char last_open = stack[top]; char expected = expected_close(last_open); if (ch != expected) { // prekríženie: reportujeme aktuálnu pozíciu i (0-index) printf("Crossed bracket %c in %d, expected %c\n", ch, i, expected); ok = 0; break; } top--; } } if (ok && top >= 0) { // chýba zatváracia zátvorka pre poslednú otvorenú -- pozícia sa vypisuje 0-index printf("Missing closing bracket for %c from %d\n", stack[top], pos[top]); ok = 0; } if (ok) printf("All brackets OK\n"); return 0; }