#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
template<class ElemType>
class stack
{
private:
ElemType *base; // 栈底指针
ElemType *top; // 栈顶指针
int maxSize;
public:
//初始化顺序栈
stack(int MS=100)
{
base = new ElemType[MS];
top = base;
maxSize=MS;
}
//删除顺序栈
~stack()
{
stackDestroy();
}
//将顺序栈置为空表
bool StackClear()
{
top = base;
return true;
}
//返回顺序栈的长度
int StackLength() const
{
return top - base;
}
//设置顺序栈的长度
bool isEmpty() const
{
return top == base;
}
//判断顺序栈是否为满栈
bool isFull() const
{
return StackLength()==maxSize;
}
//用e返回栈顶元素
bool GetTop(ElemType &e) const
{
if(isEmpty()) return false;
e = *(top-1);
return true;
}
//入栈
bool push(ElemType &e)
{
if(isFull()) return false;//DoubleSpace();
*top = e;
top++;
return true;
}
//出栈
bool pop(ElemType &e)
{
if(isEmpty()) return false;
top--;
e=*top;
return true;
}
//销毁顺序栈
bool stackDestroy()
{
delete []base;
return true;
}
//遍历顺序栈
void stackTraverse() const
{
for(ElemType *p = top-1; p>=base; p--)
{
cout<<*p;
}
cout<<endl;
}
};
template<class ElemType>
int getprecedence(ElemType &operater)//优先级
{
switch(operater)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
}
return 0;
}
template<class ElemType>
void infix_to_suffix( stack<ElemType> &s, string &infix, string &suffix)
{
char e;
for(int i=0; i<infix.length(); i++)
{
if(isdigit(infix[i]))//遇到数字字符直接输出,因为我们oj上格式控制有要求,所以空格空格有点麻烦
{
cout<<infix[i];
if(!isdigit(infix[i+1])&&(i+1)!=infix.length())
cout<<" ";
if(i==infix.length()-1)
cout<<" ";
}
else if(infix[i]=='+'||infix[i]=='-'||infix[i]=='*'||infix[i]=='/')
{
if(s.isEmpty())//是空的的话就直接入栈
s.push(infix[i]);
else
{
s.GetTop(e);
if(getprecedence(e)>=getprecedence(infix[i]))//比较优先级,栈中运算符优先级大于或等于此运算符就弹出
{
s.pop(e);
cout<<e<<" ";
}
s.push(infix[i]);
}
}
else if(infix[i]=='('||infix[i]==')')//遇到右括号,就将栈中运算符一直弹出直至遇到左括号
{
if(infix[i]=='(')
s.push(infix[i]);
else
{
s.GetTop(e);
while(e!='(')
{
cout<<e<<' ';
s.pop(e);
s.GetTop(e);
}
s.pop(e);
}
}
if(i==infix.length()-1&&!s.isEmpty())
{
while(!s.isEmpty())
{
s.pop(e);
cout<<e;
if(s.StackLength()!=0)
cout<<" ";
}
}
}
cout<<endl;
}
int main()
{
stack<char>s;
string infix,suffix;
getline(cin,infix);
infix_to_suffix(s,infix,suffix);
return 0;
}