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 <stdio.h> | ||||||
|  | #include <stdlib.h> | ||||||
| #include <string.h> | #include <string.h> | ||||||
| 
 | 
 | ||||||
| #define MAX_LEN 100 | #define MAX_LENGTH 100 | ||||||
| 
 | 
 | ||||||
| // Стек для хранения открывающих скобок
 |  | ||||||
| char stack[MAX_LEN]; |  | ||||||
| int top = -1; |  | ||||||
| 
 | 
 | ||||||
| // Функция для добавления элемента в стек
 | int isMatching(char opening, char closing) { | ||||||
| void push(char c) { |     return (opening == '{' && closing == '}') || | ||||||
|     if (top < MAX_LEN - 1) { |            (opening == '[' && closing == ']') || | ||||||
|         stack[++top] = c; |            (opening == '(' && closing == ')') || | ||||||
|     } |            (opening == '<' && closing == '>'); | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // Функция для извлечения элемента из стека
 |  | ||||||
| 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 main() { | int main() { | ||||||
|     char code[MAX_LEN]; |     char input[MAX_LENGTH + 1]; | ||||||
|     fgets(code, MAX_LEN, stdin); |     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; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     // Дополнительный массив для хранения индексов открывающих скобок
 |     printf("Načítané: %s\n", input); | ||||||
|     int indexStack[MAX_LEN]; |  | ||||||
|     int indexTop = -1; |  | ||||||
| 
 | 
 | ||||||
|     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); |         if (current == '{' || current == '[' || current == '(' || current == '<') { | ||||||
|             indexStack[++indexTop] = i; // Сохраняем индекс открывающей скобки
 |             stack[++top] = current; \ | ||||||
|         } else if (c == ')' || c == '}' || c == ']' || c == '>') { |         }  | ||||||
|  |         | ||||||
|  |         else if (current == '}' || current == ']' || current == ')' || current == '>') { | ||||||
|  |              | ||||||
|             if (top == -1) { |             if (top == -1) { | ||||||
|                 // Закрывающая скобка без соответствующей открывающей
 |                 printf("Neočekávaný znak %c na %d, očakávaná otváracia zátvorka.\n", current, position); | ||||||
|                 printf("Unexpected closing bracket %c in %d\n", c, i); |                 return 1; | ||||||
|                 return 1;  // Возвращаем 1 при ошибке
 |  | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             char lastOpen = pop(); |              | ||||||
|             int lastIndex = indexStack[indexTop--]; // Извлекаем индекс соответствующей открывающей скобки
 |             if (!isMatching(stack[top--], current)) { | ||||||
|             if (!isMatchingPair(lastOpen, c)) { |                 printf("Prekrývajúca sa zátvorka %c na %d, očakávaná %c.\n", current, position,  | ||||||
|                 // Здесь нужно использовать lastIndex + 1, чтобы он соответствовал формату вывода
 |                        (current == '}') ? '{' : (current == ']') ? '[' :  | ||||||
|                 printf("Crossed bracket %c in %d, expected %c\n", c, i + 1, matchingCloseBracket(lastOpen)); |                        (current == ')') ? '(' : '<'); | ||||||
|                 return 1;  // Возвращаем 1 при ошибке
 |                 return 1; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // Проверка на незакрытые скобки
 |      | ||||||
|     if (top != -1) { |     if (top != -1) { | ||||||
|         printf("Missing closing brackets: "); |         printf("Neočekávaný koniec vstupu, očakávaná zatváracia zátvorka pre %c.\n", stack[top]); | ||||||
|         for (int i = top; i >= 0; i--) { |         return 1; | ||||||
|             printf("%c", matchingCloseBracket(stack[i])); |  | ||||||
|         } |  | ||||||
|         printf("\n"); |  | ||||||
|         return 1;  // Возвращаем 1 при ошибке
 |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     printf("All brackets OK\n"); |     printf("Všetky zátvorky sú v poriadku\n"); | ||||||
|     return 0;  // Возвращаем 0 при успехе
 |     return 0; | ||||||
| } | } | ||||||
| 
 |  | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user