線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2092
推到 Plurk!
推到 Facebook!

對紅色之條件運算式不了解其意思? 知道的朋友能否說明一下

答題得分者是:syntax
vbkiller
一般會員


發表:65
回覆:28
積分:18
註冊:2007-03-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-09-20 15:49:59 IP:218.164.xxx.xxx 未訂閱
procedure TForm1.Button1Click(Sender: TObject);
var
sr: TSearchRec;
FileAttrs: Integer;
begin
StringGrid1.RowCount := 1;
if CheckBox1.Checked then
FileAttrs := faReadOnly
else
FileAttrs := 0;
if CheckBox2.Checked then
FileAttrs := FileAttrs faHidden;
if CheckBox3.Checked then
FileAttrs := FileAttrs faSysFile;
if CheckBox4.Checked then
FileAttrs := FileAttrs faVolumeID;
if CheckBox5.Checked then
FileAttrs := FileAttrs faDirectory;
if CheckBox6.Checked then
FileAttrs := FileAttrs faArchive;
if CheckBox7.Checked then
FileAttrs := FileAttrs faAnyFile;
with StringGrid1 do
begin
RowCount := 0;
if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then
begin
repeat
if (sr.Attr and FileAttrs) = sr.Attr then
begin
RowCount := RowCount 1;
Cells[1,RowCount-1] := sr.Name;
Cells[2,RowCount-1] := IntToStr(sr.Size);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
end;
st33chen
尊榮會員


發表:15
回覆:591
積分:1201
註冊:2005-09-30

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-09-23 13:56:05 IP:114.32.xxx.xxx 未訂閱
您好,

FileAttrs) = then

對嗎 ?

------
IS IT WHAT IT IS
我是 李慕白 請倒著唸.
又想把老話拿出來說, 請用台語發音 : 專家專家全是ROBOT CAR (滷肉腳啦);
都已接手這麼久了, 績效還是那麼爛, 講話還那麼大聲.
RootKit
資深會員


發表:16
回覆:358
積分:419
註冊:2008-01-02

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-09-23 21:03:14 IP:122.126.xxx.xxx 訂閱
st33chen 說得對應該是那樣寫。

如果你瞭解二進位及 And 閘 就會知道了。
if (sr.Attr and FileAttrs) = FileAttrs then
比較白話表示 FileAttrs 成員包含在sr.Attr 裡。

不想解釋太多,感覺我自己會有點白X。...
AndrewK
高階會員


發表:6
回覆:151
積分:161
註冊:2006-10-09

發送簡訊給我
#4 引用回覆 回覆 發表時間:2009-09-23 23:02:27 IP:58.115.xxx.xxx 訂閱
你可以查一下 TSearchRec 的 Help 說明









faReadOnly 1
faHidden 2
faSysFile 4
faVolumeID 8
faDirectory 16
faArchive 32
faSymLink 64
faAnyFile 71


把上面的值轉成二進位看待,再去思考以下程式碼所代表的意義
if (sr.attr and faReadOnly ) = faReadOnly
------
Just Do It
-------------------------
其實男生不是真的喜歡你不減肥,而是喜歡你愛吃還不肥;也不是真的喜歡你不化妝,而是喜歡你素顏也好看;也不是真的喜歡你瘦,而是喜歡你瘦卻有胸;也不是真喜歡你獨立,而是他忙的時候別煩他。女孩子,太認真你就輸了。
編輯記錄
AndrewK 重新編輯於 2009-09-23 23:14:50, 註解 無‧
AndrewK 重新編輯於 2009-09-23 23:18:57, 註解 無‧
AndrewK 重新編輯於 2009-09-23 23:41:42, 註解 無‧
syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#5 引用回覆 回覆 發表時間:2009-09-29 08:14:23 IP:59.120.xxx.xxx 訂閱
(sr.Attr and FileAttrs) = sr.Attr

(A and B) = C

表示 A 與 B 做 And 運算後的結果,是否等於 C
C 可以是 A 或是 B,或是都不是的另一個數值

至於 And 運算是什麼,請自己 google 或看書(計算計概論)

如 if (011 and 010) = 101 或 if (011 and 010) = 010 或 if (011 and 010) = 011
可以用來做過濾,或是看其一是否包含有另一數值, A, B 誰先誰後並不完全,還需要考慮 C

程式碼的整體意義來看,是在判斷 sr.Attr 是否包含/在範圍內 FileAttrs

如果以「包含」的觀點,C 是應該使用 FileAttrs (sr.Attr and FileAttrs) = FileAttrs ,程式解釋成,如果檔案屬性包含 xxx (我們指定的 FileAttrs),就列出其名稱

如果以「在範圍內」的觀點來看,C 可以使用 sr.Attr (sr.Attr and FileAttrs) = sr.Attr,程式解釋成,如果檔案屬性包含於 FileAttrs 內,就列出其名稱

兩種使用時機與意義不同

===================引 用 vbkiller 文 章===================
procedure TForm1.Button1Click(Sender: TObject);
var
sr: TSearchRec;
FileAttrs: Integer;
begin
StringGrid1.RowCount := 1;
if CheckBox1.Checked then
FileAttrs := faReadOnly
else
FileAttrs := 0;
if CheckBox2.Checked then
FileAttrs := FileAttrs faHidden;
if CheckBox3.Checked then
FileAttrs := FileAttrs faSysFile;
if CheckBox4.Checked then
FileAttrs := FileAttrs faVolumeID;
if CheckBox5.Checked then
FileAttrs := FileAttrs faDirectory;
if CheckBox6.Checked then
FileAttrs := FileAttrs faArchive;
if CheckBox7.Checked then
FileAttrs := FileAttrs faAnyFile;
with StringGrid1 do
begin
RowCount := 0;
if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then
begin
repeat
if (sr.Attr and FileAttrs) = sr.Attr then
begin
RowCount := RowCount 1;
Cells[1,RowCount-1] := sr.Name;
Cells[2,RowCount-1] := IntToStr(sr.Size);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
end;
系統時間:2024-04-25 23:06:51
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!