Add a1/program.c
This commit is contained in:
		
							parent
							
								
									d02dedcc64
								
							
						
					
					
						commit
						95be908bee
					
				
							
								
								
									
										61
									
								
								a1/program.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								a1/program.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,61 @@ | |||||||
|  | #include <stdio.h> | ||||||
|  | #include <string.h> | ||||||
|  | 
 | ||||||
|  | int isOpening(char c) { | ||||||
|  |     return c == '{' || c == '[' || c == '(' || c == '<'; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | int isClosing(char c) { | ||||||
|  |     return c == '}' || c == ']' || c == ')' || c == '>'; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | int isMatching(char open, char close) { | ||||||
|  |     return (open == '{' && close == '}') || | ||||||
|  |            (open == '[' && close == ']') || | ||||||
|  |            (open == '(' && close == ')') || | ||||||
|  |            (open == '<' && close == '>'); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | int main() { | ||||||
|  |     char input[101]; | ||||||
|  |     char stack[100]; | ||||||
|  |     int top = -1; | ||||||
|  | 
 | ||||||
|  |     printf("Введіть код: "); | ||||||
|  |     fgets(input, 101, stdin); | ||||||
|  | 
 | ||||||
|  |     for (int i = 0; i < strlen(input); i++) { | ||||||
|  |         char current = input[i]; | ||||||
|  | 
 | ||||||
|  |         if (isOpening(current)) { | ||||||
|  |             if (top < 99) { | ||||||
|  |                 stack[++top] = current; | ||||||
|  |             } else { | ||||||
|  |                 printf("Помилка: стек переповнений.\n"); | ||||||
|  |                 return 1; | ||||||
|  |             } | ||||||
|  |         } else if (isClosing(current)) { | ||||||
|  |             if (top == -1) { | ||||||
|  |                 printf("Помилка: зайвий закриваючий символ '%c' на позиції %d.\n", current, i); | ||||||
|  |                 return 1; | ||||||
|  |             } | ||||||
|  |             if (!isMatching(stack[top], current)) { | ||||||
|  |                 printf("Помилка: очікувався '%c', але знайдено '%c' на позиції %d.\n",  | ||||||
|  |                        (stack[top] == '{') ? '}' :  | ||||||
|  |                        (stack[top] == '[') ? ']' :  | ||||||
|  |                        (stack[top] == '(') ? ')' : '>',  | ||||||
|  |                        current, i); | ||||||
|  |                 return 1; | ||||||
|  |             } | ||||||
|  |             top--; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     if (top != -1) { | ||||||
|  |         printf("Помилка: незакриті дужки.\n"); | ||||||
|  |         return 1; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     printf("Код в порядку: всі дужки правильно вкладені.\n"); | ||||||
|  |     return 0; | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user