Blog信息 |
blog名称:注册会计师(注会)练习软件 日志总数:398 评论数量:116 留言数量:27 访问次数:3273956 建立时间:2005年6月6日 |

| |
[delpih编程]delphi中tlist 的使用范例 软件技术
吕向阳 发表于 2009/4/6 9:09:52 |
program testlist;
{$APPTYPE CONSOLE}
uses
SysUtils, Classes;
type
PInfo = ^TInfo;
TInfo = record
Name: string;
Tel: string;
Address: string;
end;
var
SL: TStringList;
List: TList;
AppPath: string;
{ 开辟一个新的PInfo 指针,填入信息并返回指针将在
Command_Delete 或FinaInfo 里释放 }
function MakeInfo(Name, Tel, Address: string): PInfo;
var
P: PInfo;
begin
New(P);
P^.Name := Name;
P^.Tel := Tel;
P^.Address := Address;
{ 返回的指针将被保存在List 里 }
result := P;
end;
{ 在屏幕上打印所有可用的命令 }
procedure PrintMenu;
begin
writeln(\ '======菜单======\');
writeln(\ 'V---- 查看所有人员的信息\');
writeln(\ 'A---- 增添新的人员信息\');
writeln(\ 'D----删除人员\');
writeln(\ 'E---- 修改人员信息\');
writeln(\ 'M---- 查看所有可用命令\');
writeln(\ 'X----退出程序\');
end;
{ 修改人员信息的程序 }
procedure Command_Edit;
var
I: Integer;
Name, Tel, Address: string;
P: PInfo;
begin
write(\ ' 请输入要修改的人员信息的序号:\');
readln(I);
if (I < 0) or (I >= List.Count) then
writeln(\ ' 输入超出范围。\')
else
begin
{ 取得某个人员信息的指针 }
P := List.Items;
writeln(\ ' 开始输入人员信息(若某项信息不需要修改则留
空):\');
write(\ ' 姓名:\');
readln(Name);
write(\ ' 电话号码:\');
readln(Tel);
write(\ ' 地址:\');
readln(Address);
{ 保存输入的信息 }
if Name <> \ '\' then
P^.Name := Name;
if Tel <> \ '\' then
P^.Tel := Tel;
if Address <> \ '\' then
P^.Address := Address;
writeln(\ ' 修改人员信息执行完毕。\');
end;
end;
{ 增加人员信息的程序 }
procedure Command_Add;
var
Name, Tel, Address: string;
begin
writeln(\ ' 开始输入人员信息:\');
write(\ ' 姓名:\');
readln(Name);
write(\ ' 电话号码:\');
readln(Tel);
write(\ ' 地址:\');
readln(Address);
{ 使用MakeInfo 生成TInfo 的指针
并加入Tlist 中 }
List.Add(MakeInfo(Name, Tel, Address));
writeln(\ ' 增加人员信息执行完毕。\');
end;
{ 打印所有人员信息的程序 }
procedure Command_View;
var
I: Integer;
P: PInfo;
begin
writeln(\ ' 人员信息列表:\');
for I := 0 to List.Count - 1 do
begin
P := List.Items;
writeln(IntToStr(I) + \ '号===================\');
writeln(\ '姓名:\' + P^.Name);
writeln(\ '电话:\' + P^.Tel);
writeln(\ '地址:\' + P^.Address);
{ 满六个就暂停,刚好填满一个屏幕 }
if I mod 6 = 5 then
begin
writeln(\ ' 请按回车键继续。\');
readln;
end;
end;
writeln;
end;
{ 删除人员信息的程序 }
procedure Command_Delete;
var
I: Integer;
P: PInfo;
begin
write(\ ' 请输入要删除的人员信息的序号:\');
readln(I);
if (I < 0) or (I >= List.Count) then
writeln(\ ' 输入超出范围。\')
else
begin
P := List.Items;
List.Delete(I);
Dispose(P);
writeln(\ ' 删除执行完毕。\');
writeln;
end;
end;
{ 处理用户输入的命令 }
function GetCommand: Boolean;
{ 返回False 表示退出 }
var
C: Char;
begin
write(\ ' 输入命令并回车:\');
readln(C);
result := True;
case C of
\ 'V\', \ 'v\': Command_View;
\ 'A\', \ 'a\': Command_Add;
\ 'D\', \ 'd\': Command_Delete;
\ 'M\', \ 'm\': PrintMenu;
\ 'X\', \ 'x\': result := False;
\ 'E\', \ 'e\': Command_Edit;
else
writeln(\ ' 未知命令。\');
end;
end;
{ 从Info.txt 把人员信息加载入Tlist }
procedure LoadInfo;
var
I: Integer;
Name, Tel, Address, Index: string;
begin
SL.LoadFromFile(AppPath + \ 'Info.txt\');
for I := 0 to SL.Count div 3 - 1 do
begin
Index := IntToStr(I) + \ '.\';
{ 文件格式:Index.Field=Value
在这里使用Index.X 区别不同序号的人员信息的字段名称
然后通过Values 属性读取信息 }
Name := SL.Values[Index + \ 'Name\'];
Tel := SL.Values[Index + \ 'Tel\'];
Address := SL.Values[Index + \ 'Address\'];
List.Add(MakeInfo(Name, Tel, Address));
end;
end;
{ 把TList 里的人员信息保存到Info.txt }
procedure SaveInfo;
var
I: Integer;
P: PInfo;
begin
SL.Clear; { 清空TstringList }
for I := 0 to List.Count - 1 do
begin
P := List.Items;
SL.Add(IntToStr(I) + \ '.Name=\' + P^.Name);
SL.Add(IntToStr(I) + \ '.Tel=\' + P^.Tel);
SL.Add(IntToStr(I) + \ '.Address=\' + P^.Address);
end;
SL.SaveToFile(AppPath + \ 'Info.txt\');
end;
{ 初始化程序 }
procedure InitInfo;
begin
SL := TStringList.Create;
List := TList.Create;
{ 获得本程序所在的路径 }
AppPath := ExtractFilePath(ParamStr(0));
end;
{ 清空程序使用的内存 }
procedure FinaInfo;
var
I: Integer;
begin
for I := 0 to List.Count - 1 do
Dispose(PInfo(List.Items));
List.Free;
SL.Free;
end;
begin
{ TODO -oUser -cConsole Main : Insert code here }
InitInfo;
LoadInfo;
writeln(\ 'Information Editor V1.0 by VCZH\');
PrintMenu;
while GetCommand do
; { 循环直到返回False }
SaveInfo;
FinaInfo;
writeln(\ ' 谢谢使用。请按回车键退出程序。\');
readln;
end. |
|
|