给大家共享自己写的一点东西:一个字符串列表(0分)

  • 主题发起人 枫晚东园
  • 开始时间

枫晚东园

Unregistered / Unconfirmed
GUEST, unregistred user!
自学了五六个月,感觉DFW的离线资料很有帮助,写点东西给Show一下,大家多提意见。。
自己写的一个TStrList,基本可以代替TStringList。
unit MyList;

interface

uses
Windows;

type
PStrItem = ^TStrItem;
TStrItem = record
Num:Integer;
Str:String;
Next:pStrItem;
end;
TStrList = Class
Private
FListHead: PStrItem;
FCount : Integer;
Const StrItemLen = Sizeof(TStrItem);
Protected
Function GetCount: Integer;
Function GetTextStr:String;
Procedure SetTextSrt(const Value: string);
Procedure Update;
Function Get(Index: Integer):String
//根据行索引数得到内容
Procedure Put(Index: Integer
const S: string);//写入内容
Public
Constructor Create

Destructor Destroy
override;
Procedure Add(const S:String);
Procedure Del(const Index:Integer);
Procedure Clear;
Property Strings[Index: Integer]: string read Get write Put
default;
Property Text:String read GetTextStr Write SetTextSrt;
Property Count: Integer read GetCount;
End;

implementation

//---------------------------------------------------------TStrList
Constructor TStrList.Create

begin
FCount := 0
//FCount是行数,它的值就是最大能达到的行值
// FillChar(FListHead,StrItemLen,0)
//初始化String
end;

Destructor TStrList.Destroy

begin
Clear;
inherited Destroy;
end;

Procedure TStrList.Update

var
P:pStrItem;
I:Integer;
begin
P := FListHead;
if FCount <> 0 then //检查表是否为空
for I := 0 to FCount - 1 do begin
P^.Num := i + 1;
P := P^.Next;
if I = FCount - 1 then P^.Next := nil;
end
//末节点指针清零
end;

Procedure TStrList.Del(const Index:Integer)

var
P1,PNext,PLast : PStrItem;
I:Integer;
begin
if (FCount = 0) or (Index > FCount) then exit;
P1 := FListHead;
PLast := P1;
Try PNext := P1^.Next;
Except PNext := nil;
End;
for I := 0 to FCount - 1 do begin
if Index = P1^.Num then begin
if P1 = FListHead then FlistHead := PNext;
FreeMem(P1);
if PNext <> nil then PLast^.Next := PNext
Else PLast^.Next := nil;
FCount := FCount - 1;
Update;
exit;
end;
P1 := P1^.Next;
if PLast <> P1 then PLast := PLast^.Next;
Try PNext := P1^.Next;
Except PNext := nil;
End;
end;
end;

Function TStrList.GetCount: Integer

begin
Result := FCount;
end;

Function TStrList.GetTextStr:String

var
I:Integer;
P:pStrItem;
begin
P := FListHead;
if FCount = 0 then exit
//这时是错误结果
for I := 0 to FCount - 1 do begin
Result := Result + P^.Str + #13#10;
P := P^.Next;
end;
end;

Procedure TStrList.SetTextSrt(const Value: string)

var
I{,ITemp}:Integer;
P:pStrItem;
Temp:String;
Word:String[1];
begin
{ for i:=1 to length(Value) do
If (Value = #13) or (Value = #10) Then ITemp := ITemp + 1;
SetLength(Temp, Length(Value) - ITemp);
//这样不知道是否好一些,测试结果都是0ms。。 }
if FCount = 0 then exit;
P := FListHead;
for I := 1 to Length(Value) do begin
Word := Value;
if (Word <> #13) and (Word <> #10) then Temp := Temp + Word
Else if Word <> #10 then begin
P^.Str := Temp;
If I <> Length(Value) - 1 Then P := P^.Next;
if (P = nil) and (Length(Value) <> i + 1) then
add('')
//当下一个节点为空且文本未到头时增加一个结点
Temp := ''
//正常情况下文本长度比已经过的文本多1
end;
end;
if P^.Next <> nil then //释放多余空间
for I := FCount downto P^.num + 1 do Del(I);
end;

procedure TStrList.Clear

var
I : Integer;
P1,P2 : PStrItem;
begin
if FCount = 0 then exit;
P1 := FListHead;
for I := 0 to FCount - 1 do begin
P2 := P1^.Next;
FreeMem(P1);
if p2 <> nil then P1 := P2;
end;
FCount := 1;
end;

Function TStrList.Get(Index: Integer):String

var
i:Integer;
P:pStrItem;
begin
If Index > FCount then exit;
P := FListHead;
For I := 0 to FCount - 1 do begin
If P^.Num = Index then begin
Result := P^.Str;
exit;
end Else P := P^.Next;
end;
end;

Procedure TStrList.Put(Index: Integer
const S: string);
var
i:Integer;
P:pStrItem;
begin
If Index > FCount then exit;
P := FListHead;
For I := 0 to FCount -1 do begin
If Index = P^.Num then begin
P^.Str := S;
exit;
end;
P := P^.Next;
end;
end;

Procedure TStrList.add(const S:String)

var
i:Integer;
p1,p2:pStrItem;
begin
P1 := FListHead

P2 := P1
//操作基本无意义,目的仅为消除警告
For I := 0 to FCount + 1 do begin
If P1 = nil then begin //因为I初值为0,而第一次add时这样显然
GetMem(P1,StrItemLen);//是不行的,而且后面一旦P1合适后马上就
break
//会break,因此不用担心循环次数过多
end;
P2 := P1;
P1 := P1^.Next;
end;
// FillChar(P2,StrItemLen,0)
//初始化String
P1^.Next := nil;
P1^.Num := FCount + 1
//FCount还没自己加,呵呵
P1^.Str := S;
if FCount = 0 then FListHead := P1;
if FCount <> 0 Then P2^.Next := P1;
FCount := FCount + 1;
end;

外加一个字符串的替换:
function StrReplace(S,OldPart,NewPart:String):String;
var
i,i2:integer;
begin
I2 := 1;
for I := 0 to Length(S) - 1 do
if Copy(S,i,6) = OldPart then begin
I2 := I + 6;
Result := Result + NewPart;
end else if I = I2 then begin
Result := Result + S;
I2 := I2 + 1;
end;
Result := Result + S[Length(S)];
end;
以上代码Win2K3 Sp2 + D2007编译通过

有些东西很不解,Delphi里最基本的关键字是怎么被电脑识别的。。比如if,如果追到汇编有可能是je jne那些跳转,但是再追到机器码呢?在追下去机器是怎么理解的呢?
我觉得这也许和我们平时对东西的认识方法是相同的,而学会认识一样东西正是人工智能很难处理的东西之一。所以说,人工智能的钥匙应该早就被人类所掌握了。像现在一切要写代码,这个语言那个语言,总觉得 好像是被前辈忽悠了,而计算机的设计初衷似乎并不是这个。
 
多谢分享,先看看.
 

Similar threads

I
回复
0
查看
604
import
I
I
回复
0
查看
670
import
I
I
回复
0
查看
530
import
I
I
回复
0
查看
678
import
I
I
回复
0
查看
544
import
I
顶部