Sunday, May 7, 2017

URI Online Judge Solution 1077 Infix to postfix

URI Online Judge Solution 1077 Infix to postfix



URI Online Judge Solution 1077 Infix to postfix | Data structures
URI Main Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1077

Problem Name: URI Problem  1077 Infix to postfix

Problem Number : URI Problem 1077 Infix to postfix Solution
Online Judge : URI Online Judge Solution
Level: Data structures
Solution Language : C, C plus plus

URI Online Judge Solution 1077 Infix to postfix

URI Solution 1077 Infix to postfix Code in CPP:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;

//Check the priority of operators function
int priority(char symbol)
{
    switch(symbol)
    {
    case '+':
    case '-':
        return 1;
    case '*':
    case '/':
        return 2;
    case '^':
        return 3;
    default :
        return 0;
    }
}

int main()
{
 
    string ara;
    stack <char> mystack;
    vector <char> output;
    int tc;
 
    cin>>tc;
    while(tc--){
    cin>>ara;
 
    for(int i=0;i<ara.length();i++){
 
            if( ara[i] == '+' || ara[i] == '-' || ara[i] == '*' || ara[i] == '/' || ara[i] == '^'){
                while( !mystack.empty() && priority(mystack.top()) >= priority(ara[i]) ){
                    output.push_back(mystack.top());
                    mystack.pop();
                }
                mystack.push(ara[i]);
            }
            else if(ara[i] == '('){
                mystack.push(ara[i]);
            }
            else if( ara[i]==')' ){
                while( mystack.top() !='(' ){
                      output.push_back(mystack.top());
                      mystack.pop();
                }
                mystack.pop();
            }
            else {
                output.push_back(ara[i]);
            }
 
    }
 
        while( !mystack.empty() ){
            output.push_back(mystack.top());
            mystack.pop();
        }
 
        for(int i=0;i<output.size();i++){
            cout<<output[i];
        }
        cout<<endl;
        while(!output.empty()){
            output.pop_back();
        }
 
    }
 
    return 0;
}

No comments:
Write comments

To know more about the problem, give us your valuable commment. We'll try to help you. Thanks

All rights reserved ©2016 -URI ONLINE JUDGE SOLUTION | Developed by Maniruzzaman Akash

© 2016 URI ONLINE JUDGE SOLUTION. Developed by Maniruzzaman Akash | Distributed By Gooyaabi Templates
Powered by Blogger.