Problem Source: http://www.cnblogs.com/del/archive/2008/08/16/1268786.html#1289015
In this example, both Delphi 2007 and Delphi 2009 are debugged and run:
Code file:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; Procedure button1click (Sender: tobject); Procedure button2click (Sender: tobject); Procedure button3click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} uses comobj; // The runform function that executes mathematical formulas: // The principle is to use the javascrip script. The code references the format function of Delphi. // The first parameter is the formula, constants in the formula must be identified by ten uppercase letters a B c d e f g h I j. // The second parameter is a parameter array, returns the constant value in order (using a string); // currently, supported functions are listed below. However, in order to distinguish them from the following parameters, they can only be lowercase. function runform (formula: string; const ARGs: array of const): string; const F = 'acos = math. ACOs; '+' Asin = math. asin; '+' atan = math. atan; '+' atan2 = math. atan2; '+ 'ceil = math. ceil; '+' Cos = math. cos; '+' E = math. e; '+' exp = math. exp; '+' floor = math. floor; '+' ln10 = math. ln10; '+ 'ln2 = math. ln2; '+' log = math. log; '+ 'log10e = math. log10e; '+ 'log2e = math. log2e; '+' max = math. max; '+' min = math. min; '+' Pi = math. pi; '+' POW = math. pow; '+' random = math. random; '+' Round = math. round; '+' sin = math. sin; '+' SQRT = math. SQRT; '+' sqrt2 = math. sqrt2; '+' tan = math. tan; '; var Len, buflen: integer; Buffer: array [0 .. 4095] of char; Script: olevariant; I: integer; begin for I: = 0 to 9 do formula: = stringreplace (formula, char (I + 65 ), '%' + inttostr (I) + ': s', [rfreplaceall]); buflen: = length (buffer); If length (formula) = buflen-1 then begin while Len> = buflen-1 do begin Inc (buflen, buflen); Result: = ''; setlength (result, buflen ); {$ ifdef Unicode} Len: = formatbuf (pchar (result), buflen-1, pointer (formula) ^, length (formula), argS); {$ else} Len: = formatbuf (pointer (result) ^, buflen-1, pointer (formula) ^, length (formula), argS); {$ endif} end; setlength (result, Len ); end else setstring (result, buffer, Len); try Script: = createoleobject ('scriptcontrol'); script. language: = 'javascript '; script. executestatement (F + 'str = '+ result); Result: = script. eval ('str'); failed t result: = 'err'; end; {runform function end} // Test 1: note that the second parameter must be procedure tform1.button1click (Sender: tobject); var S: string; begin S: = runform ('(A + B) /(a-B) ', ['6', '4']); {Here A = 6; B = 4} // S: = runform ('(6 + 4)/(6-4)', []); {This can also be} showmessage (s); {5} end; // Test 2: Use case-sensitive commands procedure tform1.button2click (Sender: tobject); var S: string; begin S: = runform ('sin () + cos (B) + Tan (a) ', ['0. 8', '0. 9 ']); showmessage (s); {2.36860461622055} end; // Test 3. Javascript constants can be used, but procedure tform1.button3click (Sender: tobject) must be lowercase letters ); vaR S: string; begin S: = runform ('sqrt (POW (A, 2) ', ['Pi']); showmessage (s); {3.14159265358979} end; end.
Form file:
object Form1: TForm1 Left = 0 Top = 0 Caption = 'Form1' ClientHeight = 130 ClientWidth = 206 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False Position = poDesktopCenter PixelsPerInch = 96 TextHeight = 13 object Button1: TButton Left = 64 Top = 24 Width = 75 Height = 25 Caption = 'Button1' TabOrder = 0 OnClick = Button1Click end object Button2: TButton Left = 64 Top = 55 Width = 75 Height = 25 Caption = 'Button2' TabOrder = 1 OnClick = Button2Click end object Button3: TButton Left = 64 Top = 86 Width = 75 Height = 25 Caption = 'Button3' TabOrder = 2 OnClick = Button3Click endend