Update 'cv3/program.c'

This commit is contained in:
Maryna Kravtsova 2020-10-24 16:59:41 +00:00
parent ffb35f129d
commit fe62adf684

View File

@ -10,6 +10,8 @@ struct stack {
int size; int size;
}; };
//push number to the stack
void push(struct stack* stack,float values){ void push(struct stack* stack,float values){
if(stack->size < 10){ if(stack->size < 10){
stack->values[stack->size] = values; 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){ float pop(struct stack *stack){
if(stack->size > 0){ if(stack->size > 0){
float value = stack->values[stack->size-1]; float value = stack->values[stack->size-1];
@ -58,9 +62,10 @@ int main() {
float r = 0; float r = 0;
float z = 0; float z = 0;
//if string is not empty
if(line[1] != 0 && x != NULL){ if(line[1] != 0 && x != NULL){
//if there is a wrong symbol
if(!isdigit(line[0]) && line[0] != '+' && line[0] != '*' && line[0] != '/' && line[0] != '-'){ if(!isdigit(line[0]) && line[0] != '+' && line[0] != '*' && line[0] != '/' && line[0] != '-'){
printf("bad input\n"); printf("bad input\n");
return 0; return 0;
@ -70,8 +75,8 @@ int main() {
r = pop(&mystack) + pop(&mystack); r = pop(&mystack) + pop(&mystack);
} }
else if(line[0] == '-'){ else if(line[0] == '-'){
z = pop(&mystack); z = pop(&mystack); //swap numbers(e.g input "3 4 -"
r = pop(&mystack) - z; r = pop(&mystack) - z; //output 3 - 4, not 4 - 3)
} }
else if(line[0] == '*'){ else if(line[0] == '*'){
r = pop(&mystack) * pop(&mystack); r = pop(&mystack) * pop(&mystack);
@ -86,13 +91,14 @@ int main() {
r = pop(&mystack) / z; r = pop(&mystack) / z;
} }
} }
//if symbol is not operation, than it is number
//change string to float
else { else {
r = strtof(line,&x); r = strtof(line,&x);
} }
//push result to stack
push(&mystack,r); push(&mystack,r);
print_stack(&mystack); print_stack(&mystack);
} }
else{ else{