Reverse Polish Notation???

This is a discussion on Reverse Polish Notation??? within the C Programming forums, part of the General Programming Boards category; Does anyone have the code readily available for RPN?? Just incase you are wondering what RPN is, It is what ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    34

    Reverse Polish Notation???

    Does anyone have the code readily available for RPN??

    Just incase you are wondering what RPN is, It is what the compiler sees when you enter a line like 5+2 which it would see
    5 2+ or something like that.

    What i need is the code to take the normal user input and printout what the compiler would see.

    I don't need one that works for everything, just one that works with () and single digit numbers.

    If you have one avaliable
    Thanks.

  2. #2
    Registered User EvenFlow's Avatar
    Join Date
    Oct 2001
    Posts
    422
    Well I do have a text with a case study on Polish Notation (Data Structures and Program Design in C, ISBN 013288366), but to type out the code would take ages. It doesn't cover reverse Polish Notation however, but it may get things started for you.

    hth

    edit - you may also want to look at this http://www.naveen.net/calculator/ - it's in javascript, but it might be worth a look
    Last edited by EvenFlow; 10-30-2001 at 11:28 PM.
    Ramble on...

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    34
    How about the code that would take the infix expression and give you the post fix expression in return?

  4. #4
    Registered User EvenFlow's Avatar
    Join Date
    Oct 2001
    Posts
    422
    Gimme a sec I'll type it out.
    Ramble on...

  5. #5
    Registered User EvenFlow's Avatar
    Join Date
    Oct 2001
    Posts
    422
    Code:
    /* InfixtoPostfix - translate from infix to postfix form
    Pre: The input is a valid expression in infix form.
    Post: The output is the input expression converted into postfix expression. */
    
    void InfixtoPostfix(Expression expr, Expression postfix)
    {
    	Stack stack;
    	StackEntry topentry;
    	Token token, tmp;
    	KindType type;
    
    	CreateStack(&stack);
    	do {
    		GetToken(token, expr);
    		switch (type = Kind(token)){
    
    		case OPERAND:
    			PutToken(token, postfix);
    			break;
    
    		case LEFTPAREN:
    			Push(token, &stack);
    			break;
    
    		case RIGHTPAREN:
    			for (Pop(token, &stack); Kind(token) != LEFTPAREN; Pop(token, &stack))
    				PutToken(token, postfix);
    			break;
    
    		case UNARYOP:	/* Treat both kinds together */
    
    		case BINARYOP:
    			do {
    				if (StackEmpty(&stack))
    					break;
    				else {
    					StackTop(&topentry, &stack);
    					if(Kind(topentry) == LEFTPAREN)
    						break;
    					else if (Priority(topentry) < Priority(token))
    						break;
    						else if (Priority(topentry) == Priority(token)
    							&& Priority(token) == MAXPRIORITY)
    							break;
    
    						else {
    							Pop(tmp, &stack);
    							PutToken(tmp, postfix);
    						}
    				}
    			}while (1);
    			Push(token, &stack);
    			break;
    
    		case ENDEXPR:
    			while(!StackEmpty(&stack)) {
    				Pop(token, &stack);
    				PutToken(token, postfix);
    			}
    			break;
    
    		}
    	}while (type != ENDEXPTR);
    }
    Ramble on...

  6. #6
    Registered User EvenFlow's Avatar
    Join Date
    Oct 2001
    Posts
    422
    From the text...

    "Since, in postfix form, all operators come after their operands, the task of translation from infix to postfix form amounts to moving operators so that they come after their operands instead of before or between them."

    Delay each operator until its right-hand operand has been translated. Pass each simple operand through to the output without delay

    Examples -

    infix form: x + y @ x x + y + z

    postfix form: x y + x @ x y z +
    Ramble on...

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    34
    You wouldnt happen to have the header files and the main you used would U??

    I am not seeing were some of your classes are coming from ie Tokens etc.

    Thanks

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

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