#include #include struct tree { struct tree *l, *r; char s; tree() { l = r = NULL; } void print() { if( l != NULL && r != NULL ) { if( (s == '*' || s == '/') && (l->s == '+' || l->s == '-' ) ) { cout << '('; l->print(); cout << ')'; } else l->print(); cout << s; if( (s == '*' || s == '/') && (l->s == '+' || l->s == '-' ) ) { cout << '('; r->print(); cout << ')'; } else r->print(); } else cout << s; } }; char buf[1024], *s; tree *add( void ); tree *exp( void ) { // cout << "c = " << *s << "\n"; if( *s >= 'a' && *s <= 'z' ) { tree *t = new tree; t->s = *s; s++; return t; } if( *s == '(' ) { s++; tree *t = add(); s++; return t; } return 0; } tree *mul( void ) { // cout << "muil\n"; tree *l = exp(); if( *s == '*' || *s == '/' ) { tree *t = new tree(); t->s = *s; s++; tree *r = exp(); t->l = l; t->r = r; /* t->print(); cout << " - mul \n"; */ return t; } return l; } tree *add( void ) { // cout << "add\n"; tree *l = mul( ); if( *s == '+' || *s == '-' ) { tree *t = new tree(); t->s = *s; s++; tree *r = mul(); t->l = l; t->r = r; /* t->print(); cout << " - add \n"; */ return t; } return l; } int main() { int N; cin >> N; int i; for( i = 0; i < N; i++ ) { cin >> buf; s = buf; tree *t = add( ); t->print(); cout << "\n"; } return 0; }