2015-11-13 58 views
-2

我想讓一個變量被解釋爲一個命令。 所以如何讓一個變量在delphi中被解釋爲一個函數/命令?

procedure TForm1.Button1Click(Sender: TObject); 
begin 
    opp := 'add' ;  
end; 

procedure TForm1.Button2Click(Sender : TObject); 
begin 
    opp := 'sub' 
end   

procedure TForm1.Button3Click(Sender : TObject); 
var 
    op1, op2: Integer; 
begin 
    op1 := 1;   
    op2 := 1; 
    Edit1.Text := FloatToStr(opp(op1,op2)); 
end; 
+0

您如何期待我們知道您在說什麼? – Wosi

+1

也許你的意思[程序類型](http://docwiki.embarcadero.com/RADStudio/Seattle/en/Procedural_Types)? – fantaghirocco

+0

我看到了一個可接受的答案,您可能也對錶達式解析器感興趣,例如http://wiki.freepascal.org/How_To_Use_TFPExpressionParser –

回答

2

您可以爲函數指定一個變量,以獲得與您想要實現的內容類似的功能。

更多關於程序類型here

type 
    TForm1 = class(TForm) 
    . . . 
    private 
    FOpp: function (AOp1, AOp2: Integer): Double; 
    . . . 
    end; 

implementation 

function OpAddFunction(AOp1, AOp2: Integer): Double; 
begin 
    Result := AOp1 + AOp2; 
end; 

function OpSubFunction(AOp1, AOp2: Integer): Double; 
begin 
    Result := AOp1 - AOp2; 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
    FOpp := OpAddFunction; 
end; 

procedure TForm1.Button2Click(Sender: TObject); 
begin 
    FOpp := OpSubFunction; 
end; 

procedure TForm1.Button3Click(Sender: TObject); 
var 
    op1, op2: Integer; 
begin 
    if not Assigned(FOpp) then 
    Exit; 
    op1 := 1; 
    op2 := 2; 
    Edit1.Text := FloatToStr(FOpp(op1, op2)); 
end; 

end. 
1

德爾福IST沒有腳本語言,一切都被編譯。

要將字符串解釋爲delphi指令,您需要腳本解析器,例如DWScript

0

我假定您的Button1Click和Button2Click處理程序的內容是基於操作名稱的文本操作,如何調用算術操作。所以我有點疑惑,你已經接受了一個不能解決這個問題的答案,但不管。其他回答告訴你,Delphi是一個編譯器,而不是腳本解釋器本身,但是它們有很多Delphi實現,而DWScript是一個特別優秀和強大的例子。

該答案重點在於根據操作名稱的文本操作調用算術操作。當然,這只是通用腳本解釋器(或者實際上更簡單的表達式評估器)所做的一小部分工作,但瞭解如何通過文本名稱自己執行此調用而沒有整個級聯「如果OpName ='xxx'那麼... else if ...」語句。

在下面的代碼,所述類TMethodWrapper用於存儲一個特定的整數運算(I已忽略爲了簡單浮點/雙精度的結果)的引用,並應用操作來調用的參數I1 & I2。算術評估是在其ApplyOperator方法中完成的。

然後TStringList,「OpList」被用作「字典」來存儲包裝和操作名稱。 Button1Click處理程序查找Edit1.Text中指定的操作並將其應用於操作的參數。

我寫了這個簡單的實現,以便它能在D7中工作。很顯然,在最近的Delphi版本中使用泛型設備時,將所涉及的原理應用於Integer之外的其他數據類型會更容易,並且可以使用RTTI添加函數及其名稱,而不是我在FormCreate中完成它的方式。他們也有類似TDictionary的對象,您可以在TStringList的步調中使用,

type 

    TIntegerOperation = function(Int1, Int2 : Integer) : Integer of object; 

    TMethodWrapper = Class 
    private 
    FOperation : TIntegerOperation; 
    public 
    constructor Create(AnOperation : TIntegerOperation); 
    function GetValue(I1, I2 : Integer) : Integer; 
    property Operation : TIntegerOperation read FOperation write FOperation; 
    end; 

    TForm1 = class(TForm) 
    Edit1: TEdit; 
    Button1: TButton; 
    procedure Button1Click(Sender: TObject); 
    procedure FormCreate(Sender: TObject); 
    private 
    OpList : TStringList; 
    function Add(I1, I2 : Integer): Integer; 
    function Subtract(I1, I2 : Integer): Integer; 
    procedure ApplyOperator(OpName: String; I1, I2 : Integer); 
    public 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.DFM} 

function TForm1.Add(I1, I2 : Integer) : Integer; 
begin 
    Result := I1 + I2; 
end; 

function TForm1.Subtract(I1, I2 : Integer) : Integer; 
begin 
    Result := I1 - I2; 
end; 


procedure TForm1.FormCreate(Sender: TObject); 
begin 
    OpList := TStringList.Create; 

    OpList.AddObject('Add', TMethodWrapper.Create(Add)); 

    OpList.AddObject('Subtract', TMethodWrapper.Create(Subtract)); 

end; 

constructor TMethodWrapper.Create(AnOperation: TIntegerOperation); 
begin 
    inherited Create; 
    Operation := AnOperation; 
end; 

function TMethodWrapper.GetValue(I1, I2 : Integer): Integer; 
var 
    IntegerOperation : TIntegerOperation; 
begin 
    IntegerOperation := TIntegerOperation(Operation); 
    Result := IntegerOperation(I1, I2); 
end; 

procedure TForm1.ApplyOperator(OpName : String; I1, I2 : Integer); 
var 
    Index : Integer; 
    Res : Integer; 
    MW : TMethodWrapper; 
begin 
    Index := OpList.IndexOf(OpName); 
    if Index < 0 then Exit; 
    MW := TMethodWrapper(OpList.Objects[Index]); 
    Res := MW.GetValue(I1, I2); 
    Caption := IntToStr(Res); 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
    ApplyOperator(Edit1.Text, 3, 5); 
end; 
相關問題