Plz help!!! Dunno wats wrong!!

Discussion in 'C' started by vIp3rS, Mar 9, 2010.

  1. vIp3rS

    vIp3rS New Member

    Joined:
    Mar 9, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define MAX 50
    #define EMPTY -1
    
    struct stack
    {
    		  int data[MAX];
    		  int top;
    };
    
    
    int isempty(struct stack *s)
    {
    		  return (s->top == EMPTY) ? 1 : 0;
    }
    
    void emptystack(struct stack* s)
    {
    		  s->top = EMPTY;
    }
    
    void push(struct stack* s,int item)
    {
    		  if(s->top == (MAX-1))
    		  {
    					 printf("\nSTACK FULL");
    		  }
    		  else
    		  {
    					 ++s->top;
    					 s->data[s->top]=item;
    		  }
    }
    
    int pop(struct stack* s)
    {
    		  int ret=EMPTY;
    		  if(s->top == EMPTY)
    					 printf("\nSTACK EMPTY");
    		  else
    		  {
    					 ret= s->data[s->top];
    					 --s->top;
    		  }
    		  return ret;
    }
    
    void display(struct stack s)
    {
    		  while(s.top != EMPTY)
    		  {
    					 printf("\n%d",s.data[s.top]);
    					 s.top--;
    		  }
    }
    
    
    int isoperator(char e)
    {
    		  if(e == '+' || e == '-' || e == '*' || e == '/' || e == '%')
    					 return 1;
    		  else
    					 return 0;
    }
    
    
    int priority(char e)
    {
    		  int pri = 0;
    
    		  if(e == '*' || e == '/' || e =='%')
    					 pri = 2;
    		  else
    		  {
    					 if(e == '+' || e == '-')
    								pri = 1;
    		  }
    		  return pri;
    }
    
    int evaluate(char *postfix)
    {
    		  char *p;
    		  struct stack stk;
    		  int op1,op2,result;
    
    		  emptystack(&stk);
    		  p = &postfix[0];
    
    		  while(*p != '\0')
    		  {
    			  /* removes tabs and spaces */
    					 while(*p == ' ' || *p == '\t')
    					 {
    								p++;
    					 }
    			 /* if is digit */
    					 if(isdigit(*p))
    					 {
    								push(&stk,*p - 48);
    					 }
    					 else
    					 {
    						 /* it is an operator */
    								op1 = pop(&stk);
    								op2 = pop(&stk);
    
    								switch(*p)
    								{
    										  case '+':
    													 result = op2 + op1;
    													 break;
    
    										  case '-':
    													 result = op2 - op1;
    													 break;
    
    										  case '/':
    													 result = op2 / op1;
    													 break;
    
    										  case '*':
    													 result = op2 * op1;
    													 break;
    
    										  case '%':
    													 result = op2 % op1;
    													 break;
    
    										  default:
    													 printf("\nInvalid Operator");
    													 return 0;
    								}
    								push(&stk,result);
    					 }
    					 p++;
    		  }
    		  result = pop(&stk);
    		  return result;
    }
    
    
    void infix2postfix(char* infix, char * postfix, int insertspace)
    {
    		  char *i,*p;
    		  struct stack X;
    		  char n1;
    		  emptystack(&X);
    		  i = &infix[0];
    		  p = &postfix[0];
    
    		  while(*i)
    		  {
    					 while(*i == ' ' || *i == '\t')
    					 {
    								i++;
    					 }
    
    					 if( isdigit(*i) || isalpha(*i) )
    					 {
    								while( isdigit(*i) || isalpha(*i))
    								{
    										  *p = *i;
    										  p++;
    										  i++;
    								}
    								/*SPACE CODE*/
    								if(insertspace)
    								{
    										  *p = ' ';
    										  p++;
    								}
    								/*END SPACE CODE*/
    					 }
    
    					 if( *i == '(' )
    					 {
    								push(&X,*i);
    								i++;
    					 }
    
    					 if( *i == ')')
    					 {
    								n1 = pop(&X);
    								while( n1 != '(' )
    								{
    										  *p = n1;
    										  p++;
    										  /*SPACE CODE*/
    										  if(insertspace)
    										  {
    													 *p = ' ';
    													 p++;
    										  }
    										  /*END SPACE CODE*/
    										  n1 = pop(&X);
    								}
    								i++;
    					 }
    
    					 if( isoperator(*i) )
    					 {
    								if(isempty(&X))
    										  push(&X,*i);
    								else
    								{
    										  n1 = pop(&X);
    										  while(priority(n1) >= priority(*i))
    										  {
    													 *p = n1;
    													 p++;
    													 /*SPACE CODE*/
    													 if(insertspace)
    													 {
    																*p = ' ';
    																p++;
    													 }
    													 /*END SPACE CODE*/
    													 n1 = pop(&X);
    										  }
    										  push(&X,n1);
    										  push(&X,*i);
    								}
    								i++;
    					 }
    		  }
    		  while(!isempty(&X))
    		  {
    					 n1 = pop(&X);
    					 *p = n1;
    					 p++;
    					 /*SPACE CODE*/
    					 if(insertspace)
    					 {
    								*p = ' ';
    								p++;
    					 }
    					 /*END SPACE CODE*/
    		  }
    		  *p = '\0';
    }
    
    int main(void)
    {
    	char infix[MAX], postfix[MAX], result[MAX], choice;
    	do
    	{
    		printf("Enter expression: ");
    		fflush(stdin);
    		gets(infix);
    		infix2postfix(&infix[0],&postfix[0],1);
    		result[0]=postfix[0];
    		printf("\nPostfix expression is: %s",&result[0]);
    		printf("\n %s = %d.",&infix[0],evaluate(&result[0]));
    		printf("\n\nDo you want to continue?<Y/N>");
    		scanf("%c",&choice);
    	}while(choice=='y' || choice=='Y');
    		return 0;
    }
    
     
  2. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    What is it supposed to do? What is it doing instead?
     
    shabbir likes this.
  3. vIp3rS

    vIp3rS New Member

    Joined:
    Mar 9, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    this program would convert infix expression to postfix then evaluate......but when i ran this program the answer would always be zero......
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice