Update cv3/program.c
This commit is contained in:
		
							parent
							
								
									2aca08a766
								
							
						
					
					
						commit
						86cce8d6f9
					
				| @ -11,7 +11,7 @@ struct stack { | |||||||
|     int size; |     int size; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| // Function declarations
 | 
 | ||||||
| void push_stack(struct stack* stack, float value); | void push_stack(struct stack* stack, float value); | ||||||
| float pop_stack(struct stack* stack); | float pop_stack(struct stack* stack); | ||||||
| void print_stack(struct stack* stack); | void print_stack(struct stack* stack); | ||||||
| @ -19,29 +19,29 @@ int is_number(const char* str); | |||||||
| 
 | 
 | ||||||
| int main() { | int main() { | ||||||
|     struct stack mystack; |     struct stack mystack; | ||||||
|     memset(&mystack, 0, sizeof(struct stack)); // Initialize the stack
 |     memset(&mystack, 0, sizeof(struct stack));  | ||||||
| 
 | 
 | ||||||
|     char input[50]; |     char input[50]; | ||||||
|     while (1) { |     while (1) { | ||||||
|         printf("Enter a number or an operator (+, -, *, /): "); |         printf("Enter a number or an operator (+, -, *, /): "); | ||||||
|         if (!fgets(input, sizeof(input), stdin)) { |         if (!fgets(input, sizeof(input), stdin)) { | ||||||
|             break; // Exit on EOF
 |             break;  | ||||||
|         } |         } | ||||||
|         // Remove trailing newline character
 |         | ||||||
|         input[strcspn(input, "\n")] = 0; |         input[strcspn(input, "\n")] = 0; | ||||||
| 
 | 
 | ||||||
|         // Check if the input is empty
 |         | ||||||
|         if (strlen(input) == 0) { |         if (strlen(input) == 0) { | ||||||
|             printf("no input\n"); |             printf("no input\n"); | ||||||
|             break; |             break; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (is_number(input)) { |         if (is_number(input)) { | ||||||
|             // Convert input to float and push onto stack
 |             | ||||||
|             float value = atof(input); |             float value = atof(input); | ||||||
|             push_stack(&mystack, value); |             push_stack(&mystack, value); | ||||||
|         } else if (strlen(input) == 1 && strchr("+-*/", input[0])) { |         } else if (strlen(input) == 1 && strchr("+-*/", input[0])) { | ||||||
|             // Perform operation
 |              | ||||||
|             if (mystack.size < 2) { |             if (mystack.size < 2) { | ||||||
|                 fprintf(stderr, "Error: Not enough values in stack.\n"); |                 fprintf(stderr, "Error: Not enough values in stack.\n"); | ||||||
|                 exit(EXIT_FAILURE); |                 exit(EXIT_FAILURE); | ||||||
| @ -77,28 +77,28 @@ int main() { | |||||||
|             exit(EXIT_FAILURE); |             exit(EXIT_FAILURE); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         // Print the current stack
 |          | ||||||
|         print_stack(&mystack); |         print_stack(&mystack); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Push a value onto the stack
 | 
 | ||||||
| void push_stack(struct stack* stack, float value) { | void push_stack(struct stack* stack, float value) { | ||||||
|     assert(stack->size < STACK_SIZE); // Ensure there's space in the stack
 |     assert(stack->size < STACK_SIZE);  | ||||||
|     stack->values[stack->size] = value; |     stack->values[stack->size] = value; | ||||||
|     stack->size += 1; |     stack->size += 1; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Pop a value from the stack
 | 
 | ||||||
| float pop_stack(struct stack* stack) { | float pop_stack(struct stack* stack) { | ||||||
|     assert(stack->size > 0); // Ensure there's something to pop
 |     assert(stack->size > 0);  | ||||||
|     stack->size -= 1; |     stack->size -= 1; | ||||||
|     return stack->values[stack->size]; |     return stack->values[stack->size]; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Print the contents of the stack
 | 
 | ||||||
| void print_stack(struct stack* stack) { | void print_stack(struct stack* stack) { | ||||||
|     for (int i = 0; i < stack->size; i++) { |     for (int i = 0; i < stack->size; i++) { | ||||||
|         printf("%.2f ", stack->values[i]); |         printf("%.2f ", stack->values[i]); | ||||||
| @ -106,7 +106,7 @@ void print_stack(struct stack* stack) { | |||||||
|     printf("\n"); |     printf("\n"); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Check if the input string is a valid number
 | r | ||||||
| int is_number(const char* str) { | int is_number(const char* str) { | ||||||
|     char* endptr; |     char* endptr; | ||||||
|     strtod(str, &endptr); |     strtod(str, &endptr); | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user