Thread: question

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    question

    here is a

    Code:
    */
     * reverse polish calculator
     */
    
    #include <stdio.h>
    #include "calc.h" /* header file */
    #include <math.h>
    #include <stdlib.h>
    
    #define MAXOP 100
    
    main()
    {
      int type;
      int op2;
      char s[MAXOP];
      
      while ((type = getop(s)) != EOF) 
        {
          switch(type) 
    	{
    	case NUMBER:
    	  push(atof(s));
    	  break;
    
            /* case '^':
              push(pop()^pop());
              break; */
    
            case '~':
              op2 = pop();
              push(pop() ~ op2);
              break;
              
    	case '+':
    	  push(pop() + pop());
    	  break;
    	case '*':
    	  push(pop() * pop());
    	  break;
            case '-':
    	  op2 = pop();
    	  push(pop() - op2);
    	  break;
    
            case '%':
              op2 = pop();
              if(op2)
              push(fmod(pop(), op2));
              else
              printf("\nError: Division by zero!");
              break;
    
    
    	case '/':
    	  op2 = pop();
    	  if (op2 != 0.0)
    	    push(pop() / op2);
    	  else 
    	    {
    	      printf("error : zero divisor\n");
    	      exit(1);
    	    }
    	  break;
    	case '\n':
    	  printf("The answer is %.8g.\n", pop());
    	  break;
    	default:
    	  printf("error: unknown command %s\n", s);
    	  exit(1);
    	}
        }
    }

    looking at this part of the code I'm doing something wrong

    Code:
            case '^':
              push(pop()^pop());
              break; 
    
            case '~':
              op2 = pop();
              push(pop() ~ op2);
              break;
    i am getting

    "parser error before '~' token
    main.o error 1
    "parser error before '^' token
    main.o error 1

    i am trying to add these binary operations to the RPN calculator, what's wrong here?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... but ~ is a unary, not binary (dyadic) operator.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >push(pop() ~ op2);
    ~ isn't a binary operator, it's a unary operator.

    >push(pop()^pop());
    Can you show us how you've defined pop?
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    the ~ operator is a unary operator, meaning it only takes one variable. You're using it as a binary operator, which is one that uses two variables.

    unary: ! var
    unary: - var
    binary: var + var1
    binary: var && var1

    also, name your file main.c, not main.o

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    re:

    Code:
    #define NUMBER '0'
    
    void push(double);
    double pop(void);
    int getop(char[]);
    int getch(void);
    void ungetch(int);
    so how can I include these unary operators so that the calculator is able to perform these bit operations?

    and i changed my main.o to main.c. Thanks for the reponses
    Last edited by elrookie; 07-02-2007 at 09:27 AM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >double pop(void);
    That's your problem. ^ is a bitwise operator, and bitwise operators can't be used with floating-point values. I'm guessing you were trying to do x to the power of y rather than x XOR y. Try calling the pow function from math.h instead.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    Smile re:

    ohhh thanks guys now it works... !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM