I am going through the C Programming Language book by Kernighan and Ritchie. On pages 76-79 is code for a reverse Polish calculator. I decided to try it out, so I put the main code in a file named calculator.c, the code for the stack in a file named stack.c, the code for getting the operands in a file named getop.c, and the code for getting input from the user in a file named getch.c I show the code below. I then opened a command window and typed this gcc command:
gcc calculator.c stack.c getop.c getch.c -o calculator
I got many errors.
I have several questions:
calculator.c has
#include <stdio.h>so why do I need it also in the other files; don't they inherit theinclude?calculator.c also has
#define NUMBER '0'so why do I get an error in getop.c saying'NUMBER' is undeclared; again, doesn't it inherit thedefine?Is there some command that I can enter at the command line to direct
gccto use theincludeanddefinethat is in calculator.c with the other files? Or, do I need to repeat theincludeanddefinein the other files? What do C experts do?
Here is calculator.c
#include <stdio.h>
#include <math.h> // for atof() //
#define MAXOP 100 /* max size of operand or operator */
#define NUMBER '0' /* signal that a number was found */
int getop(char []);
void push(double);
double pop(void);
/* reverse Polish calculator */
int main()
{
int type;
double op2;
char s[MAXOP];
while ((type = getop(s)) != EOF) {
switch (type) {
case NUMBER:
push(atof(s));
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;
default:
printf("error: unknown command %a\n", s);
break;
}
}
return 0;
}
Here is stack.c
#define MAXVAL 100 /* maximum depth of the 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, can't 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;
}
}
Here is getop.c
#include <ctype.h>
int getch(void);
void ungetch(int);
/* getop: get next operator or numeric operand */
int getop(char s[])
{
int i, c;
while ((s[0] = c = getch()) == ' ' || c == '\t')
;
s[1] = '\0';
if (!isdigit(c) && c != '.')
return c; /* not a number */
i = 0;
if (isdigit(c)) /* collect integer part */
while (isdigit(s[++i] = c = getch()))
;
if (c == '.') /* collect fraction part */
while (isdigit(s[++i] = c = getchar()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
return NUMBER;
}
Here is getch.c
#define BUFSIZE 100
char buf[BUFSIZE]; /* buffer for ungetch */
int bufp = 0; /* next free position in buf */
int getch(void) /* get a (possibly pushed back) character */
{
return (bufp > 0 ? buf[--bufp] : getchar();
}
void ungetch(int c) /* push characters back on input */
{
if (bufp >= BUFSIZE)
printf("ungetch: too manycharacters\n");
else
buf[bufp++] = c;
}