usaa21/a2/program.c
2021-11-26 15:05:20 +01:00

138 lines
3.3 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define LINESIZE 100
int i = 0;
int hodnota = 0;
int a = 0;
int hej = 0;
int p = 0;
int main(){
char r[LINESIZE];
memset(r, 0, LINESIZE);
fgets(r, LINESIZE, stdin);
size_t n;
n = strlen(r);
//printf("%ld", n);
while(r[i] != '\n'){
//puts("1");
if (r[0] != '{' || r[0] != '(' || r[0] != '[' || r[0] != '<') {
i = n - 2;
hej = 1;
//puts("halo");
}
if (r[0] == '}'){
hej = 3;
printf("Unexpected closing bracket } in %d\n", i);
i = n - 2;
}
if (r[i] == '{') {
hodnota = 1;
i++;
}
else if (r[i] == '(') {
hodnota = 2;
i++;
}
else if (r[i] == '[') {
hodnota = 3;
i++;
}
else if (r[i] == '<') {
hodnota = 4;
i++;
}
//printf("hodnota: %d\n", hodnota);
if (hodnota == 1){
hodnota = 0;
for (a = p; a<n; a++){
printf("%d", a);
if (r[a] == '}') {
puts("joj");
hej = 1;
//r[a] = 'a';
p = a;
a = n;
}
else if (r[a] == ')' || r[a] == ']' || r[a] == '>'){
puts("nie");
hej = 0;
a = n;
i = n - 1;
}
//hodnota = 0;
}
}
else if (hodnota == 2){
hodnota = 0;
for (a = p; a>n; a++){
if (r[a] == ')') {
hej = 1;
//r[a] = 'a';
p = a;
a = n;
}
else if (r[a] == '}' || r[a] == ']' || r[a] == '>'){
puts("nasiel");
hej = 0;
a = n;
i = n - 1;
}
}
}
else if (hodnota == 3) {
hodnota = 0;
for (a = p; a < n; a++) {
//puts("neidem");
if (r[a] == ']') {
//puts("nasiel som");
hej = 1;
//r[a] = 'a';
p = a;
a = n - 1;
}
else if (r[a] == '}' || r[a] == ')' || r[a] == '>'){
//puts("nie");
hej = 0;
a = n;
i = n - 1;
}
}
}
else if (hodnota == 4){
hodnota = 0;
for (a = p; a>n; a++){
if (r[a] == '>') {
hej = 1;
//r[a] = 'a';
p = a;
a = n;
}
else if (r[a] == '}' || r[a] == ']' || r[a] == ')'){
puts("nasiel");
hej = 0;
a = n;
i = n - 1;
}
}
}
else{
i++;
}
}
if (hej == 1){
printf("Read: %s", r);
puts("All brackets OK");
}
if (hej == 0){
printf("Read: %s\nCrossed bracket OK\n", r);
//printf("%s\n", r);
}
return 0;
}