Click here to Skip to main content
Licence 
First Posted 7 Apr 2003
Views 79,020
Bookmarked 44 times

String Parser and Evaluator

By | 7 Apr 2003 | Article
Formula parser for C#

Sample Image - String_Evaluator.gif

Introduction

This formula evaluator can use different data types like int, double, string, DateTime or bool to calculate the value of a mathematical expression. You can use any of the operators, +, -, *, /, >, <, == etc., and specify parameters for them.

Background

.NET doesn't support types like Variant as in VB6 or Delphi. To avoid validating data type before doing any operation, I could create Variant type that can be used to save data of different types and overload some operators that are frequently used, like bitwise +, unary + -, *,/.

This code works in 2 steps: First, parse the expression and transform it to Backward Poland Notation. Now every order item can be either an operator or an operand. Second, calculate value of order using stack. Variables are saved in a public static field, that has the Hashtable type shown in the "string/value" view.

Using the code

To use this code, you must add a reference to the Evaluator.dll library.

Hashtable ht = new Hashtable();
ht.Add("A",12);
ht.Add("B",15);
Za.Evaluator.Evaluator.Variables = ht;
try
{
    textBox1.Text = Za.Evaluator.Evaluator.Evaluate("[A] + [B]/1.2").ToString();
}
catch(Za.Evaluator.CalcException ex)
{
    SomeCode xxxxxxxxx;
}
catch(Exception ex)
{
    SomeCode xxxxxxxxx;
}            

To realize addition functions, you need to edit EmbeddedFunction in Calculator.cs file.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Zainaustria

Web Developer

Russian Federation Russian Federation

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
GeneralOperator Or (and,...) Pinmemberrichardlukes0:49 12 Nov '08  
GeneralUpdated Code Pinmemberzacksz11:03 26 Sep '08  
GeneralMuch easier to do this in .NET 2.0 PinmemberKristopher Cargile14:04 8 Apr '08  
GeneralA Bug needs attention Pinmembersakykhan14:04 30 May '07  
GeneralMinus/Substract Bug resolved PinmemberAfersol009:47 3 Aug '06  
GeneralGood Work, but got a bug PinmemberMarais20:46 13 Mar '06  
Generalbug Pinmemberwanwenguo20:42 20 Jan '05  
Generalbuggy Pinmemberdl4gbe16:12 21 Dec '04  
GeneralRe: buggy Pinmemberpatmarleon11:34 2 Jul '11  
GeneralI'd like to submit some improvements PinmemberMike955:44 13 Feb '04  
GeneralRe: I'd like to submit some improvements PinmemberTony M John4:24 27 Feb '04  
GeneralRe: I'd like to submit some improvements Pinmemberemiliodabdoub21:12 25 Sep '07  
GeneralImprovements PinmemberMatthew Dye6:51 28 Nov '03  
GeneralWhy not CodeDOM PinmemberJonathan de Halleux4:33 9 Oct '03  
GeneralBug PinmemberChibacity4:28 9 Oct '03  
GeneralRe: Bug PinmemberTJ Manjaly7:30 1 Mar '04  
GeneralRe: Bug PinmemberNaues4:43 16 Aug '04  
GeneralRe: Bug PinmemberSerkan Kokturk3:16 20 Dec '06  
GeneralAnd && Or operators PinmemberJ.M.Piulachs22:00 13 Sep '03  
QuestionUsage guidelines? PinsussArchieBunker10:57 22 May '03  
GeneralNicely Done PinmemberBlake Coverett18:07 8 Apr '03  
GeneralRe: Nicely Done Pinmemberjdunlap12:50 1 Sep '03  
GeneralRe: Nicely Done Pinmemberemiliodabdoub21:13 25 Sep '07  

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
Web04 | 2.5.120517.1 | Last Updated 8 Apr 2003
Article Copyright 2003 by Zainaustria
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid