72 lines
1.8 KiB
C
72 lines
1.8 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define MAX_LENGTH 100
|
|
|
|
|
|
int isMatching(char opening, char closing) {
|
|
return (opening == '{' && closing == '}') ||
|
|
(opening == '[' && closing == ']') ||
|
|
(opening == '(' && closing == ')') ||
|
|
(opening == '<' && closing == '>');
|
|
}
|
|
|
|
int main() {
|
|
char input[MAX_LENGTH + 1];
|
|
char stack[MAX_LENGTH];
|
|
int top = -1;
|
|
int position = 0;
|
|
|
|
|
|
printf("Zadajte kód (maximálne 100 znakov): ");
|
|
fgets(input, sizeof(input), stdin);
|
|
|
|
|
|
input[strcspn(input, "\n")] = 0;
|
|
|
|
|
|
if (strlen(input) > MAX_LENGTH) {
|
|
printf("Chyba: dĺžka reťazca presahuje 100 znakov.\n");
|
|
return 1;
|
|
}
|
|
|
|
printf("Načítané: %s\n", input);
|
|
|
|
|
|
for (int i = 0; i < strlen(input); i++) {
|
|
char current = input[i];
|
|
position++;
|
|
|
|
|
|
if (current == '{' || current == '[' || current == '(' || current == '<') {
|
|
stack[++top] = current; \
|
|
}
|
|
|
|
else if (current == '}' || current == ']' || current == ')' || current == '>') {
|
|
|
|
if (top == -1) {
|
|
printf("Neočekávaný znak %c na %d, očakávaná otváracia zátvorka.\n", current, position);
|
|
return 1;
|
|
}
|
|
|
|
|
|
if (!isMatching(stack[top--], current)) {
|
|
printf("Prekrývajúca sa zátvorka %c na %d, očakávaná %c.\n", current, position,
|
|
(current == '}') ? '{' : (current == ']') ? '[' :
|
|
(current == ')') ? '(' : '<');
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if (top != -1) {
|
|
printf("Neočekávaný koniec vstupu, očakávaná zatváracia zátvorka pre %c.\n", stack[top]);
|
|
return 1;
|
|
}
|
|
|
|
printf("Všetky zátvorky sú v poriadku\n");
|
|
return 0;
|
|
}
|