Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #ifdef ERFANUL007
- #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
- template < typename Arg1 >
- void __f(const char* name, Arg1&& arg1){
- cout << name << " = " << arg1 << std::endl;
- }
- template < typename Arg1, typename... Args>
- void __f(const char* names, Arg1&& arg1, Args&&... args){
- const char* comma = strchr(names, ',');
- cout.write(names, comma - names) << " = " << arg1 <<" | ";
- __f(comma+1, args...);
- }
- #else
- #define debug(...)
- #endif
- int main(){
- #ifdef ERFANUL007
- clock_t tStart = clock();
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- int t, i=0; scanf("%d", &t);
- getchar();
- getchar();
- char s[60];
- stack< char > st;
- while(t){
- char ch;
- if(scanf("%c", &ch) == EOF){
- while(!st.empty()){
- s[i++]= st.top();
- st.pop();
- }
- s[i] = '\0';
- printf("%s\n", s);
- break;
- }
- //printf("%c", ch);
- if(ch == '\n'){
- while(!st.empty()){
- s[i++]= st.top();
- st.pop();
- }
- s[i] = '\0';
- printf("%s\n", s);
- i = 0;
- t--;
- if(t) printf("\n");
- continue;
- }
- else if(ch >= '0' && ch <='9') s[i++] = ch;
- else if(ch == '(') st.push(ch);
- else if(ch == ')'){
- while(!st.empty()){
- ch = st.top();
- st.pop();
- if(ch == '(') break;
- s[i++] = ch;
- }
- }
- else if(ch == '+' || ch == '-'){
- while(!st.empty() && st.top() != '('){
- s[i++] = st.top();
- st.pop();
- }
- st.push(ch);
- }
- else if(ch == '*' || ch == '/'){
- while(!st.empty() && st.top() != '('){
- if(st.top() == '+' || st.top() == '-') break;
- s[i++] = st.top();
- st.pop();
- }
- st.push(ch);
- }
- getchar();
- }
- #ifdef ERFANUL007
- fprintf(stderr, ">>> Runtime : %.9f\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement