Thread: K&R reverse polish calculator (modified for getline use)

  1. #1
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294

    K&R reverse polish calculator (modified for getline use)

    Hello all:

    Im working on an exercise in the k&r handbook, specifically the reverse polish calculator. Ive created my own version based on the one in the book. Im getting one compiler error saying "error: line 66 'index' redeclared as different type of symbol". Im not quite sure what's causing this error, nor really what the error message even means.

    Im wondering if someone can tell what this error is trying to tell me, and maybe give some suggetions as to solve my problem. Thanks in advance everyone!

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXOP        15    /* maximum size of operator/operand (15 digits seem enough?) */
    #define NUMBER        '0'    /* signal that indicates return of number */
    #define MAXLINE        1024    /* maximum line length */
    #define ERROR        '^'    /* signal that there was an error */
    #define OVERFLOW    '?'    /* signal that user entered input larger than MAXOP */
    
    double pop();
    void push( double );
    int getop( char[] , char[] );
    int gotline( char[] );
    void clearstack();
    
    int main(){
        int len , type;
        double op2;
        char s[ ( MAXLINE + 1 ) ] ,    /* string holding line of calc input, plus one for null */
        s2[ ( MAXOP + 1 ) ];        /* string holding returning number */
    
        while( ( len = gotline( s ) ) > 0 ){
            while( ( type = getop( s , s2 ) ) != EOF ){
                switch( type ){
                    case '+':
                        push( pop() + pop() );
                        break;
                    case '*':
                        push( pop() * pop() );
                        break;
                    case '-':
                        op2 = pop();
                        push( pop() - op2 );
                        break;
                    case '/':
                        if( ( op2 = pop() ) != 0.0 )
                            push( op2 / pop() );
                        else
                            printf( "error: zero divisor\n" );
                        break;
                    case '%':
                        if( ( op2 = pop() ) != 0.0 )
                            push( ( int ) pop() % ( int ) op2 );
                        else
                            printf( "error: zero modulous divisor\n" );
                        break;
                    case '\n':
                        printf( "\t.8g\n" );
                        break;
                    case NUMBER:
                        push( atof( s2 ) );
                        break;
                    case OVERFLOW:
                        printf( "error: command contains too long of operand %s" , s );
                        clearstack();
                    default:
                        printf( "error: not a valid command %s\n" , s );
                        clearstack();
                        break;
                }
            }
        }
    }
    
    static int index = 0;    /* static counter used by gotline() and getop() */
    
    int gotline( char s[] ){
        int c;
        index = 0;    /* reset persistant index used by getop() */
        while( ( s[ 0 ] = c = getchar() ) == ' ' || c == '\t' )
            {    }
        while( index < MAXLINE && ( s[ index++ ] = c = getchar() ) != '\n' && c != EOF )
            {    }
        s[ index ] = '\0';
        return index;
    }
    
    int getop( char s[ ] , char s2[] ){
        int c , j = 0;
        while( ( s2[ j ] = c = s[ index++ ] ) == ' ' || c == '\t' )
            {    }
        if( c == '-' ){
            if( s2[ j++ ] = c = s[ index++ ] == ' ' )
            /* check for subtraction operator */
                return '-';
            if( j < MAXOP && isdigit( s2[ j++ ] = c = s[ index++ ] ) ){
                /* digit found, read in rest of digits.. */
                while( j < MAXOP && isdigit( s2[ j++ ] = c = s[ index++ ] ) )
                    {    }
                if( c == '.' )
                /* read in factorial part */
                    while( j < MAXOP && isdigit( s2[ j++ ] = c = s[ index++ ] ) )
                        {    }
                s2[ j ] = '\0';
                return ( j == MAXOP ) ? OVERFLOW : ( ( c == ' ' ) ? NUMBER : ERROR );
            }
            return ( j == MAXOP ) ? OVERFLOW : ERROR;
            /* minus sign with non number following, error */
        }
        if( isdigit( c ) ){
        /* positive number trap */
            while( j < MAXOP && isdigit( s2[ j++ ] = c = s[ index++ ] ) )
                {    }
            if( c == '.' )
                while( j < MAXOP && isdigit( s2[ j++ ] = c = s[ index++ ] ) )
                    {    }
            s2[ j ] = '\0';
            return ( ( j == MAXOP ) ? OVERFLOW : ( ( c == ' ' ) ? NUMBER : ERROR ) );
        }
        if( j == MAXOP )
            return OVERFLOW;
        if( c == '+' || c == '*' || c == '%' || c == '/' )
            return c;
        return ERROR;
    }
    
    #define    MAXVAL    100    /* max size of op stack */
    
    static int sp = 0;    /* pointer to next free stack position */
    static int val[ MAXVAL ];    /* value stack */
    
    /* push double onto value stack */
    void push( double f ){
        if( sp < MAXVAL )
            val[ sp++ ] = f;
        else
            printf( "error: stack full, can't push %g\n" , f );
    }
    
    /* pop double off top of value stack */
    double pop(){
        if( sp > 0 )
            return val[ --sp ];
        else{
            printf( "error: stack empty\n" );
            return 0.0;
        }
    }
    
    /* clear the stack of all values */
    void clearstack(){
        sp = 0;
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    There is a function declared in string.h named index. It's a function originating in BSD and does the same thing as strchr. You can just rename index to something a little more descriptive so its name does not conflict with the function.

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Thank you christop!

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also you shouldn't have comments after your #define.
    Code:
    #define MAXOP        15    /* maximum size of operator/operand (15 digits seem enough?) */
    /// instead do.
    /* signal that indicates return of number */
    #define NUMBER        '0'
    Jim

  5. #5
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Would that just be for ease of readability? Or does that effect the code in any way? Thanks.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    It doesn't affect the code in any way, unless you're using a broken compiler. Comments are stripped out and replaced with whitespace before or during the processor phase of compilation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K & R Reverse Polish Calculator
    By BIOS in forum C Programming
    Replies: 7
    Last Post: 09-29-2011, 02:21 PM
  2. reverse polish calculator
    By Tool in forum C Programming
    Replies: 3
    Last Post: 12-30-2009, 04:46 PM
  3. reverse polish calculator
    By Tool in forum C Programming
    Replies: 1
    Last Post: 12-24-2009, 05:25 PM
  4. Reverse Polish Calculator
    By BlasterStar in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2003, 11:12 PM
  5. c++ Reverse Polish Calculator Help
    By knight101 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2001, 09:31 AM