finished it.
its working..
Code:
/*--------------------------------------------------------------------------------------------------------
| Reverse Polish Calculator
| Now you can easily calculate numbers
| Author: Otto Tatsumi, The George Washington University
| Last modified: May 6th 2008
| Always Providing You with Simple Elegant Programs Since 2007
--------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
#define STACKSIZE 6
int stack_pointer = 0;
int STACKDISPLAY = 0;
long double stack[STACKSIZE] = {0.0};
int string_equal(const char* first, const char *second)
{
return (strcmp(first, second) == 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];
char input[80]; memset(input, '\0', 80);
double aDouble;
while (1)
{
scanf("%s", &input);
long double numericalInput = atof(input);
if(numericalInput == 0.0)
{
// then try parsing the commands, like "chs", "abs", "sin", etc.
// use the stringEquals() method.
if(string_equal(input, "+"))
{
// addition
printf(" The sum is %f\n", push_onto_stack(pop_off_stack() + pop_off_stack()));
} else if (string_equal(input, "-")) {
// subtraction goes here
printf(" The sum is %f\n", push_onto_stack(pop_off_stack() - pop_off_stack()));
} else if (string_equal(input, "*")) {
//multiplcation here
printf(" The sum is %f\n", push_onto_stack(pop_off_stack() * pop_off_stack()));
} else if (string_equal(input, "/")) {
//division here
printf( "The sum is %f\n", push_onto_stack(pop_off_stack() / pop_off_stack()));
} else if (string_equal(input, "chs")) {
//change signs
printf( "The number is %f\n", push_onto_stack(pop_off_stack()*(-1)));
} else if (string_equal(input, "abs")) {
//absolute value
printf("the result is: %f\n", push_onto_stack(abs((double)pop_off_stack())));
} else if (string_equal(input, "sqrt")) {
//square root here
printf("the result is: %f\n", push_onto_stack(sqrt((double)pop_off_stack())));
} else if (string_equal(input, "sin")) {
//sin function here
printf("the result is: %f\n", push_onto_stack(sin((double)pop_off_stack())));
} else if (string_equal(input, "cos")) {
//cos function here
printf("the result is: %f\n", push_onto_stack(cos((double)pop_off_stack())));
} else if (string_equal(input, "tan")) {
//tan function here
printf("the result is: %f\n", push_onto_stack(tan((double)pop_off_stack())));
} else if (string_equal(input, "q") || string_equal(input, "Q")) {
//quitting program
printf("Quitting Good Bye \n");
exit(0);
}
} else {
// push the value 'numericalInput' onto the stack.
push_onto_stack (numericalInput);
}
}
/* end infinite while */
return 0; /* normal termination */
}
//by Otto Tatsumi at the George Washington University