The following code acts as a reverse polish notation calculator
example: 5 5 +
outputs 10
I'm trying to figure out a method to print the top element of the stack without popping using the '/' command, but no luck. I also want to add a command 'a' to duplicate the top value of the stack, and then a command 'c' to clear the stack. Any help would be appreciated thanks!
Code:
#include <stdio.h>
#include <ctype.h>
#define MAXOP 100 /* Max size of operand or operator */
#define NUMBER '0' /* Signal that a number was found */
#define MAXVAL 100 /* Max value of stack */
/* Functions Prototypes */
double atof(char s[]);
int getop(char []);
void push(double);
double pop(void);
int getch(void);
void ungetch(int);
int main()
{
int c;
int type = 0;
int modulus;
double op1 = 0.0;
double op2 = 0.0;
char s[MAXOP];
int count = 0;
while((type = getop(s)) != EOF){
switch (type){
case NUMBER:
push(atof(s));
break;
/* Modulus operator code...returns the remainder
following the division between two integers
on the stack */
case '%':
modulus = pop();
/* cast both values on the stack to ints */
if (modulus != 0) {
push((int)pop() % (int)modulus);
}
else {
printf("error: Cannot mod by 0\n");
}
break;
case 'P':
ungetch(c);
printf("%d\n",c);
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");
}//end if()
break;
case '\n':
printf("\t%.8g\n", pop());
break;
default:
printf("error: unknown command %s\n", s);
break;
}//end switch()
}//end while()
return 0;
}//end main()
/* ****************** Functions *********************** */
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 sp = 0;
double val[MAXVAL];
void push(double f)
{
if(sp < MAXVAL){
val[sp++] = f;
}
else{
printf("error: stack full can't push %g\n",f);
}//end if()
}//End push()
double pop(void)
{
if(sp > 0 ){
return val[--sp];
}
else{
return 0.0;
}//end if()
}//end pop()
/* getop: get next operator or number operand */
int getop(char s[])
{
int i = 0;
int c = 0;
while((s[0] = c = getch()) == ' ' || c == '\t');
s[1] = '\0';
if(!isdigit(c) && c == '?'){
printf("%d", c) ;
}
if(!isdigit(c) && c != '.'){
return c ; /* not a number */
}//End if()
i = 0;
if(isdigit(c)){
while(isdigit(s[++i] = c = getch()));
}//End if()
if(c == '.') /* collect fraction part */
{
while(isdigit(s[++i] = c = getch()));
}//End if()
s[i] = '\0';
if(c != EOF){
ungetch(c);
}//end if()
return NUMBER;
}//End getop()
/* Globals for get functions below */
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0;
int getch(void)
{
return(bufp > 0) ? buf[--bufp] : getchar();
}// end getch()
void ungetch(int c)
{
if(bufp >= BUFSIZE)
{
printf("ungetch: too many characters \n");
}
else
{
buf[bufp++] = c;
}//End if
}// End ungetch