2024-10-24 10:20:55 +00:00
|
|
|
#include <stdio.h>
|
2024-10-24 12:23:37 +00:00
|
|
|
#include <stdlib.h>
|
2024-10-24 10:20:55 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2024-10-24 12:23:37 +00:00
|
|
|
#define MAX_LENGTH 100
|
2024-10-24 10:20:55 +00:00
|
|
|
|
2024-10-24 12:24:53 +00:00
|
|
|
// Funkcia na kontrolu, či sú dva znaky pár skob
|
2024-10-24 12:23:37 +00:00
|
|
|
int isMatching(char opening, char closing) {
|
|
|
|
return (opening == '{' && closing == '}') ||
|
|
|
|
(opening == '[' && closing == ']') ||
|
|
|
|
(opening == '(' && closing == ')') ||
|
|
|
|
(opening == '<' && closing == '>');
|
2024-10-24 10:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2024-10-24 12:23:37 +00:00
|
|
|
char input[MAX_LENGTH + 1];
|
|
|
|
char stack[MAX_LENGTH];
|
2024-10-24 12:24:53 +00:00
|
|
|
int top = -1; // Index pre vrchol zásobníka
|
|
|
|
int position = 0; // Pozícia aktuálneho znaku
|
2024-10-24 12:23:37 +00:00
|
|
|
|
2024-10-24 12:24:53 +00:00
|
|
|
// Čítanie reťazca
|
2024-10-24 12:23:37 +00:00
|
|
|
printf("Zadajte kód (maximálne 100 znakov): ");
|
|
|
|
fgets(input, sizeof(input), stdin);
|
|
|
|
|
2024-10-24 12:24:53 +00:00
|
|
|
// Odstránenie znaku nového riadku, ak je prítomný
|
2024-10-24 12:23:37 +00:00
|
|
|
input[strcspn(input, "\n")] = 0;
|
|
|
|
|
2024-10-24 12:24:53 +00:00
|
|
|
// Kontrola dĺžky reťazca
|
2024-10-24 12:23:37 +00:00
|
|
|
if (strlen(input) > MAX_LENGTH) {
|
|
|
|
printf("Chyba: dĺžka reťazca presahuje 100 znakov.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2024-10-24 10:20:55 +00:00
|
|
|
|
2024-10-24 12:23:37 +00:00
|
|
|
printf("Načítané: %s\n", input);
|
2024-10-24 10:47:58 +00:00
|
|
|
|
2024-10-24 12:24:53 +00:00
|
|
|
// Prechod cez každý znak reťazca
|
2024-10-24 12:23:37 +00:00
|
|
|
for (int i = 0; i < strlen(input); i++) {
|
|
|
|
char current = input[i];
|
|
|
|
position++;
|
2024-10-24 10:20:55 +00:00
|
|
|
|
2024-10-24 12:24:53 +00:00
|
|
|
// Ak je aktuálny znak otváracia zátvorka, vložíme ju do zásobníka
|
2024-10-24 12:23:37 +00:00
|
|
|
if (current == '{' || current == '[' || current == '(' || current == '<') {
|
2024-10-24 12:24:53 +00:00
|
|
|
stack[++top] = current; // Zvýšiť top a vložiť zátvorku do zásobníka
|
2024-10-24 12:23:37 +00:00
|
|
|
}
|
2024-10-24 12:24:53 +00:00
|
|
|
// Ak je aktuálny znak zatváracia zátvorka
|
2024-10-24 12:23:37 +00:00
|
|
|
else if (current == '}' || current == ']' || current == ')' || current == '>') {
|
2024-10-24 12:24:53 +00:00
|
|
|
// Ak je zásobník prázdny, je to chyba
|
2024-10-24 10:28:31 +00:00
|
|
|
if (top == -1) {
|
2024-10-24 12:23:37 +00:00
|
|
|
printf("Neočekávaný znak %c na %d, očakávaná otváracia zátvorka.\n", current, position);
|
|
|
|
return 1;
|
2024-10-24 10:28:31 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 12:24:53 +00:00
|
|
|
// Ak zátvorky nesúhlasia, je to chyba
|
2024-10-24 12:23:37 +00:00
|
|
|
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;
|
2024-10-24 10:20:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-24 12:24:53 +00:00
|
|
|
// Ak zostali otvorené zátvorky v zásobníku, je to chyba
|
2024-10-24 10:20:55 +00:00
|
|
|
if (top != -1) {
|
2024-10-24 12:23:37 +00:00
|
|
|
printf("Neočekávaný koniec vstupu, očakávaná zatváracia zátvorka pre %c.\n", stack[top]);
|
|
|
|
return 1;
|
2024-10-24 10:20:55 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 12:36:39 +00:00
|
|
|
// Očakávané správne výstup
|
|
|
|
printf("Read: %s\n", input);
|
|
|
|
printf("All brackets OK\n");
|
|
|
|
|
2024-10-24 12:23:37 +00:00
|
|
|
return 0;
|
2024-10-24 10:20:55 +00:00
|
|
|
}
|
2024-10-24 12:36:39 +00:00
|
|
|
|