Update 'a1/program.c'
This commit is contained in:
parent
a047c1eb94
commit
215c3c7fab
23
a1/program.c
23
a1/program.c
@ -1,5 +1,9 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
|
||||
|
||||
|
||||
int open_brackets(char a) {
|
||||
return (a == '(' || a == '[' || a == '{' || a == '<');
|
||||
@ -26,18 +30,24 @@ char brackets(char a) {
|
||||
|
||||
int main() {
|
||||
char code[100];
|
||||
printf("Read: ");
|
||||
printf("");
|
||||
fgets(code, sizeof(code), stdin);
|
||||
|
||||
int stack[100];
|
||||
int skobochka = -1;
|
||||
int error = -1;
|
||||
char letters[100];
|
||||
int letter_index = 0;
|
||||
|
||||
printf("Read: ");
|
||||
for (int i = 0; code[i] != '\0'; i++) {
|
||||
if (isalpha(code[i])) {
|
||||
letters[letter_index++] = code[i];
|
||||
}
|
||||
if (open_brackets(code[i])) {
|
||||
stack[++skobochka] = i;
|
||||
} else if (close_brackets(code[i])) {
|
||||
if (skobochka == -1) {
|
||||
if (skobochka == -1) {
|
||||
error = i;
|
||||
break;
|
||||
}
|
||||
@ -50,11 +60,14 @@ int main() {
|
||||
}
|
||||
|
||||
if (error == -1 && skobochka == -1) {
|
||||
printf("All brackets OK\n");
|
||||
for (int i = 0; i < letter_index; i++) {
|
||||
printf("%c", letters[i]);
|
||||
}
|
||||
printf("\nAll brackets OK\n");
|
||||
} else if (error == -1) {
|
||||
printf("Crossed bracket %d expected\n", stack[skobochka]);
|
||||
printf("Crossed bracket %c in %d, expected %c\n", code[error], error, brackets(code[error]));
|
||||
} else {
|
||||
printf("Prekrížené zátvorky %c v pozícii %d, sa očakávalo %c\n", code[error], error, brackets(code[error]));
|
||||
printf("Bracket on position %d not closed\n", stack[skobochka]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user