Jump to content

RPN (postfix calculator)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
butrus

butrus

    Newbie

  • Members
  • Pip
  • 1 posts
I have been given a task writing a postfix calculator. I know how to do it using a stack, but that's the catch. I was told to do with recursion and no stack. I busted my *** off trying to figure this out but I'm running short of time and I could really use your help \ tips.
thanks ahead.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I suspect that "by recursion" it means something like the following:
float process(string rpn_string)
{
  string operator = //peel off operator
  string param1 = //peel out first param string
  string param2 = //peel out second param string
  result = process(param1) operator process(param2)
}

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
nutario

nutario

    Newbie

  • Members
  • PipPip
  • 23 posts
It is nearly the same. When you use a stack, you musst manage all the operations by your self. When you are using recursion, than the operating system manages your stack operations for you.

The operating system puts all function calls on a stack.
So you allready have a stack you can use.

The stack of the operating system contains the hole function. So your function must manage the push and pop operations like the code example above.
Your stack in a non recursive function only contains what you pushed in (e.g. a string).

Edited by nutario, 22 March 2008 - 01:28 AM.