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;
}