727 - Equation (Runtime Error)

All about problems in Volume VII. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

727 - Equation (Runtime Error)

Postby darkurvivor » Wed May 16, 2012 2:18 pm

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;
}
darkurvivor
New poster
 
Posts: 3
Joined: Tue Oct 25, 2011 5:19 pm

Re: 727 - Equation (Runtime Error)

Postby brianfry713 » Wed May 16, 2012 11:34 pm

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+
brianfry713
Guru
 
Posts: 1771
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 727 - Equation (Runtime Error)

Postby darkurvivor » Thu May 17, 2012 3:44 pm

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!!
darkurvivor
New poster
 
Posts: 3
Joined: Tue Oct 25, 2011 5:19 pm

Re: 727 - Equation (Runtime Error)

Postby shuvokr » Thu Feb 14, 2013 9:59 pm

Please help >> I got RE again and again .....

Code: Select all
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>

using namespace std;

#define sf scanf
#define pf printf
#define LLU unsigned long long
#define Lu unsigned long
#define LLD long long
#define LD long

int main()
{
    int T = 0, i = 0, cou = 0;
    char c;
    sf("%d", &T);
    getchar();
    getchar();
    while(T--)
    {
        stack <char> st;
        char ans[100000] = {0};
        i = 0;
        cou = 0;
        while(sf("%c", &c) != EOF)
        {
            getchar();
            if(c == '(' || c == '*' || c == '/' || c == '+' || c == '-')
            {
               if(c == '(') cou++;
               if(cou == 0)
               {
                   while(!st.empty())
                   {
                       ans[i++] = st.top();
                       st.pop();
                   }
               }
               st.push(c);
            }
            else if(c == ')')
            {
                while(!st.empty() && st.top() != '(')
                {
                        ans[i++] = st.top();
                        st.pop();
                }
                st.pop();
                cou--;
            }
            else
            {
                ans[i++] = c;
            }
        }
        while(!st.empty())
        {
            ans[i++] = st.top();
            st.pop();
        }
        puts(ans);
    }
    return 0;
}
shuvokr
New poster
 
Posts: 28
Joined: Tue Oct 02, 2012 8:16 pm

Re: 727 - Equation (Runtime Error)

Postby lbv » Fri Feb 15, 2013 12:58 am

shuvokr wrote:Please help >> I got RE again and again .....

The way you handle the input seems flaky. Have you tried testing an input file with more than one test case?

Check for example:

Input
Code: Select all
2

3

1
+
2

Output
Code: Select all
3

12+
lbv
Learning poster
 
Posts: 54
Joined: Tue Nov 29, 2011 8:40 am

Re: 727 - Equation (Runtime Error)

Postby brianfry713 » Fri Feb 15, 2013 2:45 am

From uhunt:
AKJ88> Inside [ else if(c == ')') ] you've used [ st.pop() ], what if stack is empty?
brianfry713
Guru
 
Posts: 1771
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 727 - Equation (Runtime Error)

Postby shuvokr » Sat Feb 16, 2013 4:41 pm

ibv & brianfry thanks a lot for reply. but i didn't point out my bug... :(
Again and again RE ...
Please help
this is my update code::

Code: Select all
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>

using namespace std;

#define sf scanf
#define pf printf
#define LLU unsigned long long
#define Lu unsigned long
#define LLD long long
#define LD long

int main()
{
    int T = 0, i = 0, cou = 0, len, j = 0;
    bool ck = true;
    sf("%d", &T);
    getchar();
    while(T--)
    {
        getchar();
        if(!ck) pf("\n");
        ck = false;
        char c;
        stack <char> st;
        char ans[100000] = {0};
        cou = 0;
        while(sf("%c", &c) != EOF)
        {
            getchar();
            if(c == '(' || c == '*' || c == '/' || c == '+' || c == '-')
            {
               if(c == '(') cou++;
               if(cou == 0)
               {
                   while(!st.empty())
                   {
                       ans[j++] = st.top();
                       st.pop();
                   }
               }
               st.push(c);
            }
            else if(c == ')')
            {
                while(st.top() != '(')
                {
                    if(!st.empty())
                      {
                        ans[j++] = st.top();
                        st.pop();
                      }
                }
                if(!st.empty())
                    st.pop();
                cou--;
            }
            else
            {
                ans[j++] = c;
            }
        }
        while(!st.empty())
        {
            ans[j++] = st.top();
            st.pop();
        }
        puts(ans);
        j = 0;
    }
    return 0;
}
shuvokr
New poster
 
Posts: 28
Joined: Tue Oct 02, 2012 8:16 pm

Re: 727 - Equation (Runtime Error)

Postby lbv » Sat Feb 16, 2013 6:12 pm

shuvokr wrote:Again and again RE ...
Please help

  • Do test your program against an input file with more than one test case. Check the way you handle the input, in particular the blank line between two consecutive cases. I'm not sure about the reason for the RE, but it could be that the input has garbage at the end, so consider that as a possibility.
  • Read carefully what the problem statement says about the precedence of the operators.

Try for example:

Input
Code: Select all
2

2
+
3
*
5

(
(
9
)
)

)

Output
Code: Select all
235*+

9

Notice the extra ')' at the end. As I said, I'm not sure if something like this happens in the judge's data, but it could be one reason why your code produces RE. If your program handles the input robustly, it shouldn't be a problem.
lbv
Learning poster
 
Posts: 54
Joined: Tue Nov 29, 2011 8:40 am

Re: 727 - Equation (Runtime Error)

Postby shuvokr » Sun Feb 17, 2013 10:20 am

@lbv Again thanks for reply
Lastly I got AC :D
shuvokr
New poster
 
Posts: 28
Joined: Tue Oct 02, 2012 8:16 pm

Re: 727 - Equation (Runtime Error)

Postby waleed.lutfi » Sun Mar 31, 2013 1:42 pm

I keep getting WA, although I passed all test cases written here.
Can anyone write more test cases please.
waleed.lutfi
New poster
 
Posts: 5
Joined: Thu Jul 19, 2012 1:02 am

Re: 727 - Equation (Runtime Error)

Postby brianfry713 » Mon Apr 01, 2013 9:42 pm

You can generate your own at:http://www.uvatoolkit.com/problemssolve.php
brianfry713
Guru
 
Posts: 1771
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA


Return to Volume VII

Who is online

Users browsing this forum: No registered users and 0 guests