2
This commit is contained in:
		
							parent
							
								
									dd7ae61dc3
								
							
						
					
					
						commit
						23fd7ac3c3
					
				| @ -13,8 +13,9 @@ bool is_number(char *string) { | |||||||
|     strtof(string, &end); |     strtof(string, &end); | ||||||
|     return end != string && *end == '\0'; |     return end != string && *end == '\0'; | ||||||
| } | } | ||||||
| bool is_operation(char c) { | 
 | ||||||
|     return c == '+' || c == '-' || c == '*' || c == '/'; | bool is_operation(char *string) { | ||||||
|  |     return strlen(string) == 2 && (string[0] == '+' || string[0] == '-' || string[0] == '*' || string[0] == '/'); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| float calculator(float n1, float n2, char operation) {  | float calculator(float n1, float n2, char operation) {  | ||||||
| @ -31,25 +32,27 @@ float calculator(float n1, float n2, char operation) { | |||||||
|         if (n2 != 0) { |         if (n2 != 0) { | ||||||
|             return n1 / n2; |             return n1 / n2; | ||||||
|         } else { |         } else { | ||||||
|             printf("Ошибка: деление на ноль\n"); |             printf("Error: division by zero\n"); | ||||||
|             exit(1); |             exit(1); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
|  | 
 | ||||||
| void push(float number) { | void push(float number) { | ||||||
|     if (stack_top < STACK_SIZE - 1) { |     if (stack_top < STACK_SIZE - 1) { | ||||||
|         stack[++stack_top] = number; |         stack[++stack_top] = number; | ||||||
|     } else { |     } else { | ||||||
|         printf("Ошибка: стек переполнен\n"); |         printf("Error: stack overflow\n"); | ||||||
|         exit(1); |         exit(1); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
| float pop() { | float pop() { | ||||||
|     if (stack_top >= 0) { |     if (stack_top >= 0) { | ||||||
|         return stack[stack_top--]; |         return stack[stack_top--]; | ||||||
|     } else { |     } else { | ||||||
|         printf("Ошибка: стек пуст\n"); |         printf("Error: stack is empty\n"); | ||||||
|         exit(1); |         exit(1); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -63,32 +66,35 @@ int main() { | |||||||
|         if (arr[0] == '\n' || arr[0] == EOF) { |         if (arr[0] == '\n' || arr[0] == EOF) { | ||||||
|             break; |             break; | ||||||
|         } |         } | ||||||
|  | 
 | ||||||
|         if (is_number(arr)) { |         if (is_number(arr)) { | ||||||
|             float number = strtof(arr, &pend); |             float number = strtof(arr, &pend); | ||||||
|             push(number); |             push(number); | ||||||
|  |             printf("Number added: %.2f\n", number); | ||||||
|         } |         } | ||||||
|         else if (is_operation(arr[0])) { |         else if (is_operation(arr)) { | ||||||
|             if (stack_top < 1) { |             if (stack_top < 1) { | ||||||
|                 printf("Ошибка: недостаточно чисел в стеке для операции\n"); |                 printf("Error: not enough numbers in the stack for operation\n"); | ||||||
|                 return 1; |                 return 1; | ||||||
|             } |             } | ||||||
|  | 
 | ||||||
|             float n2 = pop(); |             float n2 = pop(); | ||||||
|             float n1 = pop(); |             float n1 = pop(); | ||||||
|             char operation = arr[0]; |             char operation = arr[0]; | ||||||
|             float result = calculator(n1, n2, operation); |             float result = calculator(n1, n2, operation); | ||||||
|             push(result); |             push(result); | ||||||
|  |             printf("Operation result: %.2f\n", result); | ||||||
|         } |         } | ||||||
|         else { |         else { | ||||||
|             printf("Ошибка: некорректный ввод\n"); |             printf("Error: invalid input\n"); | ||||||
|             return 1; |             return 1; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // Вывод результата
 |  | ||||||
|     if (stack_top == 0) { |     if (stack_top == 0) { | ||||||
|         printf("Результат: %.2f\n", pop()); |         printf("Final result: %.2f\n", pop()); | ||||||
|     } else { |     } else { | ||||||
|         printf("Ошибка: стек содержит более одного числа после операций\n"); |         printf("Error: stack contains more than one number after operations\n"); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     return 0; |     return 0; | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user