Update cv3/program.c
This commit is contained in:
		
							parent
							
								
									45b15ad033
								
							
						
					
					
						commit
						768000285e
					
				
							
								
								
									
										153
									
								
								cv3/program.c
									
									
									
									
									
								
							
							
						
						
									
										153
									
								
								cv3/program.c
									
									
									
									
									
								
							| @ -1,116 +1,81 @@ | |||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
| #include <string.h> |  | ||||||
| #include <assert.h> |  | ||||||
| #include <ctype.h> |  | ||||||
| 
 | 
 | ||||||
| #define STACK_SIZE 10 | #define STACK_SIZE 10 | ||||||
| 
 | 
 | ||||||
|  | double stack[STACK_SIZE]; | ||||||
|  | int top = 0; | ||||||
| 
 | 
 | ||||||
| struct stack { | void print_stack() { | ||||||
|     float values[STACK_SIZE]; |     for (int i = 0; i < top; i++) { | ||||||
|     int size; |         printf("%.2f ", stack[i]); | ||||||
| }; |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| void init_stack(struct stack* s) { |  | ||||||
|     memset(s, 0, sizeof(struct stack)); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| void push_stack(struct stack* s, float value) { |  | ||||||
|     assert(s->size < STACK_SIZE);  |  | ||||||
|     s->values[s->size] = value; |  | ||||||
|     s->size += 1; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| float pop_stack(struct stack* s) { |  | ||||||
|     assert(s->size > 0);  |  | ||||||
|     s->size -= 1; |  | ||||||
|     return s->values[s->size]; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| void print_stack(struct stack* s) { |  | ||||||
|     for (int i = 0; i < s->size; i++) { |  | ||||||
|         printf("%.2f ", s->values[i]); |  | ||||||
|     } |     } | ||||||
|     printf("\n"); |     printf("\n"); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | int push(double value) { | ||||||
| int is_number(const char* str) { |     if (top >= STACK_SIZE) { | ||||||
|     char* endptr; |         printf("full stack\n"); | ||||||
|     strtof(str, &endptr); |         return 0; | ||||||
|     return endptr != str && *endptr == '\0';  |     } | ||||||
|  |     stack[top++] = value; | ||||||
|  |     print_stack(); | ||||||
|  |     return 1; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | int pop(double *value) { | ||||||
|  |     if (top <= 0) { | ||||||
|  |         printf("not enough operands\n"); | ||||||
|  |         return 0; | ||||||
|  |     } | ||||||
|  |     *value = stack[--top]; | ||||||
|  |     return 1; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | int apply_operation(char op) { | ||||||
|  |     double a, b; | ||||||
|  | 
 | ||||||
|  |     if (!pop(&b) || !pop(&a)) { | ||||||
|  |         return 0; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     double result; | ||||||
|  |     switch (op) { | ||||||
|  |         case '+': result = a + b; break; | ||||||
|  |         case '-': result = a - b; break; | ||||||
|  |         case '*': result = a * b; break; | ||||||
|  |         case '/': | ||||||
|  |             if (b == 0) { | ||||||
|  |                 printf("division by zero\n"); | ||||||
|  |                 return 0; | ||||||
|  |             } | ||||||
|  |             result = a / b; | ||||||
|  |             break; | ||||||
|  |         default: | ||||||
|  |             printf("bad input\n"); | ||||||
|  |             return 0; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return push(result); | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| int main() { | int main() { | ||||||
|     struct stack mystack; |     char input[100]; | ||||||
|     init_stack(&mystack); |  | ||||||
| 
 | 
 | ||||||
|     char input[20];  |     while (scanf("%s", input) == 1) { | ||||||
|  |         char *end; | ||||||
|  |         double value = strtod(input, &end); | ||||||
| 
 | 
 | ||||||
|     while (1) { |         if (*end == '\0') { | ||||||
|         printf("Enter a number or an operation (+, -, *, /): "); |             if (!push(value)) return 0; | ||||||
|         if (!fgets(input, sizeof(input), stdin)) { |         } else if (input[1] == '\0' && strchr("+-*/", input[0])) { | ||||||
|             break;  |             if (!apply_operation(input[0])) return 0; | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|          |  | ||||||
|         input[strcspn(input, "\n")] = 0; |  | ||||||
| 
 |  | ||||||
|         if (is_number(input)) { |  | ||||||
|              |  | ||||||
|             float value = strtof(input, NULL); |  | ||||||
|             push_stack(&mystack, value); |  | ||||||
|             print_stack(&mystack); |  | ||||||
|         } else { |         } else { | ||||||
|              |             printf("bad input\n"); | ||||||
|             if (mystack.size < 2) { |             return 0; | ||||||
|                 printf("Error: Not enough values on the stack to perform operation.\n"); |  | ||||||
|                 break;  |  | ||||||
|             } |  | ||||||
|             float b = pop_stack(&mystack); |  | ||||||
|             float a = pop_stack(&mystack); |  | ||||||
|             float result = 0;  |  | ||||||
| 
 |  | ||||||
|             switch (input[0]) { |  | ||||||
|                 case '+': |  | ||||||
|                     result = a + b; |  | ||||||
|                     break; |  | ||||||
|                 case '-': |  | ||||||
|                     result = a - b; |  | ||||||
|                     break; |  | ||||||
|                 case '*': |  | ||||||
|                     result = a * b; |  | ||||||
|                     break; |  | ||||||
|                 case '/': |  | ||||||
|                     if (b == 0) { |  | ||||||
|                         printf("Error: Division by zero.\n"); |  | ||||||
|                          |  | ||||||
|                         push_stack(&mystack, a); |  | ||||||
|                         push_stack(&mystack, b); |  | ||||||
|                         print_stack(&mystack); |  | ||||||
|                         continue;  |  | ||||||
|                     } |  | ||||||
|                     result = a / b; |  | ||||||
|                     break; |  | ||||||
|                 default: |  | ||||||
|                     printf("Error: Unknown operation '%s'.\n", input); |  | ||||||
|                      |  | ||||||
|                     push_stack(&mystack, a); |  | ||||||
|                     push_stack(&mystack, b); |  | ||||||
|                     print_stack(&mystack); |  | ||||||
|                     continue;  |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             push_stack(&mystack, result); |  | ||||||
|             print_stack(&mystack); |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     printf("no input\n"); | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user