Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int T;
- string S;
- bool operand(char x){
- if(int(x) >= 65 && int(x) <= 90)
- return true;
- if(int(x) >= 97 && int(x) <= 122)
- return true;
- return false;
- }
- int prec(char x){
- if(x == '^')
- return 3;
- if(x == '*' || x == '/')
- return 2;
- if(x == '-' || x == '+')
- return 1;
- return 0;
- }
- int main()
- {
- cin>>T;
- while(T--){
- cin>>S;
- stack<char> st;
- string res = "";
- for(int i=0; i<S.length(); ++i){
- if(operand(S[i]))
- res += S[i];
- else{
- if(S[i] == ')'){
- while(st.top() != '('){
- res += st.top();
- st.pop();
- }
- st.pop();
- }
- else if(st.empty() || prec(st.top()) < prec(S[i]) || S[i] == '(')
- st.push(S[i]);
- else{
- while(!st.empty() && prec(st.top()) >= prec(S[i])){
- res += st.top();
- st.pop();
- }
- st.push(S[i]);
- }
- }
- }
- while(!st.empty()){
- res += st.top();
- st.pop();
- }
- cout<<res<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement