Click here to Skip to main content
Licence CPOL
First Posted 31 Mar 2009
Views 8,658
Downloads 149
Bookmarked 8 times

C# RPN Expression

By | 31 Mar 2009 | Article
Calculate expressions with variables and functions using RPN.

Introduction

This article describes a simple but powerful expression parser using RPN. Expressions know how to deal with variables and functions. Additional functions can also be implemented.

Background

The calculations are based on RPN - Reverse Polish Notation. Read more about RPN here: http://www.calculator.org/rpn.html.

Using the code

The usage is very simple. Just create an environment and an expression, supply the expression string and variables, and calculate.

The environment is used to register the functions. Sometimes, functions are not needed so there so no need to register them.

ExprEnvironment environment = new ExprEnvironment();
RPNFunctionUtils.RegisterFunctions(environment);

RPNExpr expr = new RPNExpr();
expr.Environment = environment;
expr.AddVariable(new Variable("X", 4));
expr.AddVariable(new Variable("Y", 5));
expr.Prepare();
label5.Text = expr.GetValue().ToString();

Once an expression is prepared, you may call GetValue many times with different variables. The expression will just go through the stack and calculate the value rather than parse the whole expression again.

expr.VariableByName("X").value = 9;
expr.VariableByName("Y").value = 3;
label5.Text = expr.GetValue().ToString();

The RPN stack can be extracted from the list and shown to the user.

Points of interest

Try to implement your own function. It's very easy, you can find an example in the demo. Data fields can also be implemented. If you use something like [MyField] in an expression string, the expression will call the environment to return the value. You need to inherit the environment and you are ready to use the expression with data fields.

History and thanks

I should mention that RPNExpression was first written in Delphi and then rewritten to C# with some help from my friends Gregor Jakša and Matjaž Oman.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

odisej



Slovenia Slovenia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalplease fix this bug Pinmemberlidujun200815:15 17 Jan '10  
Generalserious bug [modified] Pinmemberhoanganh17b@gmail.com5:23 1 Sep '09  
General[My vote of 1] This code has a show-stopper bug Pinmemberaaava9:00 6 May '09  
GeneralArticle PinmemberMato2220:24 1 Apr '09  
GeneralMy vote of 2 PinmemberPablo Robert2:04 1 Apr '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 31 Mar 2009
Article Copyright 2009 by odisej
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid