From d8c2ae111505c997c44fe176c080434390e3933f Mon Sep 17 00:00:00 2001 From: VIliam Date: Thu, 31 Oct 2024 19:46:23 +0100 Subject: [PATCH] s --- a1/program.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/a1/program.c b/a1/program.c index 409c53f..2b68a48 100644 --- a/a1/program.c +++ b/a1/program.c @@ -1,27 +1,35 @@ #include +#include int match(char open, char close) { - return open == close; // Nesprávna funkcia pre overenie zátvoriek + return (open == '{' && close == '}') || (open == '(' && close == ')'); } void check_brackets(const char *code) { char stack[100]; - int top = 0; // Nesprávne inicializovaný zásobník (malo by byť -1) + int top = -1; printf("Read: %s\n", code); - for (int i = 0; i < strlen(code); i++) { + for (int i = 0; code[i] != '\0'; i++) { char c = code[i]; - if (c == '{' || c == '(' || c == '[') { - stack[top++] = c; // Nesprávne ukladá do zásobníka - } else if (c == '}' || c == ')' || c == ']') { - if (top == 0 || !match(stack[top--], c)) { + if (c == '{' || c == '(') { + if (top < 99) { + stack[++top] = c; + } + } else if (c == '}' || c == ')') { + if (top == -1 || !match(stack[top], c)) { printf("Error at %d\n", i); return; } + top--; } } - printf("All brackets OK\n"); + if (top == -1) { + printf("All brackets OK\n"); + } else { + printf("Unmatched brackets\n"); + } } int main() { @@ -30,4 +38,4 @@ int main() { fgets(code, 100, stdin); check_brackets(code); return 0; -} +} \ No newline at end of file