Postfix (reverse polish notation) calculator

This is a discussion on Postfix (reverse polish notation) calculator within the C Programming forums, part of the General Programming Boards category; hi, I am working on another project. For this one I have to create a reverse polish notation calculator. I ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    17

    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 */
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    the program does not compile either.
    Very informative - show the error messages


    Code:
    instr[0] = c;
    instr[1] = '\0';
    aDouble = atof(instr); /* convert string to float */
    horrible idea
    int res = c - '0';

    should do the work better


    You better fix the indentation befoer posting
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    17
    the compiler errors are from if statements that I do not know how to set the conditions of...

    Code:
    			if (c == "chs")
    				{
    					if (
    						{
    								push_onto_stack((double)pop_off_stack()*(-1.000000))= -((double)pop_off_stack());
    								printf("the result is: &#37;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)));
    						}
    				}

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    c == "chs"

    c is character? you cannot compare it to string

    Code:
    push_onto_stack((double)pop_off_stack()*(-1.000000))= -((double)pop_off_stack());
    what is this? contest howto write most unreadable code?
    use temp vars and do it step by step...
    then maybe you see your problem, you succed to write the code in a way that really does what you want it to do...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    17
    how do you do that?

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by ottomated View Post
    how do you do that?
    do what?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    17
    right now I have pointers being compared to characters.
    how do I solve that?
    how do you use temp vars?

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    17
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse Polish Notation
    By Lucid15 in forum C Programming
    Replies: 7
    Last Post: 02-16-2009, 10:05 PM
  2. Impix or reverse polish notation
    By P4R4N01D in forum C Programming
    Replies: 10
    Last Post: 11-18-2008, 11:42 PM
  3. infix vs Reverse Polish notation
    By dionys in forum C Programming
    Replies: 1
    Last Post: 05-10-2004, 03:25 PM
  4. Reverse Polish Calculator
    By BlasterStar in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2003, 11:12 PM
  5. c++ Reverse Polish Calculator Help
    By knight101 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2001, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21