#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
const int SIZE = 50;
class Stack_number {
private:
int num[SIZE];
int top1;
public:
Stack_number();
//~Stack_number();
void push(int q);
int pop();
int get_top();
};
class Stack_operator {
private:
char op[SIZE];
int top2;
public:
Stack_operator();
//~Stack_operator();
void push(char ch);
char pop();
char get_top();
};
bool In(char ch); //判断输入的符号是否是运算符!
char Precede(char ch1, char ch2); //比较运算符优先级别!
int Operate(int i, char ch, int j);//运算!
int main() {
Stack_number OPND;
Stack_operator OPTR;
int nu, n = 0, p = 0, q = 0;
char c, o;
char ch = getchar(); //getchar()是stdio.h中的库函数,它的作用是从stdin流中读入一个字符
//也就是说,如果stdin有数据的话不用输入它就可以直接读取了
//第一次getchar()时,确实需要人工的输入,但是如果你输了多个字符,以后的getchar()再执行时就会直接从缓冲区中读取了
//实际上是 输入设备->内存缓冲区->getchar()
while (ch != '#' || OPTR.get_top() != '#') {
if (!In(ch)) {
//while (!In(ch)) {
nu = int(ch) - 48;
//n++;//n用来记录当前读入数字的位数!
OPND.push(nu);
ch = getchar();
//}
}
else {
o = OPTR.get_top();
switch (Precede(o, ch))
{
case'<':
OPTR.push(ch);
ch = getchar();
break;
case'>':
p = OPND.pop();
q = OPND.pop();
c = OPTR.pop();
OPND.push(Operate(q, c, p));
ch = getchar();
break;
case'=':
OPTR.pop();
ch = getchar();
break;
}
}
}
cout << OPND.get_top();
return 0;
}
void Stack_number::push(int q) {
if (top1 < SIZE)
num[top1++] = q;
}
int Stack_number::pop() {
if (top1 > 0)
return num[top1--];
}
int Stack_number::get_top() {
if (top1 > 0)
return num[top1-1];
}
void Stack_operator::push(char ch) {
if (top2 < SIZE)
op[top2++] = ch;
}
char Stack_operator::pop() {
if (top2 > 0)
return op[top2--];
}
char Stack_operator::get_top() {
if (top2 > 0)
return op[top2-1];
}
//判断输入的符号是否是运算符!
bool In(char ch) {
if (ch == '-' || ch == '+' || ch == '*' || ch == '/' || ch == '(' || ch == ')' || ch == '#')
return true;
else
return false;
}
//比较运算符优先级别!
char Precede(char ch1, char ch2) {
int ang[2] = { 0,0 };
char op[2] = { ch1,ch2 };
char aa[7][7] = {
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}
}; //不要忘了 ;!
for (int i = 0; i < 2; i++) {
switch (op[i]) {//利用op[i]记录θ1和θ2!!!
case '+':ang[i] = 0;
break;
case'-':ang[i] = 1;
break;
case '*':ang[i] = 2;
break;
case'/':ang[i] = 3;
break;
case '(':ang[i] = 4;
break;
case')':ang[i] = 5;
break;
case'#':ang[i] = 6;
break;
}
//绝妙!i=0时 ang[0]=0~6来确定aa数列的第几行!
//i=1时ang[1] = 0~6来确定aa数列的第几列!
}
return aa[ang[0]][ang[1]];//格外注意该处格式较模糊复杂!
}
//进行二元运算!
int Operate(int i, char ch, int j) {
switch (ch) {
case'+':return i + j;
break;
case'-':return i - j;
break;
case'*':return i * j;
break;
case'/':return i / j;
break;
//default:
//break;
}
}
Stack_number::Stack_number() {
num[0] = 0;
top1 = 0;
}
//Stack_number::~Stack_number() {
//}
Stack_operator::Stack_operator() {
op[0] = '#';
top2 = 1;
}
//Stack_operator::~Stack_operator(){
//}