#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring> #include <stack> #define db(a) cout << #a << " = " << a << endl; using namespace std; int main () { char c[2]; int cont = 0, casos; void *p; string res = ""; scanf("%d", &casos); gets(c); gets(c); for (int i = 0; i < casos; i++) { res = ""; stack<char> pila; while(1) { p = gets(c); if (p == NULL) break; if (c[0] == '\0') break; //original += c[0]; if (isdigit(c[0])) res += c[0]; else { if (c[0] == ')') { while(pila.top() != '(') { res += pila.top(); pila.pop(); } pila.pop(); continue; } if (c[0] == '(') { pila.push(c[0]); continue; } if(c[0] != '*' && c[0] != '/' && c[0] != '+' && c[0] != '-') continue; if (pila.empty()) { pila.push(c[0]); continue; } while(!pila.empty() && pila.top() != '(' && ((pila.top() == '*' || pila.top() == '/') || !((pila.top() == '+' || pila.top() == '-') && (c[0] == '*' || c[0] == '/')))) { res += pila.top(); pila.pop(); } pila.push(c[0]); } } while (!pila.empty()) { res += pila.top(); pila.pop(); } //db(original); if(i + 1 == casos) printf("%s\n", res.c_str()); else printf("%s\n\n", res.c_str()); } return 0; }
viernes, 21 de octubre de 2011
727 - Equation - UVA
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario