In-Program Evaluation

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

In my projects I often need to evaluate expressions. So I created a class that provides evaluation of expressions with the posibility to declare variables and easy functions. For this article I include the XEvaluate-class in a 'calculator'-dialog, but normaly, I use the class in my application like this: XString erg = Evaluate("1+2*sqr(3)").

!!! The XString-class that XEvaluate uses, was also contributed at codeguru.

You can use external variables in a XDoubleMap. This typedef is allready defined in XEvaluate.h (typedef CMap<CString, LPCTSTR, double, double> XDoubleMap;):

	XDoubleMap var;
	var["alpha"] = 10;
	var["beta"] = 20;
	var["test"] = 1.234;

	double erg = dtoa(Evaluate("alpha+beta", var));

It's possible to use userdefined functions with one argument (this argument has to be 'x')

	Evaluate("f:=sin(x)+cos(x)");
	double erg = dtoa(Evaluate("f(pi/2)"));

You can also call Evaluate with more than one expression, if you divide the expressions by a semikolon:

	double erg = dtoa(Evaluate("a=10;b=20;c=a+b");

The following functions and operations are included in XEvaluate:

sin cos tan       trigonometric functions
asin acos atan       inverse trigonometric functions
rad deg gon       trigonometric calculation type
pow sqr sqrt exp log log10 power, square, squareroot, exp, natural log, tenth log
abs int fract fak cube   absolute, integer part, factional part, n!, cube
dec hex bin char     outputtype
fix           set the decimal digits
+ - * /     add, sub, mul, div
% & | ^ ! ~ modulo, binary and, or, xor, not, binary not
< > <= >= == != less, greater, less or equal, greater or equal, equal, not equal
= += -= *= /=   variable setting
@           old value (result of the last calculation)
?           help (lists all functions)
#           reset (delete all functions and variables)
'           actual status (number of decimal digits, trigonometric mode and output mode)
$           var and function list

Use the sample program to find out what's possible with this class.

Download demo project - 47KB

Download source - 8KB



Comments

  • calculator

    Posted by Legacy on 09/23/2000 12:00am

    Originally posted by: madhuripai

    i want a code on calculator in vc++ using mfc.can i get it
    urgently

    Reply
  • I'd like some update

    Posted by Legacy on 02/17/2000 12:00am

    Originally posted by: LuisR


    Hello,

    This is a great piece of code. Unfortunately, because it is so complex, it has some bugs, some of which have been pointed in here.

    I have one question, does the author have an update for this class that he could send to code guru?? I'd like to use this class in one of my projects, but it still has some bugs.

    See ya,

    -- LuisR

    Reply
  • Shift operator (<<, >>) missing!

    Posted by Legacy on 01/04/2000 12:00am

    Originally posted by: AndreasF

    How can I extend the functionality with the shift operators?
    Please Help me.

    Thanks
    Andreas

    Reply
  • Equality operator bug (and possible fix)

    Posted by Legacy on 07/21/1999 12:00am

    Originally posted by: Thomas Fejes

    While using this great expression evaluation code,
    
    I came across a possible bug. If you run the
    Calc.exe sample program and enter the following:

    a=5
    b=5
    a==b
    returns 0 instead of 1
    a<=b
    returns 0 instead of 1
    etc.

    also
    c=(a==b)
    does not set c to anything, but a becomes 0

    The cause seems to be that the expressions are
    calculated before the external variables. This
    causes the op and txt lists to be added to, but
    the val list never grows, and the comparisons
    always fail.

    A possible fix for this is to reverse the order
    in the XEvaluate.CPP code shown below. This may
    cause other problems, but it fixes the bug shown
    above.

    Please comment on other possible remedies.

    before Line 601:

    //====================================================================
    // Funktionen, Zuweisungen, Variablen, Fehler
    //====================================================================
    default:
    for (name = ""; isalnum(c); c = str[++i]) name += c;
    i--;
    // 1. Interne Funktion
    if (fkt.Lookup(name.Lower(), j)) { OpPush(STOP); OpPush(j); break; }
    // 2. Benutzerfunktion
    if (usrfn.Lookup(name, hilf)) { TxtPush(name); OpPush(STOP); OpPush(USRFN); break; }
    // 3. Zuweisung
    if ((i+1 < len) && (str[i+1] == '=')) { TxtPush(name); break; }
    // 4. Arithmetische Zuweisung (+=, ..)
    if ((i+2 < len) && (str[i+2] == '=')) { TxtPush(name); break; }
    // 5. Externe Variable
    if (extVar && extVar->Lookup(name, a)) { ValPush(a); break; }
    // 6. Interne Variable
    if (var.Lookup(name, a)) { ValPush(a); break; }
    // 7. Fehler
    return Error("var " + name);
    }

    after:

    //====================================================================
    // Funktionen, Zuweisungen, Variablen, Fehler
    //====================================================================
    default:
    for (name = ""; isalnum(c); c = str[++i]) name += c;
    i--;
    // 1. Interne Funktion
    if (fkt.Lookup(name.Lower(), j)) { OpPush(STOP); OpPush(j); break; }
    // 2. Benutzerfunktion
    if (usrfn.Lookup(name, hilf)) { TxtPush(name); OpPush(STOP); OpPush(USRFN); break; }
    // 5. Externe Variable
    if (extVar && extVar->Lookup(name, a)) { ValPush(a); break; }
    // 6. Interne Variable
    if (var.Lookup(name, a)) { ValPush(a); break; }
    // 3. Zuweisung
    if ((i+1 < len) && (str[i+1] == '=')) { TxtPush(name); break; }
    // 4. Arithmetische Zuweisung (+=, ..)
    if ((i+2 < len) && (str[i+2] == '=')) { TxtPush(name); break; }
    // 7. Fehler
    return Error("var " + name);
    }

    Reply
  • Bug&Fix: wrong evaluation of unary functions

    Posted by Legacy on 03/02/1999 12:00am

    Originally posted by: HDaemon

    int(3*2+0.5) = 6.5 ???
    
    

    Fix: str 511
    change
    > case '(': if (IsBinOp(OpTop())) OpPush(STOP); break;
    to
    > case '(': if (!IsBinOp(OpTop())) OpPush(STOP); break;
    > ~~~

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Whether a mandate to secure all web and mobile apps comes from a newly enlightened CIO or in response to a major security breach, beginning even a small application security program can be a daunting task. How will you know how many digital assets you have, let alone their risk profile? In this webinar we explore how, using a cloud solution like Fortify on Demand, even the largest organizations can begin to scan apps immediately and rapidly scale an application security program. Identify and risk rank assets, …

  • MongoDB has the flexibility, adaptability and extensibility to embrace widely varying data types and rapid design/deployment cycles. Combining MongoDB with our 5100 ECO Enterprise SSD brings amazing results in smaller, simpler deployments compared to legacy storage. In this technical brief, we compare two MongoDB test clusters, each using the Linux Logical Volume Manager (LVM) for RAID configuration: 5100 ECO 3-node cluster: Two Micron 5100 ECO (1.92TB) per node configured as a software RAID 0 (LVM) Legacy …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date