From 8890bbf80fb6771354934b7f23ef907fc4e203f7 Mon Sep 17 00:00:00 2001 From: nk365yf Date: Mon, 20 Oct 2025 19:59:56 +0200 Subject: [PATCH] try other --- a1/prog | Bin 16312 -> 16312 bytes a1/program.c | 125 +++++++++++++++++++-------------------------------- 2 files changed, 47 insertions(+), 78 deletions(-) diff --git a/a1/prog b/a1/prog index 521b61e471a18d01f8ff5e2c5f2a9fb8d189ac6b..d3fd74c7869819d972fd03fb0f3883510a84edd1 100755 GIT binary patch delta 405 zcmdl{zoUKw2P5Y}b_OswxLJ@fok=8K&{8NncivA=<7YRd7M;6y_fi+f<{suiLB?g1 z?}=1OSu-&(9B&o*@gEG1w@Lt6ApI|OCl`uFGoG4!R#csF(d4h9W{i20b;Q(T#kyMs zfNGjwFnY}3-`2va0OauRJJ3B9Ec%L(f8SPqpit*Qk6zZLoD2*eoyT{841D46>;Hc+ z>m?&214A>|CPs*1mS7Hl&%w!a#O&QooA-j!%{|P4f{gu> z?}=1OsW34x9B&o*@gEG1w@Lt6ApI{TCl`uFGj5xFR#cs_Z}L}BGsdXNI%4XctUK(1 z(%n-9fC~BdZRH12od-R7S$A?WFnDwx-vQG6BIMWq|6tb3e?V?C*i^>uR*)V`Fo(bA z>f{Y#_AW-vd%M@|HfykyF`T76<>q4Mt0') { - if (top == -1) { - *err = i +1; - return 0; - } - char last = stack[top--]; - if ((c == ')' && last != '(') || - (c == ']' && last != '[') || - (c == '}' && last != '{') || - (c == '>' && last != '<')) { - *err = i+1; - return 0; - } - } - } - if (top != -1) { - // Знаходимо позицію незакритої дужки - char lastOpen = stack[top]; - for (int i = 0; s[i] != '\0'; i++) { - if (s[i] == lastOpen) { - *err = i +1; - return 0; - } - } - } - return 1; -} - - char getExpectedClosing(char open) { switch (open) { case '(': return ')'; case '[': return ']'; case '{': return '}'; case '<': return '>'; - default: return '?'; + default: return '?'; } } + +bool isOpening(char c) { + return c == '(' || c == '[' || c == '{' || c == '<'; +} + +bool isClosing(char c) { + return c == ')' || c == ']' || c == '}' || c == '>'; +} + +char getExpectedOpening(char close) { + switch (close) { + case ')': return '('; + case ']': return '['; + case '}': return '{'; + case '>': return '<'; + default: return '?'; + } +} + int main() { char input[MAX_SIZE]; - if (fgets(input, MAX_SIZE, stdin) != NULL) { input[strcspn(input, "\n")] = '\0'; printf("Read: %s\n", input); - int err = 0; - if (is_valid_brackets(input, &err)) { - printf("All brackets OK\n"); + char stack[MAX_SIZE]; + int top = -1; + + for (int i = 0; input[i] != '\0'; i++) { + char c = input[i]; + if (isOpening(c)) { + stack[++top] = c; + } else if (isClosing(c)) { + if (top == -1) { + printf("Unexpected closing bracket %c in %d\n", c, i); + return 0; + } + char last = stack[top--]; + if (getExpectedClosing(last) != c) { + printf("Crossed bracket %c in %d, expected %c \n", c, i , getExpectedClosing(last)); + return 0; + } + } + } + + if (top != -1) { + printf("Missing closing brackets: "); + for (int i = top; i >= 0; i--) { + printf("%c", getExpectedClosing(stack[i])); + } + printf("\n"); } else { - char stack[MAX_SIZE]; - int top = -1; - - for (int i = 0; i < err; i++) { - char c = input[i]; - if (c == '(' || c == '[' || c == '{' || c == '<') { - stack[++top] = c; - } else if (c == ')' || c == ']' || c == '}' || c == '>') { - if (top >= 0 && c == getExpectedClosing(stack[top])) { - top--; - } else { - printf("Crossed bracket %c in %d, expected %c\n", - c, err, (top >= 0) ? getExpectedClosing(stack[top]) : '?'); - return 0; - } - } - } - - char c = input[err - 1]; - if (c == ')' || c == ']' || c == '}' || c == '>') { - if (top < -1) { - printf("Unexpected closing bracket %c in %d\n", c, err); - } - } else { - printf("Missing closing brackets: "); - for (int i = top; i > -1; i--) { - printf("%c", getExpectedClosing(stack[i])); - } - printf("\n"); - } + printf("All brackets OK\n"); } } else { printf("Error reading input\n");