Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

Reverse polish notation problem

$
0
0
 question-->http://www.codechef.com/problems/ONP
 My solution is working fine on ideone as well on my system and giving right answer for test         
 given.But it is giving wrong answer on submission. I m not able to find the problem in my 
 my solution.Plz help.

 #include<iostream> 
 #include<stack>
 #include<stdio.h>
 #include<string.h>
 using namespace std;
 int main()
{ int t;
 cin>>t;
 stack<char> S;
 while(t--)
{
char s1[401],s2[401];
cin>>s1;
int len=strlen(s1);
s1[len]=')';
s1[len+1]='\0';
S.push('(');
int i=0,j=0;
while(!S.empty())
{  
     if(s1[i]=='('||s1[i]=='+'||s1[i]=='-'||s1[i]=='*'||s1[i]=='/'||s1[i]=='^')
    {
        S.push(s1[i]);
        i++;
    }
    else if(s1[i]==')')
    {
        while(S.top()!='(')
        {
            s2[j]=S.top();
            S.pop();
            j++;
        }
        S.pop();
        i++;
    }
    else
    {
        s2[j]=s1[i];
        i++;j++;
    }

    }

    int len2=strlen(s2);
    s2[len2]='\0';
    cout<<s2;
    cout<<"\n";
    }
    return 0; 
    }

Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>