Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<cstdio>
- #include<iostream>
- #include<cstring>
- #include<stack>
- #include<vector>
- using namespace std;
- int value(char ch)
- {
- if(ch=='+')
- return 1;
- else if(ch=='-')
- return 1;
- else if(ch=='*')
- return 2;
- else if(ch=='/')
- return 3;
- else if(ch=='^')
- return 4;
- return 0;
- }
- stack<char>x,y;
- char a[10010];
- vector<char>v;
- int main()
- {
- int n;
- scanf("%d",&n);
- getchar();
- getchar();
- while(n--)
- {
- while(gets(a))
- {
- int l=strlen(a);
- if(l==0)
- break;
- int i;
- for(i=0;i<l;i++)
- {
- if((a[i]>=48&&a[i]<=57)||(a[i]>=65&&a[i]<=90))
- {
- v.push_back(a[i]);
- }
- else if(a[i]=='(')
- x.push(a[i]);
- else if(a[i]=='+'|| a[i]=='-'|| a[i]=='*'||a[i]=='/'||a[i]=='^')
- {
- if(x.empty())
- x.push(a[i]);
- else
- {
- char r=x.top();
- if(value(r)>=value(a[i]))
- {
- while(!x.empty())
- {
- v.push_back(r);
- x.pop();
- if(x.empty())
- break;
- else
- r=x.top();
- if(value(r)<value(a[i]))
- break;
- }
- x.push(a[i]);
- }
- else
- x.push(a[i]);
- }
- }
- else if(a[i]==')')
- {
- while(!x.empty())
- {
- char ch=x.top();
- if(ch=='(')
- {
- x.pop();
- break;
- }
- v.push_back(ch);
- x.pop();
- }
- }
- }
- // printf("\n");
- }
- while(!x.empty())
- {
- char ch1=x.top();
- v.push_back(ch1);
- x.pop();
- }
- for(int i=0;i<v.size();i++)
- printf("%c",v[i]);
- printf("\n");
- v.clear();
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement