« Random Slashdot Stuff for Auction on eBay | Main | Interviewing Doctors vs. Interviewing Programmers »

October 20, 2007

Last Tweak of the RPN calculator

We had the first simple version and a more data-driven one. Now I figured out how to get a static class property, so I can make that part of the calculator generic. I still have to specify whether it is on [Math] or [Decimal], so add a couple new hashtables at the top:

$dfs = @{
    "max" = "MaxValue" ;
    "min" = "MinValue" ;
}
$mfs = @{
     "e" = "E" ;
     "pi" = "PI"
}

and then the code to invoke them, which replaces the hardcoded switch cases for "e", "pi", "min", and "max":

{ $dfs.$a } {
    $s.Push(
        [Decimal].GetMember($dfs.$a)[0].GetValue($null))
}
{ $mfs.$a } {
    $s.Push(
        [Convert]::ToDecimal(
            [Math].GetMember($mfs.$a)[0].GetValue($null)))
}

What's declared in the hashtables above is probably all the static properties you need; [Math] doesn't have any others, and if you look at the ones from [Decimal] (which you can see with "[Decimal] | get-member -static") it only has MinusOne, One, and Zero, which we don't need given how our calculator works (we can just use -1, 1, and 0 and have the regular [Decimal]::TryParse() pick them up).

OK, no more about this, at least for a while, I promise.

Posted by AdamBa at October 20, 2007 08:39 AM

Trackback Pings

TrackBack URL for this entry:
http://proudlyserving.com/cgi-bin/mt-tb.cgi/629

Comments