Click here to Skip to main content
Click here to Skip to main content

C# RPN Expression

, 31 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

odisej

Slovenia Slovenia
No Biography provided

Comments and Discussions

 
Generalplease fix this bug Pinmemberlidujun200817-Jan-10 16:15 
Generalserious bug [modified] Pinmemberhoanganh17b@gmail.com1-Sep-09 6:23 
General[My vote of 1] This code has a show-stopper bug Pinmemberaaava6-May-09 10:00 
GeneralArticle PinmemberMato221-Apr-09 21:24 
GeneralMy vote of 2 PinmemberPablo Robert1-Apr-09 3:04 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411022.1 | Last Updated 31 Mar 2009
Article Copyright 2009 by odisej
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid