Home Forums C Programming plzzz help…….it doesn’t evaluate right……!

Viewing 3 reply threads
  • Author
    Posts
    • #2244
      WeldonGarrison
      Participant
      #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 != '')
      {
      /* 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 = '';
      }
      
      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;
      }
      • This topic was modified 1 year, 10 months ago by M. Saqib.
      • This topic was modified 1 year, 10 months ago by M. Saqib.

    • #3642
      GWILouisaxwzkla
      Participant

      Heres a postfix to infix program I have on my drive ( hope this helps….. ):

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

      enum Boolean { False = 0 , True = 1 } ;
      const int GREATER = 1 ;
      const int NOTGREATER = 0 ;
      int cnt = 0 , cntcol = 0 , postcnt = 0 ;

      //
      struct Stack
      {
      int info ;
      Stack * previous;
      } * top;

      //
      void Push ( int number )
      {
      Stack * current;
      current = new ( Stack ) ;
      if ( current == 0 )
      {
      printf ( "n allocation error!!!!! " );
      return;
      }
      current -> info = number ;
      current -> previous = top ;
      top = current ;
      }

      //
      int Pop ()
      {
      int temp ;
      Stack * current = top;
      temp = current -> info ;
      top = top -> previous ;
      delete current ;
      return temp ;
      }

      //
      int StackTop ()
      {
      return top -> info ;
      }

      //
      Boolean isEmpty()
      {
      if ( top == NULL )
      return True;
      else
      return False ;
      }

      //
      Boolean isOperand ( char ch )
      {
      if ( ch >= '0' && ch <= '9' ) //if is a number
      return True;
      else
      return False ;
      }

      //
      void Infix ( char infix[][10])
      {
      char c ;
      while ( c != 13 )
      {
      c = getch() ;
      putch(c);
      if ( c >= '0' && c <= '9' )
      {
      infix [cnt][cntcol] = c ;
      cntcol++ ;
      }
      else if ( c == '+'|| c == '-'|| c == '*'|| c == '/')
      {
      infix [cnt][cntcol] = '' ;
      cnt++ ;
      infix [cnt][0] = c ;
      infix [cnt][1] = '' ;
      cnt++ ;
      cntcol = 0 ;
      }
      else if ( c == 13 )
      infix[cnt][cntcol] = '' ;
      }
      }


      //
      int Calculate (int op1, char opr, int op2)
      {
      int ans ;
      switch ( opr )
      {
      case '+' : ans = op1 + op2 ; break ;
      case '-' : ans = op1 - op2 ; break ;
      case '*' : ans = op1 * op2 ; break ;
      case '/' : ans = op1 / op2 ; break ;
      case '^' : ans = pow(op1, op2) ;
      }
      return ans ;
      }
      //
      int Evaluate ( char postfix[][10] )
      {
      for ( int i = 0 ; i <= cnt ; i ++ )
      {
      char ch = postfix[0] ;
      char ch1[10] ;
      strcpy ( ch1 ,postfix
      ) ;
      // cout < if ( isOperand ( ch ) == True )
      Push ( atoi(ch1) );
      else
      {
      int op2 = Pop () ;
      int op1 = Pop () ;
      int ans = Calculate ( op1, ch, op2 ) ;
      // cout << ans << endl ;
      Push ( ans ) ;
      }
      }
      int ans = Pop () ;
      return ans ;
      }

      //

      int CheckP (char cur, char st)
      {
      int a = NOTGREATER ;

      if ( cur == '(' )
      a = GREATER ;
      else if ( cur != '(' && st == '(' )
      a = GREATER ;
      else if ( cur != '(' && st != '(' )
      {
      if ( ( cur == '*' || cur == '/' ) && ( st == '+' || st == '-' ) )
      a = GREATER ;
      }
      return a ;

      }


      //
      void infixToPostfix ( char infix[][10], char postfix[][10] )
      {
      int postcol = 0 ;
      for ( int i = 0 ; i <= cnt ; i ++ )
      {
      // strcpy(postfix, infix ) ;
      // cout << postfix
      ;
      char ch;
      ch = infix
      [0] ;
      if ( isOperand ( ch ) == True )
      strcpy ( postfix [postcnt++] , infix
      ) ;
      else
      {
      if ( isEmpty() == True )
      Push ( int(ch) ) ;
      else
      {
      while ( CheckP( ch, StackTop() ) != GREATER && isEmpty() != True )
      {
      char c ;
      if ( ch == ')' )
      {
      do
      {
      c = char ( Pop() ) ;
      if ( c != '(')
      {
      postfix [postcnt][postcol] = c ;
      postfix [postcnt][postcol+1] = '' ;
      postcnt++ ;
      }
      }while ( c != '(' && isEmpty() != True);
      break ;
      }
      else
      c = char ( Pop() ) ;
      if ( c != '(' && c != ')' )
      {
      postfix [postcnt][postcol] = c ;
      postfix [postcnt][postcol+1] = '' ;
      postcnt++ ;
      }
      }
      if ( ch != ')' )
      Push ( int(ch) ) ;
      }
      }
      }

      while ( isEmpty () != True )
      {
      char c = char ( Pop () ) ;
      postfix [postcnt][postcol] = c ;
      postfix [postcnt][postcol+1] = '';
      postcnt++ ;
      }
      // postfix[postcnt][postcol] = '';
      }

      //

      void Postfix ( char postfix[][10] )
      {
      char ch[10] ;
      for ( int i = 0; i <= postcnt; i++ )
      {
      strcpy(ch , postfix ) ;
      printf ( "%c" ,ch );
      }
      }

      //

      void main()
      {
      //clrscr();
      top = NULL ;

      char infix [ 50 ][ 10 ] ;
      char postfix [ 50 ][ 10 ] ;

      //gotoxy ( 5, 8 ) ;
      printf ( "Enter Arithematic Expression ...................... : " );
      Infix ( infix ) ;

      infixToPostfix ( infix, postfix ) ;
      //gotoxy ( 5, 10 ) ;
      printf ( "Arithematic Expression in Postfix Notation......... : " );
      Postfix ( postfix ) ;
      int ans = Evaluate ( postfix ) ;
      //gotoxy (5, 11 ) ;
      printf ( "The Answer of Expression .......................... : " );
      printf ( "%i", ans );

      getch();
      }
      //
    • #3643
      PSBMaira490593
      Participant

      Hi,
      please run it again in new window and check the variable that you have declared are right or not.
      thanks.



      Debra Fine

    • #8960
      M. Saqib
      Keymaster

      OK

Viewing 3 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.