Thread: Reverse Polish Notation

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    101

    Reverse Polish Notation

    I am trying to write a calculator that calculates most mathematical operations in Reverse Polish Notation. However, i know im doing it right but when i try compiling it with just cc advcalc.c it gives me these errors... advcalc.c: In function ‘main’:
    Code:
    advcalc.c:31: error: invalid use of void expression
    advcalc.c:33: error: invalid use of void expression
    advcalc.c:35: error: invalid use of void expression
    advcalc.c:37: error: invalid use of void expression
    advcalc.c:38: warning: comparison between pointer and integer
    advcalc.c:39: error: invalid use of void expression
    advcalc.c:40: warning: comparison between pointer and integer
    advcalc.c:41: error: invalid use of void expression
    advcalc.c:42: warning: comparison between pointer and integer
    advcalc.c:43: error: invalid use of void expression
    advcalc.c:44: warning: comparison between pointer and integer
    advcalc.c:45: error: invalid use of void expression
    advcalc.c:46: warning: comparison between pointer and integer
    advcalc.c:47: error: invalid use of void expression
    advcalc.c:48: warning: comparison between pointer and integer
    advcalc.c:49: error: invalid use of void expression
    advcalc.c:50: warning: comparison between pointer and integer
    advcalc.c:51: error: invalid use of void expression
    advcalc.c:52: warning: comparison between pointer and integer
    advcalc.c:53: error: invalid use of void expression
    advcalc.c:57: error: expected declaration or statement at end of input
    any input?
    Code:
    //RPN postfix calculator
    #include <math.h>
    #include <stdlib.h>
    #include <stdio.h>
    //53* 4 7 8 9 * + cos * +   (5*3+4*cos(7+8*9)-original numerator
    //9 3 * 2 sqrt (9*3-sqrt(2))
    double stack[1000];
    int tos= -1;
    
    void push(double v){
      if (tos>999){printf ("Stack Overflow\n");exit(0);}
     tos++;
      stack[++tos]=v;
    }
    double pop(){
      double v;
      if(tos < 0){printf("stack underflow\n");exit(0);}
      v=stack[tos--];
      //v=stack[tos--];
    
      return v;
    
    }
    int main(int argc,char **argv){
      int x,y,z;
      char q;
      q=argv[1][0];
      if ((q=='-')|| (('0'<=q)&&(q<='9'))){
        tos = getchar();          
                if (tos == '+')
                    {printf("The sum is: %f\n",push(pop() + pop()));   } 
                if (tos == '-')
                    {printf("the result is: %f\n", push(pop() - pop())); }
                if (tos == 'x')
    	      {   printf("the result is: %f\n", push(pop() * pop()));}  
                if (tos == '/')
                    {  printf("the result is: %f\n", push(pop() / pop()));}   
    	    if (tos == "sqrt")
                    {printf("the result is: %f\n", push(sqrt((double)pop())));}
    	    if (tos == "qbrt")
                    { printf("the result is: %f\n", push(qbrt((double)pop())));}
                if (tos == "sin")
                    {  printf("the result is: %f\n", push(sin((double)pop())));    }
                if (tos == "cos")
                    {  printf("the result is: %f\n", push(cos((double)pop())));   }
                if (tos == "tan")
                    {printf("the result is: %f\n", push(tan((double)pop())));}
    	    if (tos == "asin")
                    {  printf("the result is: %f\n", push(asin((double)pop())));}
                if (tos == "acos")
                    {  printf("the result is: %f\n", push(acos((double)pop())));   }
                if (tos == "atan")
                    {printf("the result is: %f\n", push(atan((double)pop())));}
      }
      else{ printf("Please enter a valid integer first\n");
        exit(0);
      }

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    I didn't really pick through your code but you are obviously getting confused. Look at a line like this:
    Code:
    printf("The sum is: %f\n",push(pop() + pop()));
    The return value of push, is void!, meaning absolutely nothing. So you are trying to format nothing, which obviously throws an error. I bet if you fix this problem it will solve some other problems as well.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    How would i fix it? just make it pop before computing the output?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    supposedly i think this one works but i cannot get it to compile. It's somebody else's in the class which i am trying to compare to..
    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 */
                    {
                        int res = c - '0';
                    }
    
                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 */
    }

  5. #5
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Just look at what you are doing. You are trying to printf a (void).

    I know about reverse polish notation but I don't feel like rewriting your program. I trust you know how to implement it. I am only telling you that the argument you are sending to printf is illegal. See what I mean?

    Edit: Ok. I was typing while you posted that. But just look at both of their pop and push, they both return double. Hint hint.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Lucid15 View Post
    How would i fix it? just make it pop before computing the output?
    I would suggest making another function, like top() or so, that returns the top number but doesn't remove it from the stack.

    Also, is there a reason you are trying to do everything possible inside a print statement? Manipulate the stack, then print something. (If you did it that way, you might not need a top() since you would potentially still be holding onto the number you just pushed back on....)

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Quote Originally Posted by tabstop View Post
    I would suggest making another function, like top() or so, that returns the top number but doesn't remove it from the stack.

    Also, is there a reason you are trying to do everything possible inside a print statement? Manipulate the stack, then print something. (If you did it that way, you might not need a top() since you would potentially still be holding onto the number you just pushed back on....)
    Im actually pretty confused as someone helped me. Can you do an example of this on the + function?

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    ok so i got someone to show me their program which i now understand. but i need to know how i can implement the other functions such as sin cos and all of that ...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    double stack[1000];
    int tos;
    
    void push(double v){
      if(tos>998){
        printf("Stack overflow.\n"); exit(0);}
      tos++;
      stack[tos]=v;
    }
    
    double pop(){
      double v;
      if(tos<0){
        printf("Stack underflow.\n"); exit(0);}
      v=stack[tos];
      tos--;
      return v;
    }
    
    int main(int argc, char **argv)
    {
      double x,y,z,sum;
      int k,j,i,c; 
      tos=-1;
      j=1;
      i=argc-1;
    
      //work on while loop
    
    while(0<i){
      k=0;
      c=1;
      if(argv[j][k]=='+'){
        y=pop();
        x=pop();
        z=x+y;
        push(z);}
       
      else if(argv[j][k]=='-'){
        y=pop();
        x=pop();
        z=x-y;
        push(z);}
      else if(argv[j][k]=='x'){
        y=pop();
        x=pop();
        z=x*y;
        push(z);}
      else if(argv[j][k]=='/'){
        y=pop();
        x=pop();
        z=x/y;
        push(z);}
    
      // else if((argv[j][k]=='c') && (argv[j][k+3]==0)){
         //  x=pop();
        //  z=cos(x);
       // push(z);}
    
      // else if((argv[j][k]=='s') && (argv[j][k+1]=='i')){
      //  x=pop();
      //  z=sin(x);
      //  push(z);}
    
      // else if((argv[j][k]=='s') && (argv[j][k+1]=='q')){
      //  x=pop();
      //  z=sqrt(x);
      //  push(z);}
    
      //CAREFUL WITH THIS ONE
      else if((argv[j][k]=='e') && (argv[j][k+1]=='x')){
          x=pop();
          z=1;
          while(x>0){
    	z=z*2.7182818284590452353602874;
    	x--;}
        }
    
    
    
      else{
          while(argv[j][k]!=0){
    	k++;}
          k=k-1;
          sum=0;
          while(k>=0){
    	sum=sum+((argv[j][k]-48)*c);
    	c=c*10;
    	k--;
          }
       	push(sum);
        }
        j++;
        i--;
     }
     printf("= %lf\n", z);
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Impix or reverse polish notation
    By P4R4N01D in forum C Programming
    Replies: 10
    Last Post: 11-18-2008, 11:42 PM
  2. Postfix (reverse polish notation) calculator
    By ottomated in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 05:32 PM
  3. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  4. infix vs Reverse Polish notation
    By dionys in forum C Programming
    Replies: 1
    Last Post: 05-10-2004, 02:25 PM
  5. Reverse Polish Notation???
    By Wizard_D in forum C Programming
    Replies: 6
    Last Post: 11-01-2001, 12:20 AM