Reverse polish notation in C#

(case-insensitive)



Functions available:
  • abs - absolute
  • sqrt - square root
  • sign - gets the sign of value (+ or -)
  • sin - trigonometry sine
  • cos - trigonometry cosine
  • tan - trigonometry tangent
  • asin - trigonometry inverse sine
  • acos - trigonometry inverse cosine
  • atan - trigonometry inverse tangent
  • sinh - trigonometry hyperbolic sine
  • cosh - trigonometry hyperbolic cosine
  • tanh - trigonometry hyperbolic tangent
Operators are:
  • + for addition
  • - for subtraction
  • * for multiplication
  • / for division
  • ^ for exponents (2^3 means 2 * 2 * 2)
Defined constants:
  • e - approximate value 2.71828
  • pi - approximate value 3.14159

The hard work was in parsing the expression, which was accomplished with regular expressions. I wrote a tutorial for this, which was adapted into this implementation. You can download the source at the end of that tutorial. Feel free to study and use that code as you see fit.

There's a follow up post on my blog, explaining a bit more of the background.