Update 'a1/program.c'
This commit is contained in:
parent
4aeaedded0
commit
7f2b626453
21
a1/program.c
21
a1/program.c
@ -1,6 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include<ctype.h>
|
||||
|
||||
int open_brackets(char a) {
|
||||
return (a == '(' || a == '[' || a == '{' || a == '<');
|
||||
@ -27,22 +28,25 @@ char brackets(char a) {
|
||||
|
||||
int main() {
|
||||
char code[100];
|
||||
printf("");
|
||||
printf("Read: ");
|
||||
fgets(code, sizeof(code), stdin);
|
||||
|
||||
int stack[100];
|
||||
int skobochka = -1;
|
||||
int error = -1;
|
||||
char letters[100];
|
||||
int letter_index = 0;
|
||||
int letter_i = 0;
|
||||
|
||||
for (int i = 0; code[i] != '\0'; i++) {
|
||||
if (isalpha(code[i])) {
|
||||
letters[letter_index++] = code[i];
|
||||
printf("%c", code[i]);
|
||||
letters[letter_i++] = code[i];
|
||||
}
|
||||
if (open_brackets(code[i])) {
|
||||
printf(" ");
|
||||
stack[++skobochka] = i;
|
||||
} else if (close_brackets(code[i])) {
|
||||
printf("%c", code[i]);
|
||||
if (skobochka == -1) {
|
||||
error = i;
|
||||
break;
|
||||
@ -56,15 +60,12 @@ int main() {
|
||||
}
|
||||
|
||||
if (error == -1 && skobochka == -1) {
|
||||
printf("Read: ");
|
||||
for (int i = 0; i < letter_index; i++) {
|
||||
printf("%c", letters[i]);
|
||||
}
|
||||
printf("\nAll brackets OK\n");
|
||||
} else if (error == -1) {
|
||||
printf("Crossed bracket %c in %d, expected %c\n", code[error], error, brackets(code[error]));
|
||||
printf("\nClosing bracket at position %d is not paired\n", stack[skobochka]);
|
||||
} else {
|
||||
printf("Unexpected closing bracket in % d \n", stack[skobochka]);
|
||||
printf("\nUnexpected closing bracket %c in %d\n", code[error], error);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user