2020-10-11 12:27:21 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define STACK_SIZE 100
|
|
|
|
|
|
|
|
struct stack {
|
|
|
|
float values[STACK_SIZE];
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
|
|
|
void push(struct stack* stack,float values){
|
|
|
|
assert(stack->size < STACK_SIZE);
|
|
|
|
stack->values[stack->size] = values;
|
|
|
|
stack->size += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
float pop(struct stack *stack){
|
|
|
|
assert(stack->size > 0);
|
|
|
|
float values = stack->values[stack->size-1];
|
|
|
|
stack->size -= 1;
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
void print_stack(struct stack *stack){
|
|
|
|
int i;
|
|
|
|
int len = stack->size - 1;
|
|
|
|
for(i = 0; i < len; i++){
|
2020-10-11 12:43:09 +00:00
|
|
|
printf("%0.2f ", stack->values[i]);
|
2020-10-11 12:27:21 +00:00
|
|
|
}
|
|
|
|
if(stack->size != 0){
|
2020-10-11 12:43:09 +00:00
|
|
|
printf("%0.2f ", stack->values[i]);
|
2020-10-11 12:27:21 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
struct stack mystack;
|
|
|
|
memset(&mystack, 0, sizeof(struct stack));
|
|
|
|
int i = 0;
|
2020-10-11 12:43:09 +00:00
|
|
|
float z;
|
2020-10-11 12:27:21 +00:00
|
|
|
for(i = 0; i < 10000; i++){
|
|
|
|
char line[10];
|
|
|
|
char *x = fgets(line, 10, stdin);
|
|
|
|
float r = 0;
|
|
|
|
|
|
|
|
if(!isdigit(line[0]) && line[0] != '+' && line[0] != '*' && line[0] != '/' && line[0] != '-'){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(line[0] == '+'){
|
|
|
|
r = pop(&mystack) + pop(&mystack);
|
|
|
|
}
|
|
|
|
else if(line[0] == '-'){
|
2020-10-11 12:43:09 +00:00
|
|
|
z = pop(&mystack);
|
|
|
|
r = pop(&mystack) - z;
|
2020-10-11 12:27:21 +00:00
|
|
|
}
|
|
|
|
else if(line[0] == '*'){
|
|
|
|
r = pop(&mystack) * pop(&mystack);
|
|
|
|
}
|
|
|
|
else if(line[0] == '/'){
|
2020-10-11 12:43:09 +00:00
|
|
|
z = pop(&mystack);
|
|
|
|
if(z != 0.0){
|
|
|
|
r = pop(&mystack) / z;
|
|
|
|
}
|
2020-10-11 12:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
r = strtof(line,&x);
|
|
|
|
}
|
|
|
|
|
|
|
|
push(&mystack,r);
|
|
|
|
print_stack(&mystack);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|