hello sir...!! it is giving me runtime sigsegv error can u plzzz tell me why??

#include<stdio.h>

//#include<conio.h>

#include<string.h>

#include<stdlib.h>

const int size=400;

char infix[400],postfix[400],stack[400];

int top=-1;

int precedence(char ch);

char pop();

char topelement();

void push(char ch);

int braces(char*);

int main()

{char ele,elem,t;

int i,prep,pre,popped,j=0,chk=0;

strcpy(postfix," ");

//clrscr();

//printf("enter the number of testcases\n");

scanf("%d",&t);

while(t>0)

{ // printf("%d\n",t);

j=0;

//printf("ASSUMPTION:the infix expression contains single letter variables\n\t and single digit constants only.\n\n");

//printf("enter infix expression...\n");

scanf("%s",&infix);

/*chk=braces(infix);

if(chk!=0)

{printf("unbalanced no. of braces \n extra");

printf((chk==1?"right braces":"left braces"));

getch();

exit(1);

} */

for(i=0;infix[i]!='\0';i++)

{if(infix[i]!='('&&infix[i]!=')'&&infix[i]!='^'&&infix[i]!='*'&&infix[i]!='/'&&infix[i]!='+'&&

infix[i]!='-')

postfix[j++]=infix[i];

else if(infix[i]=='(')

{elem=infix[i];

push(elem);

}

else if(infix[i]==')')

{while((popped=pop())!='(')

{postfix[j++]=popped;

}

}



else

{elem=infix[i];

pre=precedence(elem);

ele=topelement();

prep=precedence(ele);

if(pre>prep)

push(elem);

else{while(prep>=pre)

{if(ele=='#')

break;

popped=pop();

ele=topelement();

postfix[j++]=popped;

prep=precedence(ele);

}

push(elem);

}

}
}

while((popped=pop())!='#')

postfix[j++]=popped;

postfix[j]='\0';

//printf("\npostfix:");

printf("%s\n",postfix);

t--;

}

//getch();

return 0;

}
int precedence(char ch)

{switch(ch)

{case'^':return 5;

case'/':return 4;

case'*':return 4;

case'+':return 3;

case'-':return 3;

default:return 0;

}

}

char pop()

{char ret;

if(top!=-1)

{ret=stack[top];

top--;

return ret;

}

else return'#';

}

char topelement()

{char ch;

if(top!=-1)ch=stack[top];

else ch='#';

return ch;

}

void push(char ch)

{if(top!=size-1)

{top++;

stack[top]=ch;

}

}

/*int braces(char *s)

{int i,leftbr,rightbr;

leftbr=rightbr=0;

for(i=0;s[i];i++)

{if(s[i]=='(') leftbr++;

else if(s[i]==')')      rightbr++;

}if(leftbr==rightbr) return 0;

else if(leftbr<rightbr) return 1;

else return -1;

} */

One thing is, your scanf should be scanf("%s", infix); not scanf("%s",&infix);

You need not use ā€˜&ā€™ when reading strings using scanf. If you do, you will get errors or wrong answers.

sirā€¦still m getting the same problemā€¦:frowning:

i am not a ā€œsirā€ :slight_smile:

2 Likes

Where are you getting SIGSEGV? Can you post the link to the problem and/or the link to the solution that is throwing SIGSEGV?

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

helpā€¦any 1 cn crrct my code???

Hi.

That was a silly mistake that caused your program to throw a runtime error.

The data-type of your variable t is char. Simply changing it to int will make the program AC (http://www.codechef.com/viewsolution/2398880)

Regards.

Happy Coding!

OMGā€¦!! thnx a lotā€¦:smiley:

You are welcome!