PROBLEM W/ EVALUATE....plz help me!

This is a discussion on PROBLEM W/ EVALUATE....plz help me! within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <ctype.h> #define MAX 50 #define EMPTY -1 struct stack { int data[MAX]; int top; }; int ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    PROBLEM W/ EVALUATE....plz help me!

    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 */
    								op2 = pop(&stk);
    								op1 = pop(&stk);
    
    								switch(*p)
    								{
    										  case '+':
    													 result = op1 + op2;
    													 break;
    
    										  case '-':
    													 result = op1 - op2;
    													 break;
    
    										  case '/':
    													 result = op1 / op2;
    													 break;
    
    										  case '*':
    													 result = op1 * op2;
    													 break;
    
    										  case '%':
    													 result = op1 % op2;
    													 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];
    	printf("Enter expression: ");
    	fflush(stdin);
    	gets(infix);
    	infix2postfix(&infix[0],&postfix[0],1);
    	printf("\nPostfix expression is: %s",&postfix[0]);
    	result[0]=postfix[0];
    	printf("\n %s = %d.",&infix[0],evaluate(&result[0]));
    	return 0;
    }
    Last edited by vIp3rS; 03-03-2010 at 01:03 PM.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    I don't know what went wrong......the expression will give only 0 as an answer.....

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    I don't know what went wrong, how did you get past the code checking so that you managed to post an unreadable mess.

    READ THIS FIRST!
    << !! Posting Code? Read this First !! >>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    sorry......i was in a hurry....^^

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: PROBLEM W/ EVALUATE....plz help me!

    I changed added two conditions in your code its working fine.

    1. In the infix2postfix function while checking the operator pop function returns -1. so it adds that is also in stack. so after checking you can push that into the stack.

    Code:
     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);
                                    }
                                    if(n1!=-1)
                                    push(&X,n1);
                                    push(&X,*i);
                            }
                            i++;
                    }
    2. Then in the evaluate function after removing the spaces you need to check whether it is reached null or not.

    Code:
          while(*p != '\0')
            {
                    /* removes tabs and spaces */
                    while(*p == ' ' || *p == '\t' )
                    {
                            p++;
                    }
                    if(*p == '\0')
                            break;
    
    After changing these conditions try to execute it will work fine.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    the answer to the expression is still 0......i think the problem lies with the return statement at the end of the switch in the function evaluate.....but i do not know what to do with it.......
    switch(*p)
    {
    case '+':
    result = op1 + op2;
    break;

    case '-':
    result = op1 - op2;
    break;

    case '/':
    result = op1 / op2;
    break;

    case '*':
    result = op1 * op2;
    break;

    case '%':
    result = op1 % op2;
    break;

    default:
    printf("\nInvalid Operator");
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. plz help me with run problem
    By onlinegeek in forum C++ Programming
    Replies: 9
    Last Post: 12-14-2005, 11:46 PM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. compiling problem! plz help!
    By aaroroge in forum C++ Programming
    Replies: 9
    Last Post: 10-24-2005, 10:57 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 02:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21