Hey there. I have been having trouble compiling this Ritchie program on polish notation after i tried adding a mod case to it. it says I am missing a semicolon but I dont understand why. Also, for some reason, when I try to get the input to have a negative sign first or something, it says error. I have been using unix, so maybe that's why?
PHP Code:
#include <stdio.h>
/* This program will read Polish notation into the system and then translate it properly. */
#define MAXOP 100
#define NUMBER '0' /* flag, signals that a number was found */
double atof(char s[]);
int getop(char[]);
void push(double);
double pop(void);
main() {
int type;
double op2; /* placeholder for certain cases to calculate correctly */
char s[MAXOP];
printf("Enter postfix or ^D (control + D) to quit:\n");
while ((type = getop(s)) != EOF) {
switch (type) {
case NUMBER:
push(atof(s)); /* calls atof to translate to floating first */
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. Cannot divide by zero.\n");
break;
case '%':
op2 = pop();
if (op2 != 0.0)
push(int pop() % int op2);
else
printf("Error, Cannot mod by zero.\n");
break;
case '\n':
printf("%.8f\n", pop());
printf("Enter postfix or ^D to quit:\n");
break;
default:
printf("Error. Enter postfix or ^D to quit:\n");
break;
}
}
return 0;
} /* end of main */
double atof(char s[]) { /* converts string s to type double */
double val, power;
int i, sign;
for (i = 0; isspace(s[i]); i++) /* crunches out whitespace */
;
sign = (s[i] == '-') ? -1 : 1; /* checks for minus sign, options for first character */
if (s[i] == '+' || s[i] == '-') /* checks if there is a sign at all, if so bumps over it */
i++;
for (val = 0.0; isdigit(s[i]); i++)
val = 10.0 * val + (s[i] - '0'); /* subtracts out the 30 to account for ascii */
if (s[i] == '.')
i++;
for (power = 1.0; isdigit(s[i]); i++) {
val = 10.0 * val + (s[i] - '0');
power *= 10.0;
}
return (sign * val / power); /* returns expression */
} /* end of atof */
int getch(void);
void ungetch(int);
int getop(char s[]) { /* gets next operator or operand */
int i, c;
while ((s[0] = c = getch()) == ' ' || c == '\t') /* crunches out whitespace */
;
s[1] = '\0';
if (!isdigit(c) && c != '.')
return c; /* it's not a number */
i = 0;
if (isdigit(c)) /* gets integer part */
while (isdigit(s[++i] = c = getch()))
;
if ( c == '.') /* gets fraction part */
while (isdigit(s[++i] = c = getch()))
;
s[i] = '0'; /* terminates string */
if (c != EOF)
ungetch(c); /* read too far, need to put back */
return NUMBER;
} /* end of getop */
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0; /* indicates next free position in buffer */
int getch(void) { /* gets a character */
return (bufp > 0) ? buf[--bufp] : getchar(); /* checks if something is there, gives options */
} /* end of getch */
void ungetch(int c) { /* pushes character back on input if necessary */
if (bufp >= BUFSIZE)
printf("Overflow, no space left.\n");
else
buf[bufp++] = c; /* otherwise puts in array, bumps pointer to next available position */
} /* end of ungetch */
#define MAXVAL 100
int sp = 0;
double val[MAXVAL];
void push(double f) { /* pushes f onto stack */
if (sp < MAXVAL)
val[sp++] = f; /* puts f on stack if room */
else
printf("Error. Stack is full, can't push.\n");
} /* end of push */
double pop(void) { /* pops values from stack */
if (sp > 0)
return val[--sp];
else {
printf("Error. Stack is empty.\n");
return 0.0;
}
} /* end of pop */
If anyone would be willing to pop this in a compiler and tell me what the heck is going wrong i'd appreciate it. Thanks!