a1
This commit is contained in:
parent
ad46eb5145
commit
8457a6d283
42
a1/program.c
42
a1/program.c
@ -1,8 +1,44 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user