Infix to postfix to Evaluate

Discussion in 'C' started by captainpirate, Mar 18, 2010.

  1. captainpirate

    captainpirate New Member

    Joined:
    Mar 18, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    This program should first convert an infix expression to postfix expression then evaluate.

    There is no problem in converting infix expression to postfix but the problem is in the evaluation part, here is the code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAX 50
    
    char stack[MAX] ;
    char infix[MAX] ;
    char postfix[MAX] ;
    char eval[MAX] ;
    char *s, *t ; /*pointers to input and output strings*/
    int top; /*Stack top*/
    
    /*Function Prototypes*/
    void Initialize (void);
    void SetExpression (char *);
    char Pop (void );
    void Push (char);
    int priority (char);
    void Convert (void);
    int Evaluate(void);
    
    void main( )
    {
    int m;
    
    clrscr( ) ;
    Initialize ( ) ;
    printf ( "\nEnter an infix expression: " ) ;
    gets ( infix ) ;
    SetExpression (infix) ;
    Convert( ) ;
    printf ( "\nThe Postfix expression is: " ) ;
    puts(postfix);
    strcpy(eval,postfix);
    m=Evaluate( );
    printf("answer: %d", m );
    
    getch( ) ;
    }
    
    void Initialize (void)                                       
    {
    top = -1 ;/*Make stack empty*/
    }
    
    void SetExpression ( char *str )
    {
    s = str ;
    t = postfix;
    }
    
    /* adds operator to the stack */
    void Push ( char c )
    {
    if ( top == MAX - 1 )
    printf ( "\nStack is full.\n" ) ;
    else
    {
    top++ ;
    stack[top] = c ;
    }
    }
    
    /* pops an operator from the stack */
    char Pop ( void )
    {
    if ( top == -1 ) /* Stack is empty*/
    return -1 ;
    else
    {
    char item = stack[top] ;
    top-- ;
    return item ;
    }
    }
    
    int priority(char c)
    {
    if ( c == '*' || c == '/' || c == '%' ) 
    return 2;
    else if ( c == '+' || c == '-' )
    return 1;
    else
    return 0;
    }
    
    /* converts the infix expr. to postfix form */
    void Convert (void)
    {
    char x ;
    
    while ( *( s ) )
    { /*Skip white spaces, if any*/
    if ( *( s ) == ' ' || *( s ) == '\t' )
    {
    s++ ;
    continue ;
    }
    
    if ( isdigit ( *( s ) ) )/*Operands*/
    {
    while ( isdigit ( *( s ) ) )
    {
    *( t ) = *( s ) ;
    s++ ;
    t++ ;
    }
    }
    if ( *( s ) == '(' )/*Opening Parenthesis*/
    {
    Push ( *( s ) ) ;
    s++ ;
    }
    if ( *( s ) == '*' || *( s ) == '+' || *( s ) == '/' || *( s ) == '%' || *( s ) == '-' ) /*operators*/
    {
    if ( top != -1 )
    {
    x = Pop ( ) ;
    while ( priority ( x ) >= priority ( *( s ) ) )
    {
    *( t ) = x ;
    t++ ;
    x = Pop ( ) ;
    }
    Push( x ) ;
    Push ( *( s ) ) ;
    }
    else Push( *( s ) ) ;
    s++ ;
    }
    if ( *( s ) == ')' )/*Closing Parenthesis*/
    {
    x = Pop ( ) ;
    while ( x != '(' )
    {
    *( t ) = x ;
    t++ ;
    x = Pop ( ) ;
    }
    s++ ;
    }
    }
    while ( top != -1 )/*While stack is not empty*/
    {
    x = Pop ( ) ;
    *( t ) = x ;
    t++ ;
    }
    t++ ;
    }
    
    int Evaluate(void)
    {
    int i,l,a,b,q,z;
    l=strlen(eval);
    for(i=0;i<l;i++)
    {
    if ( isdigit ( eval[i] ) )
    {
    Push(eval[i]);
    }
    else if ( eval[i] == '*' || eval[i] == '+' || eval[i] == '/' || eval[i] == '%' || eval[i] == '-' )
    {
    a = Pop ( );
    b = Pop ( );
    
    switch( eval[i] )
    {
    case '+' : q=b+a; break;
    case '-' : q=b-a; break;
    case '*' : q=b*a; break;
    case '/' : q=b/a; break;
    }
    Push ( q );
    }
    }
    z = Pop ( );
    return z;
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    What is the problem with the code, i.e. what is it doing that you didn't expect, or what isn't it doing that you did expect?

    What line does it all start going wrong? I don't see any debugging statements added to the code; how are you finding problems in the code?
     

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