usaa21/a2/program.c

138 lines
3.3 KiB
C
Raw Normal View History

2021-11-26 13:49:28 +00:00
#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");
2021-11-26 13:58:55 +00:00
if (r[0] != '{' || r[0] != '(' || r[0] != '[' || r[0] != '<') {
i = n - 2;
hej = 1;
//puts("halo");
}
2021-11-26 14:05:20 +00:00
if (r[0] == '}'){
hej = 3;
printf("Unexpected closing bracket } in %d\n", i);
i = n - 2;
}
2021-11-26 13:49:28 +00:00
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){
2021-11-26 13:58:55 +00:00
printf("Read: %s", r);
puts("All brackets OK");
2021-11-26 13:49:28 +00:00
}
if (hej == 0){
2021-11-26 13:58:55 +00:00
printf("Read: %s\nCrossed bracket OK\n", r);
2021-11-26 13:49:28 +00:00
//printf("%s\n", r);
}
return 0;
}