
#include <stdio.h>

#define SIZE 10
double stack[SIZE];
int sp;

void push(double value);
double pop(void);
int isFull(void);
int isEmpty(void);
void answer(void);
void reset(void);

int main(void)
{
    int resp;
    double a, b, date;

    reset();

    while(1){
    printf("1:+ 2:- 3:* 4:/ 5:データ入力 9:結果表示 0:終了\n");

    if(scanf("%d", &resp) != 1){
    printf("数値を入力してください\n");
    break;
    }

    if(resp == 0) break;

    switch(resp){
    case 1:
    b = pop();
    a = pop();
    push(a + b);
    break;

    case 2:
    b = pop();
    a = pop();
    push(a - b);
    break;

    case 3:
    b = pop();
    a = pop();
    push(a * b);
    break;

    case 4:
    b = pop();
    a = pop();
    push(a / b);
    break;

    case 5:
    printf("数値を入力してください：");
    scanf("%lf", &date);
    push(date);
    break;

    case 9:
    answer();
    break;
        }
    }

    answer();

    return 0;
}

void push(double value)
{
    if(isFull()){
    printf("スタックが満杯です\n");
    return;
    }

    stack[sp] = value;
    sp++;
}

double pop(void)
{
    if(isEmpty()){
    printf("スタックが空です\n");
    return 0;
    }

    sp--;
    return stack[sp];
}

int isFull(void)
{
    return sp >= SIZE;
}

int isEmpty(void)
{
    return sp <= 0;
}

void answer(void)
{
    if(!isEmpty()){
    printf("計算結果：%f\n", stack[sp - 1]);
    }
    else{
    printf("スタックが空です\n");
    }
}

void reset(void)
{
    sp = 0;
}
