Update a1/program.c
This commit is contained in:
		
							parent
							
								
									6e8576acf5
								
							
						
					
					
						commit
						e88adeb6e1
					
				
							
								
								
									
										40
									
								
								a1/program.c
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								a1/program.c
									
									
									
									
									
								
							@ -4,7 +4,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#define MAX_LENGTH 100
 | 
					#define MAX_LENGTH 100
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Функция для проверки, являются ли два символа парой скобок
 | 
					// Function to check if two brackets are matching
 | 
				
			||||||
int isMatching(char opening, char closing) {
 | 
					int isMatching(char opening, char closing) {
 | 
				
			||||||
    return (opening == '{' && closing == '}') ||
 | 
					    return (opening == '{' && closing == '}') ||
 | 
				
			||||||
           (opening == '[' && closing == ']') ||
 | 
					           (opening == '[' && closing == ']') ||
 | 
				
			||||||
@ -15,65 +15,65 @@ int isMatching(char opening, char closing) {
 | 
				
			|||||||
int main() {
 | 
					int main() {
 | 
				
			||||||
    char input[MAX_LENGTH + 1];
 | 
					    char input[MAX_LENGTH + 1];
 | 
				
			||||||
    char stack[MAX_LENGTH];
 | 
					    char stack[MAX_LENGTH];
 | 
				
			||||||
    int top = -1; // Индекс для вершины стека
 | 
					    int top = -1; // Stack pointer
 | 
				
			||||||
    int position = 0; // Позиция текущего символа
 | 
					    int position = -1; // Start position at -1 to adjust to 0-based indexing
 | 
				
			||||||
    int foundBracket = 0; // Флаг для проверки, найдена ли какая-либо скобка
 | 
					    int foundBracket = 0; // Flag to check if any bracket is found
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Чтение строки
 | 
					    // Reading the string
 | 
				
			||||||
    fgets(input, sizeof(input), stdin);
 | 
					    fgets(input, sizeof(input), stdin);
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    // Удаление символа новой строки, если он присутствует
 | 
					    // Remove newline character if present
 | 
				
			||||||
    input[strcspn(input, "\n")] = 0;
 | 
					    input[strcspn(input, "\n")] = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Проверка длины строки
 | 
					    // 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("Chyba: dĺžka reťazca presahuje 100 znakov.\n");
 | 
				
			||||||
        return 1;
 | 
					        return 1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Проход по каждому символу строки
 | 
					    // 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++;
 | 
					        position++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Проверка, является ли текущий символ открывающей скобкой
 | 
					        // Check if current character is an opening bracket
 | 
				
			||||||
        if (current == '{' || current == '[' || current == '(' || current == '<') {
 | 
					        if (current == '{' || current == '[' || current == '(' || current == '<') {
 | 
				
			||||||
            stack[++top] = current; // Увеличиваем top и помещаем скобку в стек
 | 
					            stack[++top] = current; // Push to stack
 | 
				
			||||||
            foundBracket = 1; // Мы нашли скобку
 | 
					            foundBracket = 1; // Found at least one bracket
 | 
				
			||||||
        } 
 | 
					        } 
 | 
				
			||||||
        // Если текущий символ является закрывающей скобкой
 | 
					        // If current character is a closing bracket
 | 
				
			||||||
        else if (current == '}' || current == ']' || current == ')' || current == '>') {
 | 
					        else if (current == '}' || current == ']' || current == ')' || current == '>') {
 | 
				
			||||||
            // Если стек пустой, это ошибка
 | 
					            // If stack is empty, it's an error
 | 
				
			||||||
            if (top == -1) {
 | 
					            if (top == -1) {
 | 
				
			||||||
                printf("Read: %s\n", input);
 | 
					                printf("Read: %s\n", input);
 | 
				
			||||||
                printf("Unexpected closing bracket %c in %d\n", current, position);
 | 
					                printf("Unexpected closing bracket %c in %d\n", current, position);
 | 
				
			||||||
                return 1;
 | 
					                return 0;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // Если скобки не совпадают, это ошибка
 | 
					            // 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("Read: %s\n", input);
 | 
				
			||||||
                printf("Crossed bracket %c in %d, expected %c.\n", current, position, 
 | 
					                printf("Crossed bracket %c in %d, expected %c.\n", current, position, 
 | 
				
			||||||
                       (current == '}') ? '{' : (current == ']') ? '[' : 
 | 
					                       (current == '}') ? '{' : (current == ']') ? '[' : 
 | 
				
			||||||
                       (current == ')') ? '(' : '<');
 | 
					                       (current == ')') ? '(' : '<');
 | 
				
			||||||
                return 1;
 | 
					                return 0;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Если остались открытые скобки в стеке, это ошибка
 | 
					    // If there are unmatched opening brackets left in the stack
 | 
				
			||||||
    if (top != -1) {
 | 
					    if (top != -1) {
 | 
				
			||||||
        printf("Read: %s\n", input);
 | 
					        printf("Read: %s\n", input);
 | 
				
			||||||
        printf("Unexpected end of input, expected closing bracket for %c.\n", stack[top]);
 | 
					        printf("Unexpected end of input, expected closing bracket for %c.\n", stack[top]);
 | 
				
			||||||
        return 1;
 | 
					        return 0;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Если не найдены скобки, выводим сообщение
 | 
					    // If no brackets are found, output a success message
 | 
				
			||||||
    if (!foundBracket) {
 | 
					    if (!foundBracket) {
 | 
				
			||||||
        printf("Read: %s\n", input);
 | 
					        printf("Read: %s\n", input);
 | 
				
			||||||
        printf("All brackets OK\n");
 | 
					        printf("All brackets OK\n");
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        // Ожидаемый корректный вывод
 | 
					        // Expected correct output if all brackets match
 | 
				
			||||||
        printf("Read: %s\n", input);
 | 
					        printf("Read: %s\n", input);
 | 
				
			||||||
        printf("All brackets OK\n");
 | 
					        printf("All brackets OK\n");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user