From 8457a6d283ba1a10e09240022d2b192ff6a4f7d2 Mon Sep 17 00:00:00 2001 From: Bohdan Kapliuk Date: Sun, 27 Oct 2024 22:32:13 +0200 Subject: [PATCH] a1 --- a1/program.c | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/a1/program.c b/a1/program.c index da77052..b5bd415 100644 --- a/a1/program.c +++ b/a1/program.c @@ -1,8 +1,44 @@ #include #include -#include -#include -int main(){ +#define LINESIZE 100 + +int main() { + char vstup[LINESIZE]; + char *brackets = "{[(<"; + char *brackets1 = "}])>"; + char stack[LINESIZE]; + int position[LINESIZE]; + int x = 0; + fgets(vstup, LINESIZE, stdin); + for (int j = 0; j < strlen(vstup); j++) { + for (int i = 0; i < 4; i++) { + if (vstup[j] == brackets[i]) { + stack[x] = brackets[i]; + position[x] = j; + x++; + break; + } else if (vstup[j] == brackets1[i]) { + char expected; + if (stack[x-1] == '{') expected = '}'; + else if (stack[x-1] == '[') expected = ']'; + else if (stack[x-1] == '(') expected = ')'; + else if (stack[x-1] == '<') expected = '>'; + + if (vstup[j] != expected) { + printf("Crossed bracket %c in %d, expected %c\n", vstup[j], j, expected); + return 0; + } + x--; + break; + } + } + } + if (x > 0) { + printf("Unmatched opening bracket %c at position %d\n", stack[x-1], position[x-1]); + } else { + printf("All brackets OK\n"); + } + return 0; } \ No newline at end of file