From fe62adf6845fe3eacfad2c4e63a675c45248ead2 Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Sat, 24 Oct 2020 16:59:41 +0000 Subject: [PATCH] Update 'cv3/program.c' --- cv3/program.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 12e1807..73ca78b 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -10,6 +10,8 @@ struct stack { int size; }; +//push number to the stack + void push(struct stack* stack,float values){ if(stack->size < 10){ stack->values[stack->size] = values; @@ -22,6 +24,8 @@ void push(struct stack* stack,float values){ } } +//pop number from the stack + float pop(struct stack *stack){ if(stack->size > 0){ float value = stack->values[stack->size-1]; @@ -58,9 +62,10 @@ int main() { float r = 0; float z = 0; + //if string is not empty if(line[1] != 0 && x != NULL){ - + //if there is a wrong symbol if(!isdigit(line[0]) && line[0] != '+' && line[0] != '*' && line[0] != '/' && line[0] != '-'){ printf("bad input\n"); return 0; @@ -70,8 +75,8 @@ int main() { r = pop(&mystack) + pop(&mystack); } else if(line[0] == '-'){ - z = pop(&mystack); - r = pop(&mystack) - z; + z = pop(&mystack); //swap numbers(e.g input "3 4 -" + r = pop(&mystack) - z; //output 3 - 4, not 4 - 3) } else if(line[0] == '*'){ r = pop(&mystack) * pop(&mystack); @@ -86,13 +91,14 @@ int main() { r = pop(&mystack) / z; } } - - + //if symbol is not operation, than it is number + //change string to float else { r = strtof(line,&x); } - + //push result to stack push(&mystack,r); + print_stack(&mystack); } else{