From 9786b86b3fc2994410d5b2fbb892c3d76729c517 Mon Sep 17 00:00:00 2001 From: vj586da Date: Thu, 21 Oct 2021 22:16:34 +0200 Subject: [PATCH] first commit --- cv3/program.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 cv3/program.c diff --git a/cv3/program.c b/cv3/program.c new file mode 100644 index 0000000..c6294d5 --- /dev/null +++ b/cv3/program.c @@ -0,0 +1,99 @@ +#include +#include +#include +#include + +#define STACK_SIZE 20 + +struct stack{ + float values[STACK_SIZE]; + int size; +}; + +void print_stack(struct stack* stack); +void push_stack(struct stack* stack, float value); +float pop_stack(struct stack* stack); +int count_stack(struct stack* stack); + + + +int main(){ + + + char char_value[STACK_SIZE]; + memset(&char_value,0,STACK_SIZE); + float float_value; + char* ptr; + struct stack myStack; + memset(&myStack,0,sizeof(struct stack)); + while(1){ + ptr = fgets(char_value, STACK_SIZE-1, stdin); + if(ptr != NULL ){ + float_value = strtof(char_value, NULL); + if(float_value == 0){ + if(char_value[0] == '+'){ + float a = pop_stack(&myStack); + float b = pop_stack(&myStack); + float c = a+b; + push_stack(&myStack, c); + print_stack(&myStack); + printf("\n"); + } + if(char_value[0] == '-'){ + float a = pop_stack(&myStack); + float b = pop_stack(&myStack); + float c = a-b; + push_stack(&myStack, c); + print_stack(&myStack); + printf("\n"); + } + if(char_value[0] == '*'){ + float a = pop_stack(&myStack); + float b = pop_stack(&myStack); + float c = a*b; + push_stack(&myStack, c); + print_stack(&myStack); + printf("\n"); + } + if(char_value[0] == '/'){ + float a = pop_stack(&myStack); + float b = pop_stack(&myStack); + float c = a/b; + push_stack(&myStack, c); + print_stack(&myStack); + printf("\n"); + } + + }else { + push_stack(&myStack, float_value); + print_stack(&myStack); + printf("\n"); + } + }else{ + break; + } + } + + + return 0; +} + +void print_stack(struct stack* stack){ + printf(">"); + for(int i = 0; i < stack->size; i++){ + printf(" %f ",stack->values[i]); + } +} + +void push_stack(struct stack* stack, float value){ + assert(stack->size < STACK_SIZE); + stack->values[stack->size] = value; + stack->size += 1; +} + +float pop_stack(struct stack* stack){ + assert(stack->size > 0); + float value = stack->values[stack->size-1]; + stack->size -= 1; + return value; +}