4 Replies - 38550 Views - Last Post: 21 March 2010 - 11:41 AM Rate Topic: -----

#4 6gt  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 2
  • Joined: 11-July 08

Re: Infix to Postfix Conversion & Evaluation in C

Posted 12 July 2008 - 12:07 AM

Hi, I've to Submit a C program that Converts INFIX to POSTFIX and its EVALUATION
Im here after a lot of searches that gives results, but not what exactly i want.

My problem is very simple, that the code is not Converting properly in case of same precedence operators.

2+3-4 = 23+4-
but the output is 234-+
that is, precedence is calculated from Right to left in cases of same precedences
Mixed precedences and the evaluation part works perfectly.

This is my code:

#include<stdio.h>
#include<string.h>
#include<math.h>

#define oper(x) (x=='+' || x=='-' || x=='*' || x=='/')

char in[30], post[30], stack[30];
int top=-1;

void push(char x)
{
	stack[++top]=x;
}

char pop()
{
	return stack[top--];
}

int precedence(char c)
{
	if (c=='+' || c=='-')
		return 1;
	if (c=='*' || c=='/')
		return 2;
	if (c=='(')
		return 3;
}

main()
{
	char c;
	int l,i,j=0,st1[20],k,h,f,eval,s,N;
	printf("Enter the infix expression : ");
	scanf("%s",&in);
	l=strlen(in);
	for(i=0;i<=l;i++)
	{	
		if(oper(in[i]))
		{	
			post[j++]=' ';
			while(precedence(in[i])<precedence(stack[top])) //any problem here?
			{
				post[j++]=stack[top];
				pop();
				post[j++]=' ';

			}
			push(in[i]);
		}
		else if(in[i]=='\0')
		{
			while(top!=-1)
			{
				post[j++]=' ';
				post[j++]=stack[top];
				pop();
			}
		}
		else
			post[j++]=in[i];
	}
	post[j]='\0';
	printf("Postfix Expression : %s\n",post);
	i=0;top=-1;f=0;k=0;
	while(i<j)
	{
		if(oper(post[i]))
		{	
			f=1;
			c=post[i];
			eval=0;
			switch(c)
			{
			case '+':
				eval=st1[top-1]+st1[top];
				break;		
			case '-':
				eval=st1[top-1]-st1[top];
				break;
			case '*':
				eval=st1[top-1]*st1[top];
				break;
			case '/':
				eval=st1[top-1]/st1[top];
				break;
			}
			top--;
			st1[top]=eval;	
		}
		else if(post[i]==' ')
		{
			if(f==0)
			{
				h=i-k;
				s=0;
				while(post[h]!=' ')
				{
					N=(int)post[h];
					N=N-48;
					s=s+N*(pow(10,(k-1)));
					k--;
					h++;
				}
				st1[++top]=s;
			}
			k=0;
		}
		else 
		{
			k++;
			f=0;
		}
		i++;	
	}
	printf("Value : %d\n",st1[top]);
}



Could you Please help by posting necessary changes, I've got only limited time
I'll be Veeeery thankful if you help me to get over this bug
I joined only today and this is my first post, so i'm sincerely sorry for any errors from my part

Thanks
Was This Post Helpful? 1

#6 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Infix to Postfix Conversion & Evaluation in C

Posted 12 July 2008 - 05:09 AM

Refer to this tutorial for your answer.
Was This Post Helpful? 1

#7 6gt  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 2
  • Joined: 11-July 08

Re: Infix to Postfix Conversion & Evaluation in C

Posted 14 July 2008 - 07:12 AM

View Postborn2c0de, on 12 Jul, 2008 - 05:09 AM, said:

Refer to this tutorial for your answer.


Thanks...
The problem was using > instead of >=.
after correction it worked in TURBOC but not in my linux gcc compiler.
Showing segmentation fault, underflow error etc.
Anyway got another code
Thanks for helping
Was This Post Helpful? 0
  • +
  • -

#8 Guest_neha*


Reputation:

Re: Infix to Postfix Conversion & Evaluation in C

Posted 21 March 2010 - 11:37 AM

View Post6gt, on 11 July 2008 - 11:07 PM, said:

Hi, I've to Submit a C program that Converts INFIX to POSTFIX and its EVALUATION
Im here after a lot of searches that gives results, but not what exactly i want.

My problem is very simple, that the code is not Converting properly in case of same precedence operators.

2+3-4 = 23+4-
but the output is 234-+
that is, precedence is calculated from Right to left in cases of same precedences
Mixed precedences and the evaluation part works perfectly.

This is my code:

#include<stdio.h>
#include<string.h>
#include<math.h>

#define oper(x) (x=='+' || x=='-' || x=='*' || x=='/')

char in[30], post[30], stack[30];
int top=-1;

void push(char x)
{
	stack[++top]=x;
}

char pop()
{
	return stack[top--];
}

int precedence(char c)
{
	if (c=='+' || c=='-')
		return 1;
	if (c=='*' || c=='/')
		return 2;
	if (c=='(')
		return 3;
}

main()
{
	char c;
	int l,i,j=0,st1[20],k,h,f,eval,s,N;
	printf("Enter the infix expression : ");
	scanf("%s",&in);
	l=strlen(in);
	for(i=0;i<=l;i++)
	{	
		if(oper(in[i]))
		{	
			post[j++]=' ';
			while(precedence(in[i])<precedence(stack[top])) //any problem here?
			{
				post[j++]=stack[top];
				pop();
				post[j++]=' ';

			}
			push(in[i]);
		}
		else if(in[i]=='\0')
		{
			while(top!=-1)
			{
				post[j++]=' ';
				post[j++]=stack[top];
				pop();
			}
		}
		else
			post[j++]=in[i];
	}
	post[j]='\0';
	printf("Postfix Expression : %s\n",post);
	i=0;top=-1;f=0;k=0;
	while(i<j)
	{
		if(oper(post[i]))
		{	
			f=1;
			c=post[i];
			eval=0;
			switch(c)
			{
			case '+':
				eval=st1[top-1]+st1[top];
				break;		
			case '-':
				eval=st1[top-1]-st1[top];
				break;
			case '*':
				eval=st1[top-1]*st1[top];
				break;
			case '/':
				eval=st1[top-1]/st1[top];
				break;
			}
			top--;
			st1[top]=eval;	
		}
		else if(post[i]==' ')
		{
			if(f==0)
			{
				h=i-k;
				s=0;
				while(post[h]!=' ')
				{
					N=(int)post[h];
					N=N-48;
					s=s+N*(pow(10,(k-1)));
					k--;
					h++;
				}
				st1[++top]=s;
			}
			k=0;
		}
		else 
		{
			k++;
			f=0;
		}
		i++;	
	}
	printf("Value : %d\n",st1[top]);
}



Could you Please help by posting necessary changes, I've got only limited time
I'll be Veeeery thankful if you help me to get over this bug
I joined only today and this is my first post, so i'm sincerely sorry for any errors from my part

Thanks

Was This Post Helpful? 0

#9 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Infix to Postfix Conversion & Evaluation in C

Posted 21 March 2010 - 11:41 AM

WHY did you revive a 2 years + old thread?? If you need help, please open a new post. Stop hijaking ...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1