So I have written a reverse polish notation calculator:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#define NUMBER '0'
#define MAXOP   100

int main ()
{
        int type;
        int op1, op2;
        char    s[MAXOP];
        while ((type = getop(s)) != EOF)
        switch (type)
        {
        case NUMBER:
          push(atoi(s));
          break;
        case '^':
          push ((unsigned int) pop() ^ (unsigned int) pop());
          break;
        case '~':
          push(~(unsigned int) pop());
          break;
        case '+':
        case '*':
          if (type == '+')
          push(pop() + pop());
          else
          push(pop() * pop());
          break;
        case '-':
          op2 = pop();
          push(pop() - op2);
          break;
        case '/':
        case '%':
          if ((op2 = pop()) != 0.0) {
                if (type == '/')
                        push(pop() / op2);
                else {
                        op1 = pop();
                        push(op1 - op2 * ((int) (op1/op2)));
                     }
        } else
                printf("Error: Zero divisor!\n");
                break;
        case '\n':
                printf("The answer is %d\n", pop());
                break;
        default:
                printf("Error: Unknown command %s!\n", s);
                break;
        }
                return 0;
}

I now need to run the infix string " ~(((202%16) + (292/16)*16) ^292 " through my calculator...however the string is in infix, RPN takes postfix. I have been having trouble converting this expression to postfix and need help. The program itself is done I just want to test that input and don't quite understand how to make it work in postfix.

Recommended Answers

All 2 Replies

Just explore this site a bit.
Infix to postfix conversion has been discussed earlier in many threads.

Thanks! Got my answer:

292 16 % 292 16 / + 16 * 292 ^ ~

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.