本人写的一个例子,可能有错误,随便测试了一下,没有问题,先贴上
-----------------------------------------------------------------------------------------------
说明:要按自定义的条件来控制Dbgrid的显示问题
为此,必须先实现,逻辑AND和逻辑OR的运算,既然是串在一起的,就会让人想到逻辑AND和逻辑OR的优先级,
逻辑AND的优先级大于逻辑OR,此处测试就是模拟自定义条件;
条件:(1 AND 0 AND 1 OR 1 OR 1 AND 0 AND 1 OR 1 解释:1表示条件运算后为真,0表示条件运算后为假 ;(1可能是 ID<20的结果,ID值为15))
条件中的值:(10111011 解释:条件中的真假值, 【 1 AND 0 AND 1 OR 1 OR 1 AND 0 AND 1 OR 1】 去掉AND和OR就得到10111011)
条件中的ANDOR:(1100110 解释:1为AND,0为OR;【 1 AND 0 AND 1 OR 1 OR 1 AND 0 AND 1 OR 1】 去掉1和0后,AND用1替代,OR用0替代就得到1100110)
---------------------------------------------------------------------------------------------------------
--Unit
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
Label3: TLabel;
Edit3: TEdit;
Label4: TLabel;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
function GetWhereANDORBool(WhereANDORStr,WhereTFStr:string):Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
vValueString,ANDORString:string;
ANDORS,vStringValues:array of Integer;
ANDORCount,i:Integer;
TF_AND,TF_OR,TF_ANDOR,TF_ANDORCurr,TF_ANDORNext,TF_ANDORPrevious:Integer;//0和1代表的是False和True
ANDORCurr,ANDORNext,ANDORPrevious:Integer;//0和1代表的是OR和AND
begin
{vStringMast:=Edit1.Text;
if vStringMast='' then
Exit;
vValueString:=Edit3.Text;
ANDORString:=Edit2.Text ;
ANDORCount:=Length(ANDORString);
SetLength(ANDORS,ANDORCount);
for i:=0 to ANDORCount-1 do
begin
ANDORS[i]:=ANDORString[i+1];
vStringValues[i]:=vValueString[i+1];
end;
//vStringValues元素比 ANDORS多一个
vStringValues[ANDORCount]:=vValueString[ANDORCount+1];
TF_AND:=1; //都为True
TF_OR:=1; //都为True
TF_ANDOR:=1; //都为True
TF_ANDORCurr:=-1;
TF_ANDORNext:=-1;
TF_ANDORPrevious:=-1;
ANDORCurr:=-1;
ANDORNext:=-1;
ANDORPrevious:=-1;
for i:=0 to ANDORCount-1 do //ANDORS循环
begin
ANDORCurr:=ANDORS[i];
if i=0 then
begin
ANDORPrevious:=-1;
end
else if i<>ANDORCount-1 then
begin
ANDORPrevious:=ANDORS[i-1];
ANDORNext:=ANDORS[i+1];
end
else
begin
ANDORPrevious:=ANDORS[i-1];
ANDORNext:=-1;
end;
if ANDORNext>-1 then //ANDORCurr不是最后一个
begin
if ANDORCurr=ANDORNext then
begin
if ANDORCurr=1 then
TF_ANDOR:=TF_ANDOR and TF_ANDORCurr
else
TF_ANDOR:=TF_ANDOR or TF_ANDORCurr
end
else
begin
if ANDORCurr=1 then //当前是AND
begin
TF_AND:=
TF_ANDOR
end
else
begin
end;
end;
end;
if ANDORS[i]=1 then
begin
TF_AND:=
end;
end;
}
end;
procedure TForm1.Button2Click(Sender: TObject);
var
vStringMast,vValueString,ANDORString:string;
ANDORS,vStringValues:array of Integer;
ANDORCount,i:Integer;
TF_AND,TF_OR,TF_ANDOR,TF_ANDORCurr,TF_ANDORNext:Integer;//0和1代表的是False和True
ANDORCurr,ANDORNext:Integer;//0和1代表的是OR和AND//ANDORPrevious
begin
//vStringMast:=Edit1.Text;
vValueString:=Edit3.Text;
ANDORString:=Edit2.Text ;
if vValueString='' then
Exit;
if (ANDORString='') then
begin
//Result:=vValueString='1';
Exit;
end;
ANDORCount:=Length(ANDORString);
SetLength(ANDORS,ANDORCount);
SetLength(vStringValues,ANDORCount+1);
for i:=0 to ANDORCount-1 do
begin
ANDORS[i]:=StrToInt(ANDORString[i+1]);
vStringValues[i]:=StrToInt(vValueString[i+1]);
end;
//vStringValues元素比 ANDORS多一个
vStringValues[ANDORCount]:=StrToInt(vValueString[ANDORCount+1]);
TF_AND:=-1;
TF_OR:=-1;
TF_ANDOR:=1;
TF_ANDORCurr:=-1;
TF_ANDORNext:=-1;
//TF_ANDORPrevious:=-1;
ANDORCurr:=-1;
ANDORNext:=-1;
//ANDORPrevious:=-1;
if ANDORCount<2 then //只有一个AND或者OR的时候
begin
//代码要补上
Exit;
end;
for i:=0 to ANDORCount-1 do //ANDORS循环
begin
ANDORCurr:=ANDORS[i]; //当前操作的ANDOR
if i=0 then
begin
ANDORNext:=ANDORS[i+1];
if ANDORCurr=1 then
begin
TF_ANDOR:=vStringValues[i] and vStringValues[i+1];
end
else //当前是OR
begin
if ANDORCurr=ANDORNext then //下一个也是OR
begin
TF_ANDOR:=vStringValues[i] or vStringValues[i+1];
end
else//下一个是AND
begin
TF_OR:=vStringValues[i];
end;
end;
end
else if i<>ANDORCount-1 then //ANDORCurr不是最后一个
begin
ANDORNext:=ANDORS[i+1];
TF_ANDORCurr:=vStringValues[i];
TF_ANDORNext:=vStringValues[i+1];
if ANDORCurr=ANDORNext then
begin
if ANDORCurr=1 then //当前操作的是AND
begin
if TF_OR>-1 then //保存真假值
begin
if TF_AND>-1 then//有值了
begin
TF_AND:= TF_AND and TF_ANDORNext;
end
else
begin
TF_AND:=TF_ANDORCurr and TF_ANDORNext;
end;
end
else
begin
TF_ANDOR:=TF_ANDOR and TF_ANDORNext;
end;
end
else
begin
TF_ANDOR:=TF_ANDOR or TF_ANDORNext;
end;
end
else
begin
if ANDORCurr=1 then //当前是AND
begin
if TF_OR>-1 then //上一个有保存
begin
//if then
TF_AND:= TF_AND and TF_ANDORNext;
TF_ANDOR:=TF_OR or TF_AND;
TF_OR:=-1;
TF_AND:=-1;
end
else
begin
TF_ANDOR:=TF_ANDOR and TF_ANDORNext;
end;
end
else//当前的OR
begin
TF_OR:=TF_ANDOR;//TF_ANDORCurr;
end;
end;
end
else //最后一个ANDOR的处理
begin
ANDORNext:=-1;
if ANDORCurr=1 then
begin
if TF_OR>-1 then //上一个有保存
begin
if TF_AND>-1 then
begin
TF_AND:= TF_AND and vStringValues[i+1];
TF_ANDOR:=TF_OR or TF_AND;
TF_OR:=-1;
TF_AND:=-1;
end
else
begin
TF_AND:=vStringValues[i] and vStringValues[i+1];
TF_ANDOR:=TF_OR or TF_AND;
TF_OR:=-1;
TF_AND:=-1;
end;
end
else
begin
//这个不存在的
end;
end
else
begin
TF_ANDOR:=TF_ANDOR or vStringValues[i+1];
end;
end;
end;
if TF_ANDOR=1 then
Label1.Caption:='真'
else
Label1.Caption:='假';
end;
function TForm1.GetWhereANDORBool(WhereANDORStr,
WhereTFStr: string): Boolean;
var
vStringMast,vValueString,ANDORString:string;
ANDORS,vStringValues:array of Integer;
ANDORCount,i:Integer;
TF_AND,TF_OR,TF_ANDOR,TF_ANDORCurr,TF_ANDORNext:Integer;//0和1代表的是False和True
ANDORCurr,ANDORNext:Integer;//0和1代表的是OR和AND//ANDORPrevious
begin
Result:=False;
if WhereTFStr='' then
Exit;
if (WhereANDORStr='') then
begin
Result:=WhereTFStr='1';
Exit;
end;
vValueString:=WhereTFStr;
ANDORString:=WhereANDORStr ;
ANDORCount:=Length(ANDORString);
SetLength(ANDORS,ANDORCount);
SetLength(vStringValues,ANDORCount+1);
for i:=0 to ANDORCount-1 do
begin
ANDORS[i]:=StrToInt(ANDORString[i+1]);
vStringValues[i]:=StrToInt(vValueString[i+1]);
end;
//vStringValues元素比 ANDORS多一个
vStringValues[ANDORCount]:=StrToInt(vValueString[ANDORCount+1]);
TF_AND:=-1;
TF_OR:=-1;
TF_ANDOR:=1;
TF_ANDORCurr:=-1;
TF_ANDORNext:=-1;
//TF_ANDORPrevious:=-1;
ANDORCurr:=-1;
ANDORNext:=-1;
//ANDORPrevious:=-1;
if ANDORCount<2 then //只有一个AND或者OR的时候
begin
//代码要补上
Exit;
end;
for i:=0 to ANDORCount-1 do //ANDORS循环
begin
ANDORCurr:=ANDORS[i]; //当前操作的ANDOR
if i=0 then
begin
ANDORNext:=ANDORS[i+1];
if ANDORCurr=1 then
begin
TF_ANDOR:=vStringValues[i] and vStringValues[i+1];
end
else //当前是OR
begin
if ANDORCurr=ANDORNext then //下一个也是OR
begin
TF_ANDOR:=vStringValues[i] or vStringValues[i+1];
end
else//下一个是AND
begin
TF_OR:=vStringValues[i];
end;
end;
end
else if i<>ANDORCount-1 then //ANDORCurr不是最后一个
begin
ANDORNext:=ANDORS[i+1];
TF_ANDORCurr:=vStringValues[i];
TF_ANDORNext:=vStringValues[i+1];
if ANDORCurr=ANDORNext then
begin
if ANDORCurr=1 then //当前操作的是AND
begin
if TF_OR>-1 then //保存真假值
begin
if TF_AND>-1 then//有值了
begin
TF_AND:= TF_AND and TF_ANDORNext;
end
else
begin
TF_AND:=TF_ANDORCurr and TF_ANDORNext;
end;
end
else
begin
TF_ANDOR:=TF_ANDOR and TF_ANDORNext;
end;
end
else
begin
TF_ANDOR:=TF_ANDOR or TF_ANDORNext;
end;
end
else
begin
if ANDORCurr=1 then //当前是AND
begin
if TF_OR>-1 then //上一个有保存
begin
//if then
TF_AND:= TF_AND and TF_ANDORNext;
TF_ANDOR:=TF_OR or TF_AND;
TF_OR:=-1;
TF_AND:=-1;
end
else
begin
TF_ANDOR:=TF_ANDOR and TF_ANDORNext;
end;
end
else//当前的OR
begin
TF_OR:=TF_ANDOR;//TF_ANDORCurr;
end;
end;
end
else //最后一个ANDOR的处理
begin
ANDORNext:=-1;
if ANDORCurr=1 then
begin
if TF_OR>-1 then //上一个有保存
begin
if TF_AND>-1 then
begin
TF_AND:= TF_AND and vStringValues[i+1];
TF_ANDOR:=TF_OR or TF_AND;
TF_OR:=-1;
TF_AND:=-1;
end
else
begin
TF_AND:=vStringValues[i] and vStringValues[i+1];
TF_ANDOR:=TF_OR or TF_AND;
TF_OR:=-1;
TF_AND:=-1;
end;
end
else
begin
//这个不存在的
end;
end
else
begin
TF_ANDOR:=TF_ANDOR or vStringValues[i+1];
end;
end;
end;
Result:=TF_ANDOR=1;
{ if TF_ANDOR=1 then
Label1.Caption:='真'
else
Label1.Caption:='假'; }
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if GetWhereANDORBool(Edit2.Text,edit3.Text) then
Label1.Caption:='真'
else
Label1.Caption:='假';
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if '-1'<'0' then
begin
ShowMessage('0');
end;
end;
end.
--------------Form
object Form1: TForm1
Left = 521
Top = 348
Width = 488
Height = 227
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 160
Top = 120
Width = 129
Height = 16
Alignment = taCenter
AutoSize = False
end
object Label2: TLabel
Left = 8
Top = 24
Width = 88
Height = 13
Alignment = taRightJustify
AutoSize = False
Caption = '条件'
end
object Label3: TLabel
Left = -1
Top = 88
Width = 97
Height = 13
Hint = 'AND为1,OR为0'
Alignment = taRightJustify
AutoSize = False
Caption = '条件中的ANDOR'
ParentShowHint = False
ShowHint = True
end
object Label4: TLabel
Left = 7
Top = 56
Width = 89
Height = 13
Alignment = taRightJustify
AutoSize = False
Caption = '条件中的值'
end
object Button1: TButton
Left = 16
Top = 160
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object Edit1: TEdit
Left = 104
Top = 22
Width = 329
Height = 21
ImeName = '中文(简体) - 搜狗拼音输入法'
ReadOnly = True
TabOrder = 1
Text = '1 AND 0 AND 1 OR 1 OR 1 AND 0 AND 1 OR 1'
end
object Edit2: TEdit
Left = 104
Top = 86
Width = 329
Height = 21
ImeName = '中文(简体) - 搜狗拼音输入法'
TabOrder = 2
Text = '1100110'
end
object Edit3: TEdit
Left = 104
Top = 54
Width = 329
Height = 21
ImeName = '中文(简体) - 搜狗拼音输入法'
TabOrder = 3
Text = '10111011'
end
object Button2: TButton
Left = 128
Top = 144
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 4
OnClick = Button2Click
end
object Button3: TButton
Left = 240
Top = 144
Width = 75
Height = 25
Caption = 'Button3'
TabOrder = 5
OnClick = Button3Click
end
object Button4: TButton
Left = 384
Top = 144
Width = 75
Height = 25
Caption = 'Button4'
TabOrder = 6
OnClick = Button4Click
end
end