 # TRANSFORM THE EXPRESSION

#1

http://www.codechef.com/viewsolution/6585917

PROBLEM-----> http://www.codechef.com/problems/ONP/

this solution is showing WA in codechef, runtime error in ideone and working perfectly in code::blocks

The test cases which are working fine in code::blocks are showing runtime erroe at ideone.
WHY???

I have commented my algorithm for better understanding

#2

The problem is in your method for fast string input i.e., str_input, modify it for newline charater.

The newline character is the reason for such weird behavior.

`t=input();` with `str_input(s);` is giving you runtime error because during reading the integer t, the new line (’
') is read and skipped. So in next str_input() method while (c!=32 && c!=10), the check for c != 10 is skipping the next (second ) string input.

If the input was
``` 3 (a+(b*c))```

((a+b)(z+x))
((a+t)
((b+(a+c))^(c+d)))

then the runtime error would not occur ( see the extra line between first and second string ), see it on ideone here.

See your same code ACcepted here with out using the " str_input " method and rest of the code remains the same.

#3

HEY, I AM GETTING WRONG ANSWER CAN ANYONE HELP…

#include
using namespace std;
main()
{
int t,j=0,top=-1;
char a,stack,b;
cin>>t;
while(t–)
{
cin>>a;

``````	for(int i=0;a*!='\0';i++)
{
if( (a*=='(')||(a*=='+')||(a*=='-')||(a*=='*')||(a*=='/')||(a*=='^') )
{
top++;
stack[top]=a*;

}
else if(a*==')')
{
b[j]=stack[top];
j++;
top--;
while(stack[top]!='(')
{
b[j]=stack[top];
j++;
}
top--;

}
else
{
b[j]=a*;
j++;

}
}

cout<<b<<endl;
j=0,top=-1;
}
``````

}

#4

hi thnx for pointing it out… i have optimized my str_input() function to check for leading spaces or new lines before string input but what i found the problem was actually with me, not terminating the string “exp” with ‘\0’
which was causing the runtime error.