#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<vector>
using std::vector;
typedef struct stack {
struct stack* pre;
struct stack* next;
char val;
int size;
}Stack;
Stack* base,*top;
int size(Stack *base)
{
return base->size;
}
bool empty(Stack*base, Stack*top)
{
if (base == top)return true;
return false;
}
bool pop(Stack*base,Stack*&top)
{
if (!empty(base,top))
{
base->size--;
top = top->pre;
return true;
}
return false;
}
void push(Stack*base,Stack*&top,char x)
{
Stack* newElem = (Stack*)malloc(sizeof(Stack));
newElem->val = x;
newElem->pre = top;
top->next = newElem;
top = newElem;
base->size++;
}
void Init(Stack*&base, Stack*&top)
{
base = (Stack*)malloc(sizeof(Stack));
base->size = 0;
top = base;
top->pre = base;
}
void Print(Stack* base, Stack* top)
{
Stack* t = top;
while (!empty(base, t))
{
printf("%c ", t->val);
pop(base, t);
}
}
int Calc(int x, int y, char ch)
{
switch (ch)
{
case '+':
return x + y;
case '-':
return x - y;
case '*':
return x * y;
case '/':
return x / y;
case '^':
int res = 1;
for (int i = 1; i <= y; ++i)
{
res *= x;
}
return res;
}
}
int rank(char ch)
{
switch (ch)
{
case '(': return -1;
case '+': return 0;
case '-': return 0;
case '*': return 1;
case '/': return 1;
case '^': return 2;
}
}
typedef struct {
int data;
bool isOperator;
}Pair;
char ch[51];
vector<Pair>Ans;
void Print_ans()
{
for (int i = 0; i < Ans.size(); ++i)
{
if (Ans[i].isOperator)
{
printf("%c ", Ans[i].data);
}
else
{
printf("%d ", Ans[i].data);
}
}
puts("");
}
void work_2()
{
Print_ans();
for (int i = 0; i < Ans.size();)
{
if (Ans[i].isOperator)
{
int x = Ans[i - 2].data, y = Ans[i - 1].data;
int res = Calc(x, y, Ans[i].data);
Ans.erase(Ans.begin() + (i - 2), Ans.begin() + (i + 1));
Ans.insert(Ans.begin() + (i - 2), Pair{res,0});
Print_ans();
i = i - 1;
}
else i++;
}
}
void work()
{
Init(base, top);
scanf("%s", ch);
for (int i = 0; i < strlen(ch); ++i)
{
if (ch[i]>='0' && ch[i]<='9')
{
Ans.push_back(Pair{ch[i]-'0', 0});
}
else
{
if (ch[i] == '(')
{
push(base, top, ch[i]);
continue;
}
if (empty(base, top)) { push(base, top, ch[i]); continue; }
if (ch[i] == ')')
{
while (!empty(base, top) && top->val != '(')
{
Ans.push_back(Pair{ top->val,1 });
pop(base, top);
}
pop(base, top);
continue;
}
int Rank = rank(ch[i]), Rank_top = rank(top->val);
if (Rank > Rank_top)
{
push(base,top,ch[i]);
}
else if (Rank == Rank_top && ch[i] == '^')
{
push(base, top, ch[i]);
}
else
{
while (!empty(base, top) && Rank <= Rank_top)
{
Ans.push_back(Pair{ top->val,1 });
pop(base, top);
Rank_top = rank(top->val);
}
push(base, top, ch[i]);
}
}
}
while (!empty(base, top))
{
Ans.push_back(Pair{ top->val,1 });
pop(base, top);
}
//Print_ans();
}
int main()
{
//freopen("a.txt", "r", stdin);
work();
work_2();
return 0;
}