1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C Program to Convert Infix to Postfix

Discussion in 'C' started by tamsy, Aug 18, 2010.

  1. tamsy

    tamsy New Member

    Joined:
    Aug 18, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    hey people please please please help me out with this. i have got my submission tommorrow and i have really worked hard on this i dont want it to turn out bad. this is a c program to convert infix expression to postfix without considering parentheses and already accepting that the given expression is a valid infix expression. what happens is the out is comin really weird.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int prece(char j);
    void main()
    {
    	char infix[10],stack[5],output[10],j;
    	int k=0,l=0,i,a,b;
    	stack[k]=0;
    	printf("write the infix expression to be converted to postfix");
    	scanf("%s",infix);
    	for(i=0;i<10;i++)
    	{
    		a=prece(infix[i]);
    		b=prece(stack[k]);
    		if(a<=b)
    		{
    			output[l]=infix[i];
    			l++;
    		}
    		else
    		{
    			stack[k]=infix[i];
    			k++;
    
    		}
    	}
    	for(i=k;i>0;i--)
    	{
    		output[l]=stack[i];
    		l++;
    	}
    	printf("%s",output);
    	getch();
    }
    int prece(char j)
    {
    	if(j=='*'||j=='/')
    	{
    	   j=3;
    	}
    	else
    	{
    	    if(j=='+'||j=='-')
    	    {
    	       j=2;
    	    }
    	    else
    	    {
    	       j=1;
    	    }
    	}
    	return j;
    }
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
  3. tamsy

    tamsy New Member

    Joined:
    Aug 18, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Re: c program to convert infix expression to postfix without considering parentheses

    hey thanks a lot !
     
  4. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
    My Pleasure.
     

Share This Page