/*
 * Soubor:  20110401-Fri-kalkul.c
 * Datum:   01.04.2011 10:04
 * 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 ZASPOC 32
#define DELKA 32

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

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

/****************************************************
 *               Hlavní program.
 ****************************************************/
int main(void)
{
    char vstup[DELKA];
    double cislo, x, y;
    while (scanf("%s", vstup) != EOF) {
        if (sscanf(vstup, "%lg", &cislo) == 1) {
            push(cislo);
/*        } else if ( vstup[0]='+' ) {*/
        } else if (strcmp(vstup, "+") == 0) {
            y = pop();
            x = pop();
            if (isnan(x)) {
                push(y);
                fprintf(stderr, "potřebuji dva operandy\n");
            } else {
                push(x + y);
                tiskni();
            }
        } else if (strcmp(vstup, "-") == 0) {
            proved2(minus);
        } else if (strcmp(vstup, "*") == 0) {
            y = pop();
            x = pop();
            push(x * y);
            printf("%g\n", x * y);
        } else if (strcmp(vstup, "/") == 0) {
            y = pop();
            x = pop();
            push(x / y);
            printf("%g\n", x / y);
        } else if (strcmp(vstup, "ln") == 0) {
            x = pop();
            if (isnan(x)) {
                push(y);
                fprintf(stderr, "potřebuji dva operandy\n");
            } else {
                push(log(x));
                tiskni();
            }
        } else if (strcmp(vstup, "log") == 0) {
            x = pop();
            if (isnan(x)) {
                push(y);
                fprintf(stderr, "potřebuji dva operandy\n");
            } else {
                push(log10(x));
                tiskni();
            }
        } else if (strcmp(vstup, "**") == 0) {
            y = pop();
            x = pop();
            if (isnan(x)) {
                push(y);
                fprintf(stderr, "potřebuji dva operandy\n");
            } else {
                push(pow(x,y));
                tiskni();
            }
        } else if (strcmp(vstup, "print") == 0) {
            tiskni();
        } else {
            fprintf(stderr, "Neplatný vstup\n");
        }
    }

    return 0;
}

double zasobnik[ZASPOC];
int pocet = 0;

void push(double number)
{
    if (pocet < ZASPOC) {
        zasobnik[pocet] = number;
        pocet++;
    }
}

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

void tiskni(void)
{
    printf("[ ");
    for (int i = 0; i < pocet; i++) {
        printf("%g ", zasobnik[i]);
    }
    printf("]\n");
}

