#include #include void skroc(char * A, char * B, int start, int end, char preop, char postop) { if(end == start + 1) { B[start] = A[start]; return; } if(end == start) { return; } int naw = 0; char lastop = 0; int oppos; int i; for(i = start; i < end; i++) { if(A[i] == '(') { naw++; continue; } if(A[i] == ')') { naw--; continue; } if(naw != 0) continue; if(A[i] == '+') { lastop = A[i]; oppos = i; } if(A[i] == '-') { lastop = A[i]; oppos = i; } } if(lastop != 0) { B[oppos] = A[oppos]; if((preop != '+' && preop != 0) || (postop != '+' && postop != '-' && postop != 0)) { B[start - 1] = '('; B[end] = ')'; } skroc(A, B, start, oppos, 0, lastop); skroc(A, B, oppos + 1, end, lastop, 0); return; } naw = 0; for(i = start; i < end; i++) { if(A[i] == '(') { naw++; continue; } if(A[i] == ')') { naw--; continue; } if(naw != 0) continue; if(A[i] == '*') { lastop = A[i]; oppos = i; } if(A[i] == '/') { lastop = A[i]; oppos = i; } } if(lastop != 0) { B[oppos] = A[oppos]; if(preop == '/') { B[start - 1] = '('; B[end] = ')'; } skroc(A, B, start, oppos, 0, lastop); skroc(A, B, oppos + 1, end, lastop, 0); return; } skroc(A, B, start + 1, end - 1, preop, postop); return; } int main() { char bufor1[255]; char bufor2[255]; char bufor3[255]; int T; cin >> T; cin.get(); for(int i = 0; i < T; i++) { cin.getline(bufor1, 255); memset(bufor2, 32, strlen(bufor1)); bufor2[strlen(bufor1)] = 0; skroc(bufor1, bufor2, 0, strlen(bufor1), 0, 0); int index2 = 0, index3 = 0; while(1) { if(bufor2[index2] != 32) { bufor3[index3] = bufor2[index2]; index3++; } if(bufor2[index2] == 0) { break; } index2++; } cout << bufor3 << endl; } return 0; }