This is the code for reverse polish notation. When I run it on unix it works fine but codechef says wrong answer. Please help.
#include<stdio.h>
#include<string.h>
void push(char);
void pop();
char stack[400];
unsigned long top=0;
int main()
{
unsigned long len, test_case,i=0,j=0;
char expr[400];
scanf("%u",&test_case);
for(;j<test_case;j++)
{
scanf("%s",expr);
len=strlen(expr);
for(;i<len;i++)
{
if(expr[i]==’+’||expr[i]==’-’||expr[i]==’*’||expr[i]==’/’||expr[i]==’^’)
push(expr[i]);
else if(expr[i]==’)’)
{
pop();
}
else if(expr[i]==’(’)
continue;
else
printf("%c",expr[i]);
}
}
return 0;
}
void pop()
{
if(top==-1)
return;
else
{
printf("%c",stack[top]);
top=top-1;
}
}
void push(char x)
{
if(top==400)
return;
else
{
stack[++top]=x;
}
}
This is solution after correcting your code.
The points you were missing were:
-
You should have initialized “i” by 0 after end of every test case.
-
You should have given a endline after printing every output.
1 Like
Thanks you!