s
This commit is contained in:
parent
d8c2ae1115
commit
f875ace078
19
a1/program.c
19
a1/program.c
@ -2,7 +2,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
int match(char open, char close) {
|
int match(char open, char close) {
|
||||||
return (open == '{' && close == '}') || (open == '(' && close == ')');
|
return (open == '{' && close == '}') || (open == '[' && close == ']') ||
|
||||||
|
(open == '(' && close == ')');
|
||||||
}
|
}
|
||||||
|
|
||||||
void check_brackets(const char *code) {
|
void check_brackets(const char *code) {
|
||||||
@ -13,13 +14,15 @@ void check_brackets(const char *code) {
|
|||||||
|
|
||||||
for (int i = 0; code[i] != '\0'; i++) {
|
for (int i = 0; code[i] != '\0'; i++) {
|
||||||
char c = code[i];
|
char c = code[i];
|
||||||
if (c == '{' || c == '(') {
|
if (c == '{' || c == '[' || c == '(') {
|
||||||
if (top < 99) {
|
stack[++top] = c;
|
||||||
stack[++top] = c;
|
} else if (c == '}' || c == ']' || c == ')') {
|
||||||
|
if (top == -1) {
|
||||||
|
printf("Unexpected closing bracket %c at position %d\n", c, i);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} else if (c == '}' || c == ')') {
|
if (!match(stack[top], c)) {
|
||||||
if (top == -1 || !match(stack[top], c)) {
|
printf("Crossed bracket %c in %d, expected %c\n", c, i, stack[top]);
|
||||||
printf("Error at %d\n", i);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
top--;
|
top--;
|
||||||
@ -28,7 +31,7 @@ void check_brackets(const char *code) {
|
|||||||
if (top == -1) {
|
if (top == -1) {
|
||||||
printf("All brackets OK\n");
|
printf("All brackets OK\n");
|
||||||
} else {
|
} else {
|
||||||
printf("Unmatched brackets\n");
|
printf("Unmatched opening bracket %c\n", stack[top]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user