#include <stdio.h>
#include <stdlib.h>
#define N 64
typedef struct _seqstack_ {
int *data;
int top;
int size;
}SeqStack;
SeqStack *creat_stack(int size);
int push_stack(SeqStack *stack, int value);
int pop_stack(SeqStack *stack);
int isempty(SeqStack *stack);
int isfull(SeqStack *stack);
int get_top(SeqStack *stack);
int priority(int opt);
int comput(int opd1, int opt, int opd2);
int process_expression(SeqStack *operand, SeqStack *operator, int opt);
int free_stack(SeqStack *stack);
int main()
{
SeqStack *operand = NULL;
SeqStack *operator = NULL;
char buff[N];
char *p = buff;
int sum = 0;
operand = creat_stack(N);
operator = creat_stack(N);
gets(buff);
while(*p != '\0')
{
if(*p >= '0' && *p <= '9')
{
sum = 0;
while(*p >= '0' && *p <= '9')
{
sum *= 10;
sum += *p - '0';
p ++;
}
push_stack(operand,sum);
}
else
process_expression(operand, operator, *p ++);
}
while(!isempty(operator))
{
int opd1,opd2,opt,result;
opd2 = pop_stack(operand);
opd1 = pop_stack(operand);
opt = pop_stack(operator);
result = comput(opd1,opt, opd2);
push_stack(operand,result);
}
printf("%d\n",get_top(operand));
free_stack(operator);
free_stack(operand);
return 0;
}
int process_expression(SeqStack *operand, SeqStack *operator, int opt)
{
int opd1, opd2, opt1, result;
if( !isempty(operator) && priority(opt) <= priority(get_top(operator)) )
{
opd2 = pop_stack(operand);
opd1 = pop_stack(operand);
opt1 = pop_stack(operator);
result = comput(opd1,opt1,opd2);
push_stack(operand,result);
}
// else
push_stack(operator,opt);
}
SeqStack *creat_stack(int size)
{
SeqStack *stack = NULL;
stack = (SeqStack *)malloc(sizeof(SeqStack));
stack->data = (int *)malloc(sizeof(int) * size);
stack->top = -1;
stack->size = size;
return stack;
}
int push_stack(SeqStack *stack, int value)
{
if(isfull(stack))
return -1;
stack->data[++ stack->top] = value;
return 0;
}
int pop_stack(SeqStack *stack)
{
if(isempty(stack))
return 0;
return stack->data[stack->top --];
}
int isempty(SeqStack *stack)
{
return stack->top < 0 ? 1 : 0;
}
int isfull(SeqStack *stack)
{
return stack->top >= stack->size - 1 ? 1 : 0;
}
int get_top(SeqStack *stack)
{
if(isempty(stack))
return 0;
return stack->data[stack->top];
}
int priority(int opt)
{
switch(opt)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
int comput(int opd1, int opt, int opd2)
{
switch(opt)
{
case '+':
return opd1 + opd2;
case '-':
return opd1 - opd2;
case '*':
return opd1 * opd2;
case '/':
return opd1 / opd2;
default:
return 0;
}
}
int free_stack(SeqStack *stack)
{
free(stack->data);
free(stack);
return 0;
}