infix to pre/postfix

get noticed on how to put parenthesis in the program

Page 1 of 1

3 Replies - 2796 Views - Last Post: 08 August 2010 - 04:44 AM Rate Topic: -----

#1 shunix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-July 10

infix to pre/postfix

Posted 05 August 2010 - 08:16 PM

guys, im halfway done but... i really dont know how to include the parenthesis and the power to my program.. need your help got to submit this next week please... :)

#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 exp<b></b>ression : ");
	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])) 
			{
				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 Exp<b></b>ression : %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]);
}

This post has been edited by JackOfAllTrades: 06 August 2010 - 04:54 AM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: infix to pre/postfix

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: infix to pre/postfix

Posted 05 August 2010 - 09:09 PM

Please use code tags so we can see the indentation. It's hard to read unindented gibberish.

:code:

As for the parenthesis and power, are you having a specific problem or are you just having trouble implementing that entire portion.

Some reference you may want to look over if you haven't already:
http://en.wikipedia....-yard_algorithm
Was This Post Helpful? 0
  • +
  • -

#3 shunix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-July 10

Re: infix to pre/postfix

Posted 08 August 2010 - 01:56 AM

View Posteker676, on 05 August 2010 - 08:09 PM, said:

Please use code tags so we can see the indentation. It's hard to read unindented gibberish.

:code:

As for the parenthesis and power, are you having a specific problem or are you just having trouble implementing that entire portion.

Some reference you may want to look over if you haven't already:
http://en.wikipedia....-yard_algorithm


probably, im having troubles in implementing the entire portion... huhuhu.. need to pass it by tomorow...
Was This Post Helpful? 0
  • +
  • -

#4 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 pre/postfix

Posted 08 August 2010 - 04:44 AM

This tutorial on Converting and Evaluating Infix, Postfix and Prefix Expressions in C on dream.in.code will help you include support for paranthesis.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1