## 727 - Equation

Moderator: Board moderators

gr81
New poster
Posts: 46
Joined: Wed Sep 26, 2012 7:52 pm

### Re: 727 Equation TLE

thanks man, line 63 got problem, somehow '(' was getting poped up because of perced check.fixed it got AC.

shuvokr
Learning poster
Posts: 66
Joined: Tue Oct 02, 2012 8:16 pm

### Re: 727 - Equation (Runtime Error)

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;
}
``````

Code: Select all

``enjoying life ..... ``

lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

### Re: 727 - Equation (Runtime Error)

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+
``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 727 - Equation (Runtime Error)

From uhunt:
AKJ88> Inside [ else if(c == ')') ] you've used [ st.pop() ], what if stack is empty?
Check input and AC output for thousands of problems on uDebug!

shuvokr
Learning poster
Posts: 66
Joined: Tue Oct 02, 2012 8:16 pm

### Re: 727 - Equation (Runtime Error)

ibv & brianfry thanks a lot for reply. but i didn't point out my bug...
Again and again RE ...
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;
}
``````

Code: Select all

``enjoying life ..... ``

lbv
Experienced poster
Posts: 128
Joined: Tue Nov 29, 2011 8:40 am

### Re: 727 - Equation (Runtime Error)

shuvokr wrote: Again and again RE ...
• 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.

shuvokr
Learning poster
Posts: 66
Joined: Tue Oct 02, 2012 8:16 pm

### Re: 727 - Equation (Runtime Error)

Lastly I got AC

Code: Select all

``enjoying life ..... ``

waleed.lutfi
New poster
Posts: 5
Joined: Thu Jul 19, 2012 1:02 am

### Re: 727 - Equation (Runtime Error)

I keep getting WA, although I passed all test cases written here.
Can anyone write more test cases please.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 727 - Equation (Runtime Error)

You can generate your own at:http://www.uvatoolkit.com/problemssolve.php
Check input and AC output for thousands of problems on uDebug!

abcman13
New poster
Posts: 6
Joined: Sun Apr 07, 2013 6:00 am

### Re: 727 - Equation

hello all. could one of you help me find the cause of a runtime error with my code? i ran all the test cases on this thread succesfully on my computer (im running windows 7, not quite sure if that's important).

Code: Select all

``accepted``
Last edited by abcman13 on Sat Jul 20, 2013 1:18 am, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 727 - Equation

On my LINUX machine using g++ your code is stuck in an infinite loop on the sample input.
You can also try http://ideone.com/iZ7L1q
Try changing your input parsing. Maybe check if gets() returns NULL.
Check input and AC output for thousands of problems on uDebug!

abcman13
New poster
Posts: 6
Joined: Sun Apr 07, 2013 6:00 am

### Re: 727 - Equation

thanks for the help. my code was accepted.

software
New poster
Posts: 3
Joined: Tue Aug 27, 2013 12:15 am

### education(runtime error)

Problem no:727
I got RE error but I can not what the problem is in my code.Please help me.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: education(runtime error)

Runtime Error (RE): Your program failed during the execution (segmentation fault, floating point exception...). The exact cause is not reported to the user to avoid hacking. Be sure that your program returns a 0 code to the shell. If you're using Java, please follow all the submission specifications.
Check input and AC output for thousands of problems on uDebug!

software
New poster
Posts: 3
Joined: Tue Aug 27, 2013 12:15 am

Problem ID:727
Here is my code: