8 Replies - 3815 Views - Last Post: 23 May 2012 - 08:33 AM Rate Topic: -----

#1 jink   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 10-July 11

reverse polish calculator using command line arguments error

Posted 23 May 2012 - 06:43 AM

i am doing a sum(not homework) from kernighan and ritchie which says we have to make a calculator using reverse polish notation which will accept the input as arguments to the program.
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?

Is This A Good Question/Topic? 0
  • +

Replies To: reverse polish calculator using command line arguments error

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: reverse polish calculator using command line arguments error

Posted 23 May 2012 - 06:58 AM

The only errors I get when I compile your code is:

Quote

main.c||In function ‘main’:|
main.c|39|warning: comparing floating point with == or != is unsafe|
main.c||In function ‘getop’:|
main.c|106|error: assignment makes integer from pointer without a cast|
main.c|91|warning: unused variable ‘j’|
||=== Build finished: 1 errors, 2 warnings ===|


But I did compile this as all one file. Don't forget that you will need to include the proper include files in each source file. Some of your errors are because you didn't include at least one include file. For example:

Quote

./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’

These errors are being caused because you didn't include the header file for printf.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 jink   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 10-July 11

Re: reverse polish calculator using command line arguments error

Posted 23 May 2012 - 07:24 AM

when i compiled it as one program i didnt get any error.thats good.
but i dont understand which header files am i missing?or what is going wrong?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: reverse polish calculator using command line arguments error

Posted 23 May 2012 - 07:42 AM

Well printf() requires what header file?

In k&r5-10c.c why are you defining the following functions?
int getch(void);
void ungetch(int);


You probably should be either including a header file where these non-standard functions are already defined or not using them. I recommend removing these lines, as I don't actually see you using these functions.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 jink   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 10-July 11

Re: reverse polish calculator using command line arguments error

Posted 23 May 2012 - 08:05 AM

i have included stdio.h for printf already.well you are right about the two functions,i deleted them but still i am getting same error.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: reverse polish calculator using command line arguments error

Posted 23 May 2012 - 08:11 AM

Post your current code, along with the complete error messages.

Also if you are just starting to learn C I would suggest you find another book, in my opinion this book is not the best book to use to learn the language. Once you have a good grasp of the language it does make a good reference.

Jim

This post has been edited by jimblumberg: 23 May 2012 - 08:26 AM

Was This Post Helpful? 1
  • +
  • -

#7 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: reverse polish calculator using command line arguments error

Posted 23 May 2012 - 08:18 AM

You know, this made my head hurt:
int getop(char [],int *point_argc,char **(*loc_argv));



Functions are good. However, it's also good to know what they should be responsible for. What do you really want from getop? Does it need to know the value comes from an array or decrements some pointer?

Keep it simple:
int getop(char *result, char *arg);

void processStatement(char *argv[], int size) {
	int i;
	for(i=0; i<size; i++) {
		char s[MAXOP];
		switch(getop(s, argv[i])) {
		/* ... */
	}
}

int main(int argc,char *argv[]) {
	processStatement(argv + 1, argc-1);
	return 0;
}



If you're going to do the global stack thing, at least make the state variables static.

However, globals are bad. Consider writing your stack so you can have more than one. e.g.
/* header */
typedef struct {
	double val[MAXVAL];
	int sp;
} Stack;

void stackInit(Stack *);
int stackPush(Stack *, double);
int stackPop(Stack *, double);


/* source */
void stackInit(Stack *s) { s->sp = 0; }

int stackPush(Stack *s, double f) {
	if(s->sp<MAXVAL) {
		s->val[s->sp++]=f;
		return 1;
	}
	printf("error:stack full,cant push %g\n",f);
	return 0;
}

int stackPop(Stack *s, double f) {
	if(s->sp>0)
		*f = s->val[--(s->sp)];
		return 1;
	}
	printf("error:stack empty\n");
	return 0;
}

/* in main */
void processStatement(char *argv[], int size) {
	Stack stack;
	int i;
	
	stackInit(&stack);
	for(i=0; i<size; i++) {



Was This Post Helpful? 1
  • +
  • -

#8 jink   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 10-July 11

Re: reverse polish calculator using command line arguments error

Posted 23 May 2012 - 08:23 AM

oh i am really sorry.i was saving the files but they were getting saved outside the file in which i wanted to save them.when you said "post your current code" i began to suspect that something is wrong other than the code.so i realised this.sorry for wasting your time.my code is compiling with only a petty error which i can sort out now.it seems that i was compiling some previous program which i had corrected later.
Was This Post Helpful? 0
  • +
  • -

#9 jink   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 10-July 11

Re: reverse polish calculator using command line arguments error

Posted 23 May 2012 - 08:33 AM

yeah i like your code.it is a lot cleaner.i must learn a thing or two or more from your code.thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1