Postfix (reverse polish notation) calculator
hi, I am working on another project. For this one I have to create a reverse polish notation calculator. I have almost everything down but I do not know how to continue. I can only get single integers to read into my program. I do not know how to read in multiple integer numbers or know how to read numbers with decimals in between. the program does not compile either. I need major help. I already spent butt loads of work.... :(
Code:
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#define STACKSIZE 6
int stack_pointer = 0;
int STACKDISPLAY = 0;
double stack[STACKSIZE] = {0.0};
void
show_stack(char message[])
{
/*
* this printout may be helpful for debugging and understanding what is
* going on
*/
int i;
if (!STACKDISPLAY)
{
return; /* return unless STACKDISPLAY is turned on */
}
printf("Displaying stack at end of function %s\nStack contents:\n\r", message);
for (i = stack_pointer; i >= 0; i--)
printf("stack[%d] = %f\n\r", i, stack[i]);
printf("\n\n");
}
double
pop_off_stack(void)
{
double return_value;
return_value = stack[stack_pointer]; /* return value on the top of the
* stack */
if (stack_pointer > 0) /* move the stack pointer down if not at the*/
/* lowest point */
{
stack_pointer--;
}
show_stack("pop_off_stack");
return return_value;
}
double
top_of_stack_value(void)
{
return stack[stack_pointer]; /* just return the value on the top of the
* stack */
}
double
push_onto_stack(double value)
{
int i;
stack[++stack_pointer] = value; /* put value on the top of the stack */
/*
* check to see if stack full, if so, move down, causing the bottom value
* to disappear
*/
assert(stack_pointer < (STACKSIZE + 1));
if (stack_pointer == STACKSIZE)
{
for (i = 0; i < STACKSIZE; i++)
stack[i] = stack[i + 1];
stack_pointer = STACKSIZE - 2; /* maximume of STACKSIZE-2 items max
* on stack */
}
show_stack("push_onto_stack");
return value;
}
int
main(int argc, char **argv)
{
char c, instr[80];
double aDouble;
while (1)
{ /* iterate until input file runs out or user
* types q or Q */
c = getchar();
if (c == EOF)
{
break;
} /* end of file was reached, quit processing
* input */
if (isdigit(c)) /* lame way to read a number in gets one
* digit only */
{
instr[0] = c;
instr[1] = '\0';
aDouble = atof(instr); /* convert string to float */
push_onto_stack(aDouble);
} /* end of lame way to read a number */
if ((c == 'q') || (c == 'Q'))
{
printf("Quiting. Bye.\n");
break; /* quit on a Q or q */
}
if (c == '+')
{
printf("The sum is: %f\n",
push_onto_stack(pop_off_stack() + pop_off_stack()));
}
/*
* the above statement sums the top two elements on the stack and push the result back onto
* the stack
*/
if (c == '-')
{
printf("the result is: %f\n", push_onto_stack(pop_off_stack() - pop_off_stack()));
}
/*
* the above statement subtracts the top two elements on the stack and push the result back onto
* the stack
*/
if (c == '*')
{
printf("the result is: %f\n", push_onto_stack(pop_off_stack() * pop_off_stack()));
}
/*
* the above statement multiplies the top two elements on the stack and push the result back onto
* the stack
*/
if (c == '/')
{
printf("the result is: %f\n", push_onto_stack(pop_off_stack() / pop_off_stack()));
}
/*
* the above statement multiplies the top two elements on the stack and push the result back onto
* the stack
*/
if (c == "chs")
{
if (
{
push_onto_stack((double)pop_off_stack()*(-1.000000))= -((double)pop_off_stack());
printf("the result is: %f\n", push_onto_stack((double)pop_off_stack()));
}
else if
{
push_onto_stack((double)pop_off_stack()*(-1.000000))= ((double)pop_off_stack());
printf("the result is: %f\n", push_onto_stack((double)pop_off_stack()*(-1.000000)));
}
}
if (c == "abs")
{
printf("the result is: %f\n", push_onto_stack(abs((double)pop_off_stack())));
}
if (c == "sqrt")
{
printf("the result is: %f\n", push_onto_stack(sqrt((double)pop_off_stack())));
}
if (c == "sin")
{
printf("the result is: %f\n", push_onto_stack(sin((double)pop_off_stack())));
}
if (c == "cos")
{
printf("the result is: %f\n", push_onto_stack(cos((double)pop_off_stack())));
}
if (c == "tan")
{
printf("the result is: %f\n", push_onto_stack(tan((double)pop_off_stack())));
}
} /* end infinite while */
return 0; /* normal termination */
}