#include <iostream>
#include <stdio.h>
using namespace std;
char priority[7][7] = { {'>', '>', '<','<', '<', '>', '>'},
{'>','>', '<', '<', '<', '>','>'},
{'>','>', '>','>', '<','>','>'},
{'>','>', '>', '>', '<', '>', '>'},
{'<','<', '<', '<','<', '=', 'E'},
{'>', '>', '>', '>','E', '>','>'},
{'<','<','<','<','<','E', '='}};
struct Node
{
int info;
Node *next;
};
void push(Node **head, int info)
{
Node *temp = NULL;
temp = (Node*)malloc(sizeof(Node));
// assert(temp!=NULL);
temp->info = info;
temp->next = *head;
*head = temp;
}
bool pop(Node **head, int *info)
{
if(*head !=NULL)
{
Node *temp;
*info = (*head)->info ;
temp = *head;
*head = (*head)->next;
free(temp);
return true;
}
return false;
}
void peek(Node **head, int *info)
{
if(*head)
*info = (*head)->info ;
}
void makempty(Node **head)
{
Node *temp;
while(*head!=NULL)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
*head = NULL;
}
bool is_empty(Node **head)
{
return (*head) ? true : false;
}
bool is_operand(int ch) //判断用户输入的是否是一个操作数.
{
if(ch>=48 && ch<= 56)
{
return true;
}
return false;
}
int compute(int op, int left, int right)
{
switch(op)
{
case '+':
{
return (left)+right;
break;
}
case '-':
{
return left-right;
break;
}
case '*':
{
return left*right;
break;
}
case '/':
{
return left/right;
break;
}
default:
{
return -1;
}
}
}
int is_priority(int left_o, int right_o)
{
int row;
int colum;
switch(left_o)
{
case '+':
row = 0;
break;
case '-':
row = 1;
break;
case '*':
row = 2;
break;
case '/':
row = 3;
break;
case '(':
row = 4;
break;
case ')':
row = 5;
break;
case '#':
row = 6;
break;
default:
break;
}
switch(right_o)
{
case '+':
colum = 0;
break;
case '-':
colum = 1;
break;
case '*':
colum = 2;
break;
case '/':
colum = 3;
break;
case '(':
colum = 4;
break;
case ')':
colum = 5;
break;
case '#':
colum = 6;
break;
default:
break;
}
return priority[row][colum];
}
int main()
{
int c;
int pri;
int left_o;
int a;
int b;
char e, d;
struct Node *op = NULL;
Node *operand = NULL;
c = getchar();
push(&op, '#');
while(c!='#' || is_empty(&op))
{
if(is_operand(c))
{
c= c-48;
push(&operand, c);
c = getchar();
}
else
{
peek(&op, &left_o);
e = left_o;
d = c;
switch(pri = is_priority( left_o, c))
{
case '<':
{
push(&op, c);
c=getchar();
break;
}
case '=':
{
pop(&op, &left_o); //=号出栈
break;
}
case '>':
{
pop(&op, &left_o);
pop(&operand, &a);
pop(&operand, &b);
push(&operand,compute( left_o, a, b));
break;
}
}
}
}
pop(&operand, &left_o);
cout<<left_o<<endl;
return 0;
}