#include <iostream>
#include <sstream>
using namespace std;
class Stack
{
private :
char *elements ;
int Size ;
int top = -1 ;
public :
Stack ()
{
Size = 100 ;
elements = new char [Size] ;
}
void puch ( char x )
{
if ( top != Size+1 )
elements[++top] = x ;
}
void pop ()
{
top-- ;
}
void view ()
{
for ( int i = 0 ; i <= top ; i++ )
cout << elements[i] << endl ;
}
char get_the_last ( )
{
return elements[top] ;
}
bool Is_empty ()
{
if ( top < 0 ) return true ;
return false ;
}
};
bool check ( char x , char y )
{
if ( x == '(' || x == '[' || x == '{' ) return false ;
else if ( y == '(' || y == '[' || y == '{' ) return false ;
else if ( x == '-' ) return true ;
else if ( y == '-' ) return false ;
else if ( x == '*' && y == '/' ) return false ;
else if ( x == '*' && y == '+' ) return false ;
else if ( x == '/' && y == '*' ) return true ;
else if ( x == '/' && y == '+' ) return false ;
else true ;
}
char get_bracket ( char x )
{
if ( x == ')' ) return '(' ;
else if ( x == '}' ) return '{' ;
else return '[' ;
}
string Handel_Exp ( string exp )
{
Stack s ;
string e ;
int i = 0 , q = 0 ;
while ( i < exp.length() )
{
if ( exp[i] == '(' || exp[i] == '{' || exp[i] == '[' || exp[i] == '+' || exp[i] == '-' || exp[i] == '*' || exp[i] == '/' )
{
if ( q > 0 )
{
e+=' ' ;
q = 0 ;
}
if ( s.Is_empty() == false )
{
char prev = s.get_the_last() ;
if ( check(exp[i],prev) == true )
{
while ( s.get_the_last() != '(' || s.get_the_last() != '{' || s.get_the_last() != '[' )
{
e = e+s.get_the_last() ;
s.pop() ;
if ( s.Is_empty() == true ) break ;
if (check(exp[i],s.get_the_last()) != true ) break ;
}
s.puch(exp[i]) ;
}
else s.puch(exp[i]) ;
}
else
{
s.puch(exp[i]) ;
}
}
else if ( exp[i] == ')' || exp[i] == '}' || exp[i] == ']' )
{
if ( q > 0 )
{
e+=' ' ;
q = 0 ;
}
char bracket = get_bracket(exp[i]) ;
while ( s.get_the_last() != bracket )
{
e = e+s.get_the_last() ;
s.pop() ;
}
s.pop() ;
}
else
{
e = e + exp[i] ;
q++ ;
}
i++ ;
}
if ( exp[i] != '+' || exp[i] != '-' || exp[i] != '/' || exp[i] != '*' ) { e+=' ' ; }
while ( s.Is_empty() == false )
{
e = e + s.get_the_last() ;
s.pop() ;
}
return e ;
}
string calculate ( string x , string y , char op )
{
stringstream ss ;
float first=0 , second=0 , third=0 ;
string z ;
ss.clear() ;
ss << x ;
ss >> first ;
ss.clear() ;
ss << y ;
ss >> second ;
if ( op == '+' ) third = first + second ;
else if ( op == '-' ) third = first - second ;
else if ( op == '*' ) third = first * second ;
else if ( op == '/' ) third = first / second ;
ss.clear() ;
ss << third ;
ss >> z ;
return z ;
}
string Reverse ( string x )
{
cout << x << endl ;
string y ;
int i = x.length()-1 ;
while ( i >= 0 )
{
y+= x[i] ;
i-- ;
}
return y ;
}
string evaluate ( string x )
{
Stack s ;
int i = 0 ;
string first , second ;
while ( i < x.length() )
{
if ( x[i] == '+' || x[i] == '-' || x[i] == '*' || x[i] == '/' )
{
s.pop() ;
while ( s.get_the_last() != ' ' )
{
second += s.get_the_last() ;
s.pop() ;
}
s.pop() ;
while ( s.get_the_last() != ' ' && s.Is_empty() == false )
{
first += s.get_the_last() ;
s.pop() ;
}
first = Reverse(first) ;
second = Reverse(second) ;
string third = calculate (first , second , x[i]) ;
first.clear() ;
second.clear() ;
int q = 0 ;
while ( q < third.length() )
{
s.puch(third[q]) ;
q++ ;
}
s.puch(' ') ;
}
else s.puch(x[i]) ;
i++ ;
}
string result ;
s.pop() ;
while ( s.Is_empty() == false )
{
result +=s.get_the_last() ;
s.pop() ;
}
result = Reverse(result) ;
return result ;
}
bool openPrace(char c)
{
if(c=='{' || c=='(' || c=='[') return true;
return false;
}
bool closePrace(char c)
{
if(c=='}' || c==')' || c==']') return true;
return false;
}
bool matched(char o,char c)
{
if(o=='{' && c=='}') return true;
if(o=='(' && c==')') return true;
if(o=='[' && c==']') return true;
return false;
}
bool checkPraces(string exp)
{
Stack s;
for(int i=0;i< exp.length();i++)
{
char c=exp[i];
if(openPrace(c))
{
s.puch(c);
}
else if(closePrace(c))
{
if(s.Is_empty()) return false;
char tmp=s.get_the_last();
if(matched(tmp,c)) s.pop();
else return false;
}
}
return s.Is_empty();
}
int main()
{
string x ;
cin >> x ;
string exp = Handel_Exp (x) ;
if ( checkPraces(x) == true )
{
cout << "matched" << endl ;
string c = evaluate(exp) ;
cout << c << endl ;
}
else
cout << "not matched" << endl ;
return 0;
}