对edit1的内容进行if逻辑判断
edit1里有逻辑判断语句,如A>B AND B>C
语句:if edit1.text then ...
有错误
[Error] :Type of expression must be BOOLEAN
怎样表达,才正确
[解决办法]
var
LText :string;
LNum :integer;
begin
LText := Trim(edit1.text);
if LText <> '' then
begin
LNum := StrToInt(LText);
if (LNum > B) and (B > C) then
ShowMessage('条件成立');
end;
end;
[解决办法]
楼上几位对楼主的意思都理解错了,他是希望做个解释器,对edit1里的逻辑表达式进行运算!
[解决办法]
如果楼主是为了做个脚本解释器,就不该是那样子做的;弄这个是为了做sql过滤条件的话,那么,使用DevExpress里的过滤器,十分方便,如果不是,那么,请你该将它的用途说出来,或者,别人会提供比你的方法更优、更简单的做法给你参考。
[解决办法]
可以试试调用JavaScript判断,不过要把符号替换成Java语言的符号,比如and变成&&; or变成
[解决办法]
在delphi中直接判断,很麻烦的,SQL语句复杂多变,要判断它的合法性几乎是不可能的。建议楼主还是将判断语句带入SQL语句直接判断。
[解决办法]
给你一个类吧,这是FastReport2中的一个类,把它借用过来就可以了。我刚才试了一下在DELPHI2010中已调试通过。太长了,你自己把它拼起来
unit FR_Pars;
interface
uses Classes, Variants;
type
TGetPValueEvent = procedure(const s: String; var v: Variant) of object;
TFunctionEvent = procedure(const Name: String; p1, p2, p3: Variant;
var Val: Variant) of object;
TfrParser = class
private
FOnGetValue: TGetPValueEvent;
FOnFunction: TFunctionEvent;
function GetIdentify(const s: String; var i: Integer): String;
function GetString(const s: String; var i: Integer):String;
procedure Get3Parameters(const s: String; var i: Integer;
var s1, s2, s3: String);
public
function Str2OPZ(s: String): String;
function CalcOPZ(const s: String): Variant;
function Calc(const s: String): Variant;
property OnGetValue: TGetPValueEvent read FOnGetValue write FOnGetValue;
property OnFunction: TFunctionEvent read FOnFunction write FOnFunction;
end;
TfrVariables = class(TObject)
private
FList: TStringList;
procedure SetVariable(const Name: String; Value: Variant);
function GetVariable(const Name: String): Variant;
procedure SetValue(Index: Integer; Value: Variant);
function GetValue(Index: Integer): Variant;
procedure SetName(Index: Integer; Value: String);
function GetName(Index: Integer): String;
function GetCount: Integer;
procedure SetSorted(Value: Boolean);
function GetSorted: Boolean;
public
constructor Create;
destructor Destroy; override;
procedure Assign(Value: TfrVariables);
procedure Clear;
procedure Delete(Index: Integer);
function IndexOf(const Name: String): Integer;
procedure Insert(Position: Integer; const Name: String);
property Variable[const Name: String]: Variant
read GetVariable write SetVariable; default;
property Value[Index: Integer]: Variant read GetValue write SetValue;
property Name[Index: Integer]: String read GetName write SetName;
property Count: Integer read GetCount;
property Sorted: Boolean read GetSorted write SetSorted;
end;
TfrFunctionSplitter = class
protected
FMatchFuncs, FSplitTo: TStrings;
FParser: TfrParser;
FVariables: TfrVariables;
public
constructor Create(MatchFuncs, SplitTo: TStrings; Variables: TfrVariables);
destructor Destroy; override;
procedure Split(s: String);
end;
function GetBrackedVariable(const s: String; var i, j: Integer): String;
implementation
uses SysUtils
{$IFDEF Delphi6}
, Variants
{$ENDIF};
type
PVariable = ^TVariable;
TVariable = record
Value: Variant;
end;
const
ttGe = #1; ttLe = #2;
ttNe = #3; ttOr = #4; ttAnd = #5;
ttInt = #6; ttFrac = #7;
ttUnMinus = #9; ttUnPlus = #10; ttStr = #11;
ttNot = #12; ttMod = #13; ttRound = #14;
function GetBrackedVariable(const s: String; var i, j: Integer): String;
var
c: Integer;
fl1, fl2: Boolean;
begin
j := i; fl1 := True; fl2 := True; c := 0;
Result := '';
if (s = '') or (j > Length(s)) then Exit;
Dec(j);
repeat
Inc(j);
if fl1 and fl2 then
if s[j] = '[' then
begin
if c = 0 then i := j;
Inc(c);
end
else if s[j] = ']' then Dec(c);
if fl1 then
if s[j] = '"' then fl2 := not fl2;
if fl2 then
if s[j] = '''' then fl1 := not fl1;
until (c = 0) or (j >= Length(s));
Result := Copy(s, i + 1, j - i - 1);
end;
{ TfrVariables }
constructor TfrVariables.Create;
begin
inherited Create;
FList := TStringList.Create;
FList.Duplicates := dupIgnore;
end;
destructor TfrVariables.Destroy;
begin
Clear;
FList.Free;
inherited Destroy;
end;
procedure TfrVariables.Assign(Value: TfrVariables);
var
i: Integer;
begin
Clear;
for i := 0 to Value.Count - 1 do
SetVariable(Value.Name[i], Value.Value[i]);
end;
procedure TfrVariables.Clear;
begin
while FList.Count > 0 do
Delete(0);
end;
procedure TfrVariables.SetVariable(const Name: String; Value: Variant);
var
i: Integer;
p: PVariable;
begin
i := IndexOf(Name);
if i <> -1 then
PVariable(FList.Objects[i]).Value := Value
else
begin
New(p);
p^.Value := Value;
FList.AddObject(Name, TObject(p));
end;
end;
function TfrVariables.GetVariable(const Name: String): Variant;
var
i: Integer;
begin
Result := Null;
i := IndexOf(Name);
if i <> -1 then
Result := PVariable(FList.Objects[i]).Value;
end;
procedure TfrVariables.SetValue(Index: Integer; Value: Variant);
begin
if (Index < 0) or (Index >= FList.Count) then Exit;
PVariable(FList.Objects[Index])^.Value := Value;
end;
function TfrVariables.GetValue(Index: Integer): Variant;
begin
Result := 0;
if (Index < 0) or (Index >= FList.Count) then Exit;
Result := PVariable(FList.Objects[Index])^.Value;
end;
function TfrVariables.IndexOf(const Name: String): Integer;
begin
Result := FList.IndexOf(Name);
end;
procedure TfrVariables.Insert(Position: Integer; const Name: String);
begin
SetVariable(Name, 0);
FList.Move(FList.IndexOf(Name), Position);
end;
function TfrVariables.GetCount: Integer;
begin
Result := FList.Count;
end;
procedure TfrVariables.SetName(Index: Integer; Value: String);
begin
if (Index < 0) or (Index >= FList.Count) then Exit;
FList[Index] := Value;
end;
function TfrVariables.GetName(Index: Integer): String;
begin
Result := '';
if (Index < 0) or (Index >= FList.Count) then Exit;
Result := FList[Index];
end;
procedure TfrVariables.Delete(Index: Integer);
var
p: PVariable;
begin
if (Index < 0) or (Index >= FList.Count) then Exit;
p := PVariable(FList.Objects[Index]);
Dispose(p);
FList.Delete(Index);
end;
procedure TfrVariables.SetSorted(Value: Boolean);
begin
FList.Sorted := Value;
end;
function TfrVariables.GetSorted: Boolean;
begin
Result := FList.Sorted;
end;