Update a1/program.c
This commit is contained in:
		
							parent
							
								
									fa2fb5fcc3
								
							
						
					
					
						commit
						8f4c16dd27
					
				
							
								
								
									
										114
									
								
								a1/program.c
									
									
									
									
									
								
							
							
						
						
									
										114
									
								
								a1/program.c
									
									
									
									
									
								
							| @ -1,91 +1,71 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| 
 | ||||
| #define MAX_LEN 100 | ||||
| #define MAX_LENGTH 100 | ||||
| 
 | ||||
| // Стек для хранения открывающих скобок
 | ||||
| char stack[MAX_LEN]; | ||||
| int top = -1; | ||||
| 
 | ||||
| // Функция для добавления элемента в стек
 | ||||
| void push(char c) { | ||||
|     if (top < MAX_LEN - 1) { | ||||
|         stack[++top] = c; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // Функция для извлечения элемента из стека
 | ||||
| char pop() { | ||||
|     if (top >= 0) { | ||||
|         return stack[top--]; | ||||
|     } | ||||
|     return '\0';  // Если стек пустой
 | ||||
| } | ||||
| 
 | ||||
| // Функция для получения соответствующей закрывающей скобки
 | ||||
| char matchingCloseBracket(char open) { | ||||
|     if (open == '(') return ')'; | ||||
|     if (open == '{') return '}'; | ||||
|     if (open == '[') return ']'; | ||||
|     if (open == '<') return '>'; | ||||
|     return '\0'; | ||||
| } | ||||
| 
 | ||||
| // Функция для проверки соответствия скобок
 | ||||
| int isMatchingPair(char open, char close) { | ||||
|     return (open == '(' && close == ')') || | ||||
|            (open == '{' && close == '}') || | ||||
|            (open == '[' && close == ']') || | ||||
|            (open == '<' && close == '>'); | ||||
| int isMatching(char opening, char closing) { | ||||
|     return (opening == '{' && closing == '}') || | ||||
|            (opening == '[' && closing == ']') || | ||||
|            (opening == '(' && closing == ')') || | ||||
|            (opening == '<' && closing == '>'); | ||||
| } | ||||
| 
 | ||||
| int main() { | ||||
|     char code[MAX_LEN]; | ||||
|     fgets(code, MAX_LEN, stdin); | ||||
|     char input[MAX_LENGTH + 1]; | ||||
|     char stack[MAX_LENGTH]; | ||||
|     int top = -1;  | ||||
|     int position = 0;  | ||||
| 
 | ||||
|     // Убираем лишний символ новой строки, если он есть
 | ||||
|     code[strcspn(code, "\n")] = 0; | ||||
|      | ||||
|     printf("Zadajte kód (maximálne 100 znakov): "); | ||||
|     fgets(input, sizeof(input), stdin); | ||||
|      | ||||
|      | ||||
|     input[strcspn(input, "\n")] = 0; | ||||
| 
 | ||||
|     printf("Read: %s\n", code); | ||||
|      | ||||
|     if (strlen(input) > MAX_LENGTH) { | ||||
|         printf("Chyba: dĺžka reťazca presahuje 100 znakov.\n"); | ||||
|         return 1; | ||||
|     } | ||||
| 
 | ||||
|     // Дополнительный массив для хранения индексов открывающих скобок
 | ||||
|     int indexStack[MAX_LEN]; | ||||
|     int indexTop = -1; | ||||
|     printf("Načítané: %s\n", input); | ||||
| 
 | ||||
|     for (int i = 0; i < strlen(code); i++) { | ||||
|         char c = code[i]; | ||||
|      | ||||
|     for (int i = 0; i < strlen(input); i++) { | ||||
|         char current = input[i]; | ||||
|         position++; | ||||
| 
 | ||||
|         if (c == '(' || c == '{' || c == '[' || c == '<') { | ||||
|             push(c); | ||||
|             indexStack[++indexTop] = i; // Сохраняем индекс открывающей скобки
 | ||||
|         } else if (c == ')' || c == '}' || c == ']' || c == '>') { | ||||
|          | ||||
|         if (current == '{' || current == '[' || current == '(' || current == '<') { | ||||
|             stack[++top] = current; \ | ||||
|         }  | ||||
|         | ||||
|         else if (current == '}' || current == ']' || current == ')' || current == '>') { | ||||
|              | ||||
|             if (top == -1) { | ||||
|                 // Закрывающая скобка без соответствующей открывающей
 | ||||
|                 printf("Unexpected closing bracket %c in %d\n", c, i); | ||||
|                 return 1;  // Возвращаем 1 при ошибке
 | ||||
|                 printf("Neočekávaný znak %c na %d, očakávaná otváracia zátvorka.\n", current, position); | ||||
|                 return 1; | ||||
|             } | ||||
| 
 | ||||
|             char lastOpen = pop(); | ||||
|             int lastIndex = indexStack[indexTop--]; // Извлекаем индекс соответствующей открывающей скобки
 | ||||
|             if (!isMatchingPair(lastOpen, c)) { | ||||
|                 // Здесь нужно использовать lastIndex + 1, чтобы он соответствовал формату вывода
 | ||||
|                 printf("Crossed bracket %c in %d, expected %c\n", c, i + 1, matchingCloseBracket(lastOpen)); | ||||
|                 return 1;  // Возвращаем 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("Missing closing brackets: "); | ||||
|         for (int i = top; i >= 0; i--) { | ||||
|             printf("%c", matchingCloseBracket(stack[i])); | ||||
|         } | ||||
|         printf("\n"); | ||||
|         return 1;  // Возвращаем 1 при ошибке
 | ||||
|         printf("Neočekávaný koniec vstupu, očakávaná zatváracia zátvorka pre %c.\n", stack[top]); | ||||
|         return 1; | ||||
|     } | ||||
| 
 | ||||
|     printf("All brackets OK\n"); | ||||
|     return 0;  // Возвращаем 0 при успехе
 | ||||
|     printf("Všetky zátvorky sú v poriadku\n"); | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user