Update a1/program.c

This commit is contained in:
Marat Izmailov 2024-10-31 20:52:38 +00:00
parent d54ea6c6c8
commit 377897bc64

View File

@ -24,7 +24,6 @@ int main() {
char input[MAX_LENGTH + 1]; char input[MAX_LENGTH + 1];
char stack[MAX_LENGTH]; char stack[MAX_LENGTH];
int top = -1; // Stack pointer int top = -1; // Stack pointer
int position = -1; // Start position at -1 to adjust to 0-based indexing
int foundBracket = 0; // Flag to check if any bracket is found int foundBracket = 0; // Flag to check if any bracket is found
// Reading the string // Reading the string
@ -35,14 +34,16 @@ int main() {
// Check if string length exceeds MAX_LENGTH // Check if string length exceeds MAX_LENGTH
if (strlen(input) > MAX_LENGTH) { if (strlen(input) > MAX_LENGTH) {
printf("Chyba: dĺžka reťazca presahuje 100 znakov.\n"); printf("Error: Input length exceeds 100 characters.\n");
return 1; return 1;
} }
// Print the input line for the output format
printf("Read: %s\n", input);
// Iterating over each character in the string // Iterating over each character in the string
for (int i = 0; i < strlen(input); i++) { for (int i = 0; i < strlen(input); i++) {
char current = input[i]; char current = input[i];
position++;
// Check if current character is an opening bracket // Check if current character is an opening bracket
if (current == '{' || current == '[' || current == '(' || current == '<') { if (current == '{' || current == '[' || current == '(' || current == '<') {
@ -53,15 +54,13 @@ int main() {
else if (current == '}' || current == ']' || current == ')' || current == '>') { else if (current == '}' || current == ']' || current == ')' || current == '>') {
// If stack is empty, it's an error // If stack is empty, it's an error
if (top == -1) { if (top == -1) {
printf("Read: %s\n", input); printf("Unexpected closing bracket %c in %d\n", current, i);
printf("Unexpected closing bracket %c in %d\n", current, position);
return 0; return 0;
} }
// If brackets do not match, it's an error // If brackets do not match, it's an error
if (!isMatching(stack[top--], current)) { if (!isMatching(stack[top--], current)) {
printf("Read: %s\n", input); printf("Crossed bracket %c in %d, expected %c.\n", current, i,
printf("Crossed bracket %c in %d, expected %c.\n", current, position,
getExpectedClosingBracket(stack[top + 1])); getExpectedClosingBracket(stack[top + 1]));
return 0; return 0;
} }
@ -70,18 +69,15 @@ int main() {
// If there are unmatched opening brackets left in the stack // If there are unmatched opening brackets left in the stack
if (top != -1) { if (top != -1) {
printf("Read: %s\n", input);
printf("Missing closing brackets: %c\n", getExpectedClosingBracket(stack[top])); printf("Missing closing brackets: %c\n", getExpectedClosingBracket(stack[top]));
return 0; return 0;
} }
// If no brackets are found, output a success message // If no brackets are found, output a success message
if (!foundBracket) { if (!foundBracket) {
printf("Read: %s\n", input);
printf("All brackets OK\n"); printf("All brackets OK\n");
} else { } else {
// Expected correct output if all brackets match // Expected correct output if all brackets match
printf("Read: %s\n", input);
printf("All brackets OK\n"); printf("All brackets OK\n");
} }