/*
 * Soubor:  20110401-Fri-kalkulacka.c
 * Datum:   01.04.2011 08:28
 * Autor:   Marek Nožka, nozka <@t> spseol <d.t> cz
 * Licence: GNU/GPL 
 * Úloha: 
 * Popis:   
 ****************************************************/
#define _ISOC99_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>

/* ***************    Makra    ******************** */
#define STACKLEN 128

/* ***************   Funkce    ******************** */

void push(double number);
double pop(void);
void stackPrint(void);

/****************************************************
 *               Hlavní program.
 ****************************************************/
int main(void)
{
    char enter[32];
    double number, x, y;

    while (scanf("%s", enter) != EOF) {
        if (sscanf(enter, "%lg", &number) == 1) {
            push(number);
/*        } else if ( enter[0]== '+' ) {*/
        } else if (strcmp(enter, "+") == 0) {
            y = pop();
            x = pop();
            if (!isnan(x)) {
                push(x + y);
                stackPrint();
            } else {
                push(y);
                fprintf(stderr, "pro + jsou potřeba dva operandy\n");
            }
        } else if (strcmp(enter, "-") == 0) {
            y = pop();
            x = pop();
            printf(" >> %g\n", x - y);
            push(x - y);
        } else if (strcmp(enter, "*") == 0) {
            y = pop();
            x = pop();
            printf(" >> %g\n", x * y);
            push(x * y);
        } else if (strcmp(enter, "/") == 0) {
            y = pop();
            x = pop();
            printf(" >> %g\n", x / y);
            push(x / y);
        } else if (strcmp(enter, "ln") == 0) {
            x = pop();
            if (!isnan(x)) {
                printf(" >> %g\n", log(x));
                push(log(x));
            }
        } else if (strcmp(enter, "log") == 0) {
            x = pop();
            if (!isnan(x)) {
                printf(" >> %g\n", log10(x));
                push(log10(x));
            }
        } else if (strcmp(enter, "print") == 0) {
            stackPrint();
        } else {
            printf(" >> Neplatný vstup");
        }
    }


    return 0;
}



double stack[STACKLEN];
int stackcount = 0;

void push(double number)
{
    if (stackcount < STACKLEN) {
        stack[stackcount] = number;
        stackcount++;
    }
}

double pop(void)
{
    if (stackcount > 0) {
        stackcount--;
        return stack[stackcount];
    } else {
        return NAN;
    }
}

void stackPrint(void)
{
    printf("[ ");
    for (int i=0; i<stackcount; i++) {
        printf("%g ",stack[i]);
    }
    printf("]\n");
}

