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.

Dani AI

Generated

Quick, practical notes that build on 's pointer to existing threads and 's example.

The important semantics: ^ in your calculator is bitwise XOR (not exponentiation) and ~ is a unary prefix bitwise-NOT. In postfix the prefix unary operator becomes a postfix token (so ~x turns into x ~). Precedence you should enforce (highest → lowest): unary ~, multiplicative * / %, additive + -, then bitwise XOR ^. Treat unary ~ as right-associative; all the binary operators used here are left-associative.

A concise shunting-yard outline to convert infix → postfix:

  • Tokenize numbers, operators and parentheses. Ensure multi-digit numbers are single tokens.
  • If token is a number: output it.
  • If token is an operator O:
    • while stack top is operator with greater precedence, or equal precedence and left-associative, pop it to output
    • push O
  • If token is (: push it. If ): pop operators to output until ( is popped.
  • After input ends, pop remaining operators to output.

Handle unary operators explicitly while tokenizing: when an operator appears at expression start or immediately after ( or another operator, treat ~ as unary. Represent it internally (e.g., u~) so it gets the correct precedence and is emitted after its operand.

Practical tips for your existing calculator:

  • Feed RPN as space-separated tokens so getop/atoi reads numbers correctly.
  • Verify integer semantics: division and modulo are integer operations; negative operands follow C rules. Use op2 != 0 (integer 0) for divisor checks to avoid mixing float literals.
  • If results are wrong, test and convert small subexpressions first, confirm parentheses balance, and confirm your converter distinguishes unary vs binary operators.

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.