1
This commit is contained in:
		
							parent
							
								
									21af9602eb
								
							
						
					
					
						commit
						dd7ae61dc3
					
				
							
								
								
									
										96
									
								
								cv3/program.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										96
									
								
								cv3/program.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,96 @@ | ||||
| #include <stdio.h> | ||||
| #include <string.h> | ||||
| #include <stdlib.h> | ||||
| #include <stdbool.h> | ||||
| 
 | ||||
| #define STACK_SIZE 10 | ||||
| 
 | ||||
| float stack[STACK_SIZE]; | ||||
| int stack_top = -1; | ||||
| 
 | ||||
| bool is_number(char *string) { | ||||
|     char *end; | ||||
|     strtof(string, &end); | ||||
|     return end != string && *end == '\0'; | ||||
| } | ||||
| bool is_operation(char c) { | ||||
|     return c == '+' || c == '-' || c == '*' || c == '/'; | ||||
| } | ||||
| 
 | ||||
| float calculator(float n1, float n2, char operation) {  | ||||
|     if (operation == '+') { | ||||
|         return n1 + n2; | ||||
|     } | ||||
|     else if (operation == '-') { | ||||
|         return n1 - n2; | ||||
|     } | ||||
|     else if (operation == '*') { | ||||
|         return n1 * n2; | ||||
|     } | ||||
|     else if (operation == '/') { | ||||
|         if (n2 != 0) { | ||||
|             return n1 / n2; | ||||
|         } else { | ||||
|             printf("Ошибка: деление на ноль\n"); | ||||
|             exit(1); | ||||
|         } | ||||
|     } | ||||
|     return 0; | ||||
| } | ||||
| void push(float number) { | ||||
|     if (stack_top < STACK_SIZE - 1) { | ||||
|         stack[++stack_top] = number; | ||||
|     } else { | ||||
|         printf("Ошибка: стек переполнен\n"); | ||||
|         exit(1); | ||||
|     } | ||||
| } | ||||
| float pop() { | ||||
|     if (stack_top >= 0) { | ||||
|         return stack[stack_top--]; | ||||
|     } else { | ||||
|         printf("Ошибка: стек пуст\n"); | ||||
|         exit(1); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| int main() { | ||||
|     char arr[50]; | ||||
|     char *pend; | ||||
|     stack_top = -1; | ||||
| 
 | ||||
|     while (fgets(arr, 50, stdin)) { | ||||
|         if (arr[0] == '\n' || arr[0] == EOF) { | ||||
|             break; | ||||
|         } | ||||
|         if (is_number(arr)) { | ||||
|             float number = strtof(arr, &pend); | ||||
|             push(number); | ||||
|         } | ||||
|         else if (is_operation(arr[0])) { | ||||
|             if (stack_top < 1) { | ||||
|                 printf("Ошибка: недостаточно чисел в стеке для операции\n"); | ||||
|                 return 1; | ||||
|             } | ||||
|             float n2 = pop(); | ||||
|             float n1 = pop(); | ||||
|             char operation = arr[0]; | ||||
|             float result = calculator(n1, n2, operation); | ||||
|             push(result); | ||||
|         } | ||||
|         else { | ||||
|             printf("Ошибка: некорректный ввод\n"); | ||||
|             return 1; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     // Вывод результата
 | ||||
|     if (stack_top == 0) { | ||||
|         printf("Результат: %.2f\n", pop()); | ||||
|     } else { | ||||
|         printf("Ошибка: стек содержит более одного числа после операций\n"); | ||||
|     } | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user