| Printable Version
Function Evaluator In C#
By Emad Barsoum
Environment: [eg Visual C#, Windows XP, Windows 2000 sp2]
Abstraction:
This Program use the transformation from infix
notation to postfix notation to evaluate most Mathematic expression, it support
most operators (+,-,*,/,%,^), functions from 0 to any number of parameters and
also a user defined function by using delegate, also it support variables in
the expression, it will generate a symbol table that can be updated at run
time. Also this program demonstrate the use of DataGrid as normal Grid
without any database connection.
Details:
All the code neccessary for transform infix expresion to
postfix expression and evaluate the postfix expression is encapsulated in the
"Function" class, also this class support variable which mean you can write
expression like "num1 + num2", it automatic will generate the symbol table for
it, and initialize all variable to zero by default.
The first member function that must be called in the
Function class is "Parse", which take the expression string as parameter and
transfrom it to an a "ArrayList" of Symbol, and store it in the member variable
"m_equation", the Symbol is a structure the contain information about any
symbol which is the smallest element in any equation, it contain the following
information:
-
m_name: the name of the symbol which may be ("num1", "+","cos","(","7",...etc).
-
m_type: the type of this symbol which is defined in the enumerator Type which
may be (variable, value, operator, comma,...etc).
-
m_value: the value of that symbol valid only if it is variable or value.
After that you must call the member function "Infix2Postfix",
which transform the infix notation stored in m_equation to postfix notation and
store it in m_postfix member variable, if the expression contain any variable,
you can call the Variables property to get the symbol table as ArrayList of
Symbol structure, this array will contains all variable type symbol only, after
changing the value of any variable just call the "Variables" property again and
put on it the updated symbol table, this property is both get and set.
After that you can call the member function "EvaluatePostfix"
to evaluate the expression, the result will be stored as double in m_result
member variable, which can be accessed by the property member "Result".
Also this Class have a number of defined function that can be
used directly in any expression, this function are case sensitive and must be
all small, the following are the buid in function:
| cos() |
Take a single parameter a raduis angle and return the "cos" of it. |
| sin() |
Take a single parameter a raduis angle and return the "sin" of it. |
| tan() |
Take a single parameter a raduis angle and return the "tan" of it. |
| cosh() |
Take a single parameter a raduis angle and return the "cosh" of it. |
| sinh() |
Take a single parameter a raduis angle and return the "sinh" of it. |
| tanh() |
Take a single parameter a raduis angle and return the "tanh" of it. |
| ln() |
Take a single parameter a number and return the "ln" of it, which is the
logarithm of base "e". |
| log() |
Take a single parameter a number and return the "log" of it, which is the
logarithm of base "10". |
| logn() |
Take a two parameters, the first is a number that we want to compute the
logarithm of it, the second parameter is the base of the logarithm. |
Also you can define your own functions, those functions can
contain from zero to any number of parameters, to define your own functions,
you must define a delegate function of the following type
"EvaluateFunctionDelegate" this type is define in Function.cs, then call the
"DefaultFunctionEvaluation" property member to set your delegate function, all
the functions that you have defined in the delegate function, will be added to
the built in function.
Also the variable "pi" is internally recognized as 3.14...,
if any error happen whatever the error type, the Error property will return
true and the Description of the Error will be returned by the
ErrorDescription property member.
Download the zip file to see some examples of how to use the Function class.
FunctionEvaluator.zip
|