Update cv3/program.c
This commit is contained in:
		
							parent
							
								
									c98563a17d
								
							
						
					
					
						commit
						fdd5e20544
					
				| @ -1,6 +1,5 @@ | ||||
| #include <stdio.h> | ||||
| #include <string.h> | ||||
| #include <math.h> | ||||
| #include <stdlib.h> | ||||
| 
 | ||||
| #define STACK_SIZE 10 | ||||
| @ -20,7 +19,7 @@ void push_stack(struct stack *s, float value) { | ||||
|         s->size++; | ||||
|     } else { | ||||
|         printf("no input.\n"); | ||||
|         exit(1); // Вихід з програми при переповненні
 | ||||
|         exit(1); // Exit the program on overflow
 | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @ -30,7 +29,7 @@ float pop_stack(struct stack *s) { | ||||
|         return s->values[s->size]; | ||||
|     } else { | ||||
|         printf("no input\n"); | ||||
|         exit(1); // Вихід з програми при порожньому стеку
 | ||||
|         exit(1); // Exit the program on underflow
 | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @ -44,18 +43,18 @@ void print_stack(struct stack *s) { | ||||
| int read(struct stack *s) { | ||||
|     char temp[100]; | ||||
| 
 | ||||
|     // Читаємо рядок з вводу
 | ||||
|     // Read a line of input
 | ||||
|     if (fgets(temp, sizeof(temp), stdin) == NULL) { | ||||
|         return 0; // Помилка при читанні
 | ||||
|         return 0; // Error reading input
 | ||||
|     } | ||||
| 
 | ||||
|     // Перевірка на пустий рядок
 | ||||
|     // Check for empty input
 | ||||
|     if (strcmp(temp, "\n") == 0) { | ||||
|         printf("no input\n"); | ||||
|         exit(1); // Повертаємо 1, щоб продовжити цикл
 | ||||
|         exit(1); // Exit on empty input
 | ||||
|     } | ||||
| 
 | ||||
|     // Завершення програми
 | ||||
|     // Check for termination input
 | ||||
|     if (strcmp(temp, "end\n") == 0) { | ||||
|         return 0;  | ||||
|     } | ||||
| @ -63,24 +62,24 @@ int read(struct stack *s) { | ||||
|     float value; | ||||
|     int scan = sscanf(temp, "%f", &value); | ||||
| 
 | ||||
|     // Якщо це число
 | ||||
|     // If it's a number
 | ||||
|     if (scan == 1) { | ||||
|         push_stack(s, value); | ||||
|         print_stack(s); // Виводимо стек після додавання
 | ||||
|         print_stack(s); // Print the stack after adding
 | ||||
|     } else { | ||||
|         // Якщо це оператор
 | ||||
|         // If it's an operator
 | ||||
|         switch (temp[0]) { | ||||
|             case '+': | ||||
|             case '-': | ||||
|             case '*': | ||||
|             case '/': { | ||||
|                 // Перевірка на недостатню кількість значень у стеку
 | ||||
|                 // Check for sufficient values in the stack
 | ||||
|                 if (s->size < 2) { | ||||
|                     printf("no input\n"); | ||||
|                     exit(1); // Повертаємо 1, щоб продовжити цикл, без виходу з програми
 | ||||
|                     exit(1); // Exit if not enough values
 | ||||
|                 } | ||||
| 
 | ||||
|                 float b = pop_stack(s); // Витягуємо значення
 | ||||
|                 float b = pop_stack(s); // Pop the last two values
 | ||||
|                 float a = pop_stack(s); | ||||
| 
 | ||||
|                 float result; | ||||
| @ -91,22 +90,23 @@ int read(struct stack *s) { | ||||
|                     case '/': | ||||
|                         if (b == 0) { | ||||
|                             printf("no input\n"); | ||||
|                             exit(1); // Вихід з програми при діленні на нуль
 | ||||
|                             exit(1); // Exit on division by zero
 | ||||
|                         } | ||||
|                         result = a / b; | ||||
|                         break; | ||||
|                 } | ||||
|                 push_stack(s, result); // Додаємо результат назад у стек
 | ||||
|                 printf("%.2f\n", result); // Виводимо результат
 | ||||
|                 push_stack(s, result); // Push the result back onto the stack
 | ||||
|                  // Print the result
 | ||||
|                 print_stack(s); // Print the stack after operation
 | ||||
|                 break; | ||||
|             } | ||||
|             default: | ||||
|                 printf("no input\n"); | ||||
|                 exit(1); // Вихід з програми при невірному введенні
 | ||||
|                 exit(1); // Exit on invalid input
 | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     return 1; // Продовжуємо цикл
 | ||||
|     return 1; // Continue the loop
 | ||||
| } | ||||
| 
 | ||||
| int main() { | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user