C Board  

Go Back   C Board > Community Boards > Tech Board

Reply
 
LinkBack Thread Tools Display Modes
Old 09-05-2006, 10:32 PM   #1
int x = *((int *) NULL);
 
Cactus_Hugger's Avatar
 
Join Date: Jul 2003
Location: Banks of the River Styx
Posts: 902
RPN & Functions

I wrote a program a while back to take an Infix expression ("2 + 2 * 3") and convert it to Postfix notation (Reverse Polish) and evaluate it. Worked like a charm, and can handle things as complex as "2 * (4 + 5 / 5 * sin(490.333))"
Now what I'm wondering is if there is any way to implement functions with a variable number of arguments. The only bright ideas I've had so far is "don't" and to keep the first thing on the stack as a "# of parameters", for those functions that need them. So, say log could take either 1 or 2 parameters, and took the form log(number, [base]), where base is the optional parameter. So far, I suppose I could write:
number 1 log
or
base number 2 log
But keeping the # of arguments like that seems like a kludge, especially if I ever want to actually use the RPN side as a human, and not just for evaluating the results of a infix conversion.

Any other ideas on how to implement this? Are there any RPN calculators out there with variable-number of argument functions - if so, how do they do it?
(Reverse Polish Notation (RPN), or postfix notation)
__________________
long time; /* know C? */
Unprecedented performance: Nothing ever ran this slow before.
Any sufficiently advanced bug is indistinguishable from a feature.
Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
The best way to accelerate an IBM is at 9.8 m/s/s.
recursion (re - cur' - zhun) n. 1. (see recursion)
Cactus_Hugger is offline   Reply With Quote
Old 09-06-2006, 09:27 AM   #2
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
The function could perhaps just pop off as many arguments as it needs. If the function doesn't know how many parameters it needs, you have no choice but passing the number of arguments anyway, or perhaps an end-of-arguments sentinel.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Old 09-06-2006, 10:22 AM   #3
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,661
I'm thinking that it should be the parser which reads either
log(10)
or
log(10,2)
and always pushes the default value onto the stack if none is specified.

Then when the evaluate comes along, the correct number of parameters are always present.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 09-06-2006, 01:21 PM   #4
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
But I think he's referring to functions that can take any amount of parameters, like
Code:
function max(parameter_bundle numbers)
  result = first of numbers
  for each n in numbers except first do
    if n greater than result then result = n
  end
end with result
Called like
max(1, 2, 3, 4, 5, 6, 7, 8) -> 8
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Functions and Classes - What did I do wrong? redmage C++ Programming 5 04-11-2005 11:50 AM
calling functions within functions edd1986 C Programming 3 03-29-2005 03:35 AM
Question regarding RPN and error detection Roule C++ Programming 4 10-02-2004 01:12 AM
Factory Functions HOWTO GuardianDevil Windows Programming 1 05-01-2004 01:41 PM
Shell functions on Win XP geek@02 Windows Programming 6 04-19-2004 05:39 AM


All times are GMT -6. The time now is 02:28 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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