eg:exp 2 3 4 + * will give output 14,where exp is the program name
i have made 3 files for that.
first file named k&r5-10a.c is as follows:
#include<stdio.h>
#include<stdlib.h>/*for atof*/
#define MAXOP 100
#define NUMBER '0'
int getop(char [],int *point_argc,char **(*loc_argv));//location of argv
void push(double);
double pop(void);
/*reverse polish calculator*/
int main(int argc,char *argv[])
{
int type;
double op2;
char s[MAXOP];
--argc;//first argument is name
while(argc>=0)
{
type=getop(s,&argc,&argv);
switch(type)
{
case NUMBER:
push(atof(s));
break;
case '+':
push(pop()+pop());
break;
case '*':
push(pop()*pop());
break;
case '-':
op2=pop();
push(pop()-op2);
break;
case '/':
op2=pop();
if(op2!=0.0)
push(pop()/op2);
else
printf("error:zero divisor\n");
break;
/*case '\n':
printf("\t%.8g\n",pop());
break;not needed*/
default:
printf("error:unknown command %s\n",s);
break;
}
}
return 0;
}
second file named k&r5-10b.c is as follows:
#include<stdio.h>
#define MAXVAL 100 /*max depth of val stack*/
int sp=0;/*next free stack position*/
double val[MAXVAL];/*value stack*/
/*push:push f onto value stack*/
void push(double f)
{
if(sp<MAXVAL)
val[sp++]=f;
else
printf("error:stack full,cant push %g\n",f);
}
/*pop:pop and return top value from stack*/
double pop(void)
{
if(sp>0)
return val[--sp];
else
{
printf("error:stack empty\n");
return 0.0;
}
}
third file named k&r5-10c.c is as follows:
#include<ctype.h>//replace argv with correspoding
int getch(void);
void ungetch(int);
/*getop:get next operator or numeric operand*/
int getop(char s[],int *point_argc,char **(*loc_argv))//loc_argv is memory address where argv is stored passed from main
{//point_argc is memory address of argc passed from main
int i,c,j=0;
++(*loc_argv);
/*while((s[0]=c=getch())==' '|| c=='\t')
;
s[1]='\0';no need*/
s[0]=c=*(*loc_argv)[0];//reading first character
if(!isdigit(c) && c!= '.'){
--*point_argc;
++(*loc_argv);
s[1]='\0';
return c;/*not a number*/
}
i=0;
if(isdigit(c))/*collect integer part*/
while(isdigit(s[++i]=c= (*(*loc_argv))++ ))
;
if(c=='.')/*collect fractional part*/
while(isdigit(s[++i]=c=*((*(*loc_argv))++) ))
;
//automatic s[i]='\0';
--*point_argc;
++(*loc_argv);
return NUMBER;
}
i am using the command gcc ./k\&r5-10a.c ./k\&r5-10b.c ./k\&r5-10c.c -o ./k\&r5-10 to compile them
but it is giving an error
./k&r5-10a.c:7:23: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
./k&r5-10a.c: In function ‘main’:
./k&r5-10a.c:17:4: error: ‘argc’ undeclared (first use in this function)
./k&r5-10a.c:17:4: note: each undeclared identifier is reported only once for each function it appears in
./k&r5-10b.c: In function ‘push’:
./k&r5-10b.c:12:3: warning: incompatible implicit declaration of built-in function ‘printf’
./k&r5-10b.c: In function ‘pop’:
./k&r5-10b.c:22:4: warning: incompatible implicit declaration of built-in function ‘printf’
./k&r5-10c.c:7:24: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
i dont know why its giving argc undeclared.last error says something about '&' token.but there is no '&' in 7th line of ./k&r5-10.c.so why is it giving such an error?

New Topic/Question
Reply


MultiQuote




|