Page 8 of 11

Re: 727 - Equation

Posted: Sun Sep 12, 2010 3:13 pm
by iqbal csedu
ahhhh....alhamdulillah at last got accepted :D :D many many thanks to sazzad vai....have u got my personal message?@sazzad vai

Re: 727 - Equation

Posted: Wed Dec 22, 2010 10:40 am
by justcodeit
Please help me....

I m getting runtime error in this problem

i tried eveything......
is STL allowed...

Code: Select all

#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
int main()
{
    long long t,k;
    cin>>t;
    k=t;
    getchar();
    getchar();
    while(t--)
    {
         if(t!=k-1)
             cout<<endl<<endl;
         stack<char> st;
         long long n;
         char infix;
         char postfix[51]="",infix_ex[51]="";
         long long i=0,j;
         while(1)    
         {
              infix=getchar();  
              if(infix=='\n')
                     break;     
              getchar();
              
              infix_ex[i++]=infix;
         }  
         infix_ex[i++]=')';
         n=i;
         st.push('(');
         j=0;
         for(i=0;i<n;i++)
         {
               if( infix_ex[i]=='0' || infix_ex[i]=='1' || infix_ex[i]=='2' || infix_ex[i]=='3' || infix_ex[i]=='4' || infix_ex[i]=='5' || infix_ex[i]=='6' || infix_ex[i]=='7' || infix_ex[i]=='8' || infix_ex[i]=='9' )              
               {
                      postfix[j++]=infix_ex[i];                   
               }
               if(infix_ex[i]=='('  )
               {
                      st.push('(');                     
               }
               
               if(infix_ex[i]=='*' || infix_ex[i]=='/')              
               {
                       while(st.top()=='*' || st.top()=='/')
                       {
                            char temp=st.top();
                            st.pop();                 
                            postfix[j++]=temp;
                       }
                       st.push(infix_ex[i]);
               }
               if(infix_ex[i]=='+' || infix_ex[i]=='-')              
               {
                       while(st.top()=='*' || st.top()=='/' || st.top()=='+' || st.top()=='-')
                       {
                            char temp=st.top();
                            st.pop();                 
                            postfix[j++]=temp;
                       }
                       st.push(infix_ex[i]);
               }
               if(infix_ex[i]==')'  )
               {
                      while(st.top()!='(')
                      {
                            char temp=st.top();
                            st.pop();                 
                            postfix[j++]=temp;                    
                      }     
                      st.pop();               
               }
         }
         cout<<postfix; 
    }
    return 0;
}
please help.....
thanks in advance

Re: 727 - Equation

Posted: Wed Nov 02, 2011 11:40 pm
by brianfry713
justcodeit, you need to check for the input terminating with end of file and print a newline after the last input.

727 - Equation (Runtime Error)

Posted: Wed May 16, 2012 2:18 pm
by darkurvivor
i've run a lot of input from forum. all the answer is the correct!
but while i submit it returns RE! plz help me to find somewhere wrong!

Code: Select all

#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;

int main(){

    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    #endif

    size_t i;
    int n, run=0;
    bool first=true;
    string str;
    cin >> n;

    getchar();
    getline(cin, str);

    while(run < n){
        vector<string> eq;
        stack<char> op;

        if(!first) cout << endl;
        first=false;

        while(getline(cin, str) && str.size()!=0){
            eq.push_back(str);
        }

        for(i=0 ; i<eq.size() ; ++i){

            if(eq[i][0]=='('){
               op.push(eq[i][0]);
            }
            else if(eq[i][0]==')'){
                while(op.top()!='('){
                    cout << op.top();
                    op.pop();
                }
                op.pop();
                if(!op.empty()){
                    if(op.top()=='*'||op.top()=='/'){
                        cout << op.top();
                        op.pop();
                    }
                }
            }
            else if(eq[i][0]<='9' && eq[i][0]>='0'){
                cout << eq[i];
            }
            else{
                if(op.empty() || (!op.empty() && op.top()=='(')){
                   op.push(eq[i][0]);
                }
                else{
                    if(eq[i][0]=='+'||eq[i][0]=='-'){
                        if(op.top()=='*'||op.top()=='/'){
                            cout << op.top();
                            op.pop();
                            if(!op.empty()){
                                cout << op.top();
                                op.pop();
                            }
                        }
                        else{
                            cout << op.top();
                            op.pop();
                        }
                        op.push(eq[i][0]);
                    }
                    else if(eq[i][0]=='*'||eq[i][0]=='/'){
                        if(!op.empty() && (op.top()=='+'||op.top()=='-')){
                            op.push(eq[i][0]);
                        }
                        else{
                            cout << op.top();
                            op.pop();
                            op.push(eq[i][0]);
                        }
                    }
                }
            }
        }
        while(!op.empty()){
            cout << op.top();
            op.pop();
        }
        cout << endl;
        run++;
    }
    return 0;
}

Re: 727 - Equation (Runtime Error)

Posted: Wed May 16, 2012 11:34 pm
by brianfry713
Next time post in the existing thread. For this input:

Code: Select all

1

1
*
2
(
1
+
2
)
1
+
2
My AC code prints:

Code: Select all

1212+1*2+

Re: 727 - Equation (Runtime Error)

Posted: Thu May 17, 2012 3:44 pm
by darkurvivor
brianfry713 wrote:Next time post in the existing thread. For this input:

Code: Select all

1

1
*
2
(
1
+
2
)
1
+
2
My AC code prints:

Code: Select all

1212+1*2+
Sorry! I'll post in the exist thread next time.
I run your input and get another answer
but it's not the problem where it is
i found that line 66 lose a statement
while i fixed it as

Code: Select all

if(!op.empty() && op.top()!='(') 
i get AC!!

727 Equation TLE

Posted: Tue Jan 01, 2013 8:22 pm
by laituanksa245
My program passes all the test cases posted in the this forum.
However, I keep getting TLE
Can anyone give me some suggestion how to speed this up ?

Code: Select all

Code Removed
Got Ac :P

Re: 727 Equation TLE

Posted: Tue Jan 01, 2013 11:55 pm
by brianfry713
Your code is throwing a RE on the sample input.

Re: 727 Equation TLE

Posted: Wed Jan 02, 2013 6:11 am
by laituanksa245
I think the error comes from how my program does input.
How do you recognize a blank line from the input ?

Re: 727 Equation TLE

Posted: Wed Jan 02, 2013 9:35 pm
by brianfry713
Use something like getline, gets, or fgets to read the input.

Re: 727 Equation TLE

Posted: Thu Jan 03, 2013 5:44 am
by laituanksa245
Thanks :)

Re: 727 Equation TLE

Posted: Mon Jan 21, 2013 12:01 pm
by hover1178
Removed

Re: 727 Equation TLE

Posted: Thu Jan 31, 2013 9:05 am
by gr81
getting RE, please find the code at http://ideone.com/yvuQZE

Thanks

Re: 727 Equation TLE

Posted: Mon Feb 04, 2013 10:45 am
by gr81
will someone look into my issue...?

Re: 727 Equation TLE

Posted: Wed Feb 06, 2013 12:40 pm
by lbv
gr81 wrote:getting RE, please find the code at http://ideone.com/yvuQZE
It seems that it's possible that your oper stack gets cleared under some circumstances when it shouldn't, causing the while at line 41 to be skipped (because the stack is empty), but then you pop from oper anyway (line 46), which causes all the troubles.

Check for example this test case:

Code: Select all

1

(
5
/
3
+
5
+
0
)
-
4
*
(
2
)
*
0
/
2
-
5
*
5
/
3
/
6
*
2
+
7
-
9
-
2
/
3

Code: Select all

53/5+0+42*0*2/-55*3/6/2*-7+9-23/-