This commit is contained in:
Džubara 2024-10-31 19:46:23 +01:00
parent 7de53efbbc
commit d8c2ae1115

View File

@ -1,27 +1,35 @@
#include <stdio.h> #include <stdio.h>
#include <string.h>
int match(char open, char close) { 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) { void check_brackets(const char *code) {
char stack[100]; char stack[100];
int top = 0; // Nesprávne inicializovaný zásobník (malo by byť -1) int top = -1;
printf("Read: %s\n", code); 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]; char c = code[i];
if (c == '{' || c == '(' || c == '[') { if (c == '{' || c == '(') {
stack[top++] = c; // Nesprávne ukladá do zásobníka if (top < 99) {
} else if (c == '}' || c == ')' || c == ']') { stack[++top] = c;
if (top == 0 || !match(stack[top--], c)) { }
} else if (c == '}' || c == ')') {
if (top == -1 || !match(stack[top], c)) {
printf("Error at %d\n", i); printf("Error at %d\n", i);
return; return;
} }
top--;
} }
} }
if (top == -1) {
printf("All brackets OK\n"); printf("All brackets OK\n");
} else {
printf("Unmatched brackets\n");
}
} }
int main() { int main() {