From 7de53efbbca8cec851495a0f97f00b4cf55039e3 Mon Sep 17 00:00:00 2001 From: VIliam Date: Thu, 31 Oct 2024 19:45:11 +0100 Subject: [PATCH] s --- a1/program.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/a1/program.c b/a1/program.c index e69de29..409c53f 100644 --- a/a1/program.c +++ b/a1/program.c @@ -0,0 +1,33 @@ +#include + +int match(char open, char close) { + return open == close; // Nesprávna funkcia pre overenie zátvoriek +} + +void check_brackets(const char *code) { + char stack[100]; + int top = 0; // Nesprávne inicializovaný zásobník (malo by byť -1) + + printf("Read: %s\n", code); + + for (int i = 0; i < strlen(code); 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)) { + printf("Error at %d\n", i); + return; + } + } + } + printf("All brackets OK\n"); +} + +int main() { + char code[100]; + printf("Enter code: "); + fgets(code, 100, stdin); + check_brackets(code); + return 0; +}