#include <stdio.h>
#include <stdlib.h>


struct Pilha {
    int topo;
    int capa;
    float *pElem;
};

void criarpilha(struct Pilha *p, int c) {
    p->topo = -1;
    p->capa = c;
    p->pElem = (float *) malloc(c * sizeof(float));
}


void push(struct Pilha *p, float v) {
    p->topo++;
    p->pElem[p->topo] = v;
}

float sub(struct Pilha *p) {
    int x, y;
    float calc;
    p->topo--;
    x = p->pElem[p->topo];
    p->topo++;
    y = p->pElem[p->topo];
    calc = x - y;
    p->topo--;
    p->pElem[p->topo] = calc;

    return calc;
}

float mpy(struct Pilha *p) {
    int x, y;
    float calc;
    p->topo--;
    x = p->pElem[p->topo];
    p->topo++;
    y = p->pElem[p->topo];

    calc = x * y;

    p->topo--;
    p->pElem[p->topo] = calc;

    return calc;
}

float add(struct Pilha *p) {
    int x, y;
    float calc;
    p->topo--;
    x = p->pElem[p->topo];
    p->topo++;
    y = p->pElem[p->topo];
    calc = x + y;
    p->topo--;
    p->pElem[p->topo] = calc;

    return calc;
}


float Div(struct Pilha *p) {
    int x, y;
    float calc;
    p->topo--;
    x = p->pElem[p->topo];
    p->topo++;
    y = p->pElem[p->topo];
    calc = x / y;
    p->topo--;
    p->pElem[p->topo] = calc;

    return calc;
}

float dec(struct Pilha *p) {
    int x;
    p->pElem[p->topo]--;
    return p->pElem[p->topo];
}


float pop(struct Pilha *p) {
    float aux = p->pElem[p->topo];
    p->topo--;
    return aux;
}

float monstrarpilha(struct Pilha *p) {
    return p->pElem[p->topo];

}

int main() {

    struct Pilha p;
    int capacidade = 4;
    int A = 9, B = 3, C = 2, D = 1, E = 1;

    criarpilha(&p, capacidade);

    push(&p, A);
    printf("\nPUSH A: %.1f\n", monstrarpilha(&p));

    push(&p, B);
    printf("\nPUSH B: %.1f\n", monstrarpilha(&p));

    printf("\nSubtracao: %.1f\n", sub(&p));

    push(&p, C);
    printf("\nPUSH C: %.1f\n", monstrarpilha(&p));

    push(&p, D);
    printf("\nPUSH D: %.1f\n", monstrarpilha(&p));

    push(&p, E);
    printf("\nPUSH E: %.1f\n", monstrarpilha(&p));

    printf("\nmultiplicacao: %.1f\n", mpy(&p));
    printf("\nadicao: %.1f\n", add(&p));
    printf("\ndecrementar: %.1f\n", dec(&p));
    printf("\ndivisao: %.1f\n", Div(&p));

    printf("\nPOP F%.1f\n", pop(&p));

}